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

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.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Like this Article?

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

0 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!