Semalt Expert: How To Create A Simple WordPress Plugin

Creating a custom WordPress plugin is easy. Most people may find this aspect beneficial to handle their modifications and many needs. Plugins are pieces of PHP code which alter the way your website operates. It is possible to create this code and add it to your WordPress website without having to modify the main code of the website. In some cases, you may need to add a unique feature to your site, one which does not have a ready-made commercial plugin.

In this SEO article, provided by Andrew Dyhan, a leading expert from Semalt, you will learn how to create your custom WordPress plugin.

Basic structure of a WordPress plugin

In this SEO guide, we will focus on creating a WordPress plugin. This is a simple PHP file which contains some instructions in it. In my SEO opinion, it is always good to create a folder and put the PHP file inside. This folder should contain a similar name with the one on the plugin. After creating this file, it is essential to upload it to wp-content/plugins folder on your server host. A plugin has some few basics. For instance, there is the header.

WordPress plugin header

A simple header has a small functional structure which WordPress can recognize. For instance, it has:

<?php

Plugin Name: Our New Plugin

This is the simplest format of a plugin header which is compatible with the WordPress content management system. By doing this step, you get a working plugin which you can be able to activate in your WordPress plugins area. However, there are no functions on it. Hence it will not alter any functionality of your website. The header can also contain some other information such as the author, description, version, etc. Some of this information may be necessary for future developmental purposes.

The rest of the plugin

There is no limit to the instructions you can put on your plugin. However, it is essential to consider the response of your website as a construction strategy. Putting too much code can make your site become heavily modified. You can modify your plugin as much as you need. It is a simple PHP file which you can modify just like any theme, any modification which you can make to your functions.php file. From a basic point, it is essential to minimize the changes which you can put in a WordPress website. For instance, I can use this snippet to help my website redirect a page to another, even on an entirely new website;

function my_custom_redirect () {

global $post;

if ( is_page() || is_object( $post ) ) {

if ( $redirect = get_post_meta($post->ID, 'redirect', true ) ) {

wp_redirect( $redirect );

exit;

add_action( 'get_header', 'my_custom_redirect' );

This snippet has one simple function. It can allow you to add a custom meta to any page. Furthermore, it can help you to add a custom 'redirect' using a different URL. In my previous header, the whole plugn would look like;

<?php

/*

Plugin Name: Our New Plugin

function my_custom_redirect () {

global $post;

if ( is_page() || is_object( $post ) ) {

if ( $redirect = get_post_meta($post->ID, 'redirect', true ) )

wp_redirect( $redirect );

exit;

add_action( 'get_header', 'my_custom_redirect' );

It is that simple to create a custom WordPress plugin. Like in the case above, we have made a plugin which can be able to redirect pages. You can also add a different line of PHP code to perform different functions on your WordPress website.