How to Make a Simple WordPress Plugin on the Post Edit Screen

How to Make a Simple WordPress Plugin on the Post Edit Screen

There are many tutorials on making WordPress plugins, but I hope to offer you a baby version of that for those who are just learning about WordPress development. I’m going to show you how to add a meta box on your post edit screen, that you can fill in with any Php code you desire.

There are only 3 parts to making a plugin:

  • 1. The information about your plugin
  • 2. Initialize your plugin by hooking into an action
  • 3. Making whatever code you want

 

Setting up the information about your plugin

Create a new php file with the following information at the top:

This information is displayed in the backend of your WordPress site under plugins. Be sure to save your file with whatever filename you want, but try to keep it unique i.e. your-plugin-name-index.php.

 

Initialize your plugin by hooking into an action

Hooking into an action is simply telling WordPress to ‘please load my function’ when you are doing ‘this’ action. In the above example, the function add_action is telling WP to load the function ‘your_plugin_add_custom_box, when it is firing off the action ‘add_meta_boxes’.

This will force WP to execute your function ‘your_plugin_add_custom_box’, if it exists and if there isn’t already another function called that, so be sure to use unique names.

Here is what will be contained inside the function ‘your_plugin_add_custom_box’:

It contains a WP function ‘add_metabox()’ that adds a meta box on the posts edit screen. I’ve set 4 parameters: 1. the ID of the box 2. the title, 3. the function to load 4. the post type(i.e. Pages, posts)

So far, we’ve basically told WP that it should load our custom meta boxes while it’s loading it’s meta boxes.

 

Your actual code → The fun part

When we added ‘add_metabox()’ we told it to call the function ‘your_plugin_inner_custom_box’. You don’t have to pass the function anything, but I am passing the $post object to access the content($post->post_content).

I’m going to make it display the word count of the current post.

That’s not a very useful plugin considering WP already gives you a word count at the bottom of the editor, but it’s just to give you an idea.

The last part would be to save all your changes, zip your file and upload & activate it under plugins. If you are writing a small plugin, you could just add it to your theme’s function file.

Tags:
,
No Comments

Sorry, the comment form is closed at this time.