Home

2 minute read

Creating your first WordPress Plug-in

Tony Lea

Creating your first WordPress Plug-in

Wordpress is great for writing posts, adding videos, and many other of your media blogging needs. Well, when you want to extend WordPress to be 'all it can be', you may want to start diving into creating WordPress Plug-ins. Which are surprisingly easy to create. All that is needed to create a Plug-in is to add a new folder (name the folder the same as the plug-in name) in the 'wp-admin/plugins' folder. Then add an 'index.php' file in that folder. The index.php file will contain the code for your plug-in. I'm going to show you the code for the simplest Wordpress Plug-in. The code is as follows:

<?php   
   
/* 
	Plugin Name: Plugin Test
	Plugin URI: http://www.yourdomain.com
	Description: Description of the Plug-in
	Version: 1.0 
	Author: Author Name
	Author URI: http://www.author_domain.com
*/  

add_filter('the_content', 'function1');

function function1($content) {  

	return $content.' END OF CONTENT';

}

?>

In the above code you will need to fill in the according sections of the header, such as Plug-in Name, Description, Author. All this information is inside of a PHP comment enclosed by /* content here */. This information will be shown in the admin Plug-in section, shown in the image below:

Now, on to the rest of the code, the following line will add a filter to WordPress. The filter will notify WordPress to run the corresponding function before displaying the content of a WordPress post.

add_filter('the_content', 'function1');
</pre>

And in the function 'function1' we accept a variable containing the content of the post. Inside the function we will return the content with the string 'End of Content' concatenated at the end.

<pre lang="PHP">

function function1($content) {  

	return $content.' END OF CONTENT';

}

So, now if our Hello World Post is displayed it will look as follows:

This was a very simple example of how you can create a plug-in to modify the content of a WordPress post. There are many other types of easy or more complicated functionalities you can perform with Wordpress Plug-ins. As a start you may want to head over to the WordPress Codex Filter Reference page and view all the different types of plug-in filters in WordPress.

These different types of filters will affect WordPress in multiple different ways. For instance our the_content filter added above allowed us to manipulate the content of the WordPress post before it was displayed on the screen :)