21 Sep 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /* Plugin Name: Your Plugin Name Plugin URI: http://your-domain.com Description: Description of your plugin Author: Your Name Version: x.x Author URI: http://author-comain.com */ |
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
1 | add_action( 'add_meta_boxes', 'your_plugin_add_custom_box' ); |
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’:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function your_plugin_add_custom_box() { add_meta_box( 'your_plugin_div_id', 'My Title', 'your_plugin _inner_custom_box', 'post' ); } |
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.
1 2 3 4 5 6 7 8 9 10 11 | function your_plugin_inner_custom_box( $post ) { $text = strip_tags($post->post_content); $words = explode(" ",$text); $total_words = count($words); echo '<h2>Word Count: ' . $total_words . '</h2>'; } |
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.
Sorry, the comment form is closed at this time.