Create WordPress plugin function

To create your first WordPress plugin, you will want to create the main function for your plugin’s logic. This will control what your plugin does after you’ve already created a WordPress plugin script.

Creating a WordPress function for a plugin

The first thing you’ll want to do before creating a function for your WordPress plugin, is try to think of a unique name. Make sure that no other WordPress plugin developer would use the same function in their own plugin.

In our example, we are calling the function for our WordPress Extra Post Info plugin simply extra_post_info.

Check to ensure function name is unique

You’ll want to wrap your entire plugin function inside of a if( !function_exists ) statement. That way, we only run our plugin’s function if a function of that same name hasn’t already been created by another WordPress plugin:

if( !function_exists("extra_post_info") ) {  }

Create our plugin’s main function

Now that we know our plugin name is unique, we want to actually start with the logic for our plugin. Our plugin will simply add some extra info to a post, so we’ll start off very simply by just manually defining what info we want added.

We start by declaring function extra_post_info($content) which creates the function called extra_post_info. This function pulls in the default WordPress $content variable, which as you might guess is the content of the post we want to add extra info too.

Within the newly created function, we setup a variable called $extra_info which simply stores the extra text we’d like to add to our post, in this case EXTRA INFO.

Finally before closing our function, we run return $content . $extra_info which goes ahead and returns the original content of the post to WordPress, but with our added text appended to the end:

function extra_post_info($content) {   $extra_info = "EXTRA INFO";   return $content . $extra_info; }

Use add_filter to make WordPress display update

Now in order for our newly created extra_post_info function to actually update the content of a WordPress post, we need to use the WordPress add_filter(‘the_content’, ‘extra_post_info’) hook.

This hook lets WordPress know we want to filter our content before displaying. The second argument of extra_post_info sets the function we want to filter our content through.

add_filter('the_content', 'extra_post_info');

Full WordPress function results

If you followed along above, the full WordPress plugin function so far should look like this:

if( !function_exists("extra_post_info") ) {    function extra_post_info($content)   {     $extra_info = "EXTRA INFO";     return $content . $extra_info;   }  add_filter('the_content', 'extra_post_info');  }

Plugin results when activated

first extra info text added to post

Create WordPress admin menu for your plugin

Once the basics for your WordPress plugin is laid out, you’ll want to create a WordPress admin menu so users can alter your plugin settings easily.

Stormy Scott
Stormy Scott Content Marketing Writer

Stormy is a Content Marketing Writer at InMotion Hosting. Her content focus is WordPress, web design, and help articles for small businesses.

More Articles by Stormy

Was this article helpful? Let us know!