WordPress Plugin ‘s quite easy with a lot of fun to get started with it. It just take you 5 minutes to create a simple wordpress plugin with the useful functionality. In this blog post, I will show you a very simple wordpress plugin tutorial with very first step to build a quality wordpress plugin. After this, you are free to extension unlimited your plugin.
WordPress Plugin Idea
Before building any plugin, we should site down and write all of our requirements to save time and do the right way in coding time. In my simple wordpress plugin idea include:
- Name: WordPress Simple Shortcode
- Description: implement a custom shortcode what only use for personal purpose
- Publication: for blog post tutorial on wp2x.com
Now, let ‘s go with our first wordpress plugin
WordPress Plugin Basic File Structure
As the minimum requirements, wordpress plugin only need one php file in folder /wp-content/plugins/ but no one do thing like that since it ‘s very difficult to maintain. So even though we do a simple wordpress plugin, let ‘s learn for future to build a complex plugin. So, the basic wordpress plugin file structure include:
- A folder in /wp-content/plugins/: let ‘s create it is simple-shortcode
- A php file in plugin folder /wp-content/plugins/simple-shortcode with the same name as plugin folder, so it should be /wp-content/plugins/simple-shortcode/simple-shortcode.php
WordPress Plugin Header
Somehow WordPress need to know the basic information of your wordpress plugin to display in control panel, kind of detail of a plugin. And here is all that needs to be in your plugin file for WordPress to recognize it once uploaded. Let ‘s add it as first line in file simple-shortcode.php
1 2 3 4 | <?php /* Plugin Name: My Custom Functions */ |
See how easy that is? At this point you have a plugin that you can activate in your WordPress plugins area. Of course our plugin doesn’t actually do anything yet but the point is you have laid the foundation of your very own plugin and it was super easy.
Now there are other elements that you can include in this header. Things like a description, version, author, etc. You can read more about those here: http://codex.wordpress.org /Writing_a_Plugin #File_Headers
That ‘s all to get a new wordpress plugin, let visit your wordpress plugin admin and activate it. Time to build the main function
Capture wordpress hooks
Keep in mind that to get our plugin optimize, we only execute our action code via wordpress shortcode except some special case. For the most case, we will capture the init hook of wordpress. Here is the code.
1 | add_action( 'init', 'simple_shortcode_register_shortcodes'); |
See, we call function simple_shortcode_register_shortcodes when the wordpress system init and we need to define this function.
1 2 3 4 | function simple_shortcode_register_shortcodes() { add_shortcode('my-first-shortcode', 'simple_shortcode_my_first_shortcode'); } |
Again we add a shortcode with tag my-first-shortcode and defined by function simple_shortcode_my_first_shortcode. One more time we need to define this function:
1 2 3 4 5 6 | function simple_shortcode_my_first_shortcode($atts, $content){ extract(shortcode_atts(array( 'lastname' => '' ), $atts)); return "<strong>{$content} {$lastname}"; } |
Ok, it ‘s quite simple, for more detail how to define this function you can look at wordpress API https://codex.wordpress.org/Function_Reference/add_shortcode . And I guess, you will know where to search for WordPress API :-).
The next step, let ‘s test it. Create a new post with this detail:
[code]Hello [my-first-shortcode lastname=”Pham”]David[/my-first-shortcode][/code]Now, view it on front-end to see the magic.
And here is the bonus for you if you need more advance about wordpress plugin https://codex.wordpress.org/Writing_a_Plugin
Nice tutorial ..easy and clean..thnks
This has helped me get started. Thanks.
I work very well. Keep continue the tutorial, I like the way you teach me very much. Thanks
Thanks Laura, it help me a lot to release more tutorial 🙂
Can you make another tutorial about create a simple wordpress theme? I need it to get started. Thanks
I am looking forward to the tutorial about wordpress theme, it ‘s nice if you make it. Thanks
Sure, we will make the wordpress theme tutorial pretty soon. Thanks
It ‘s really useful for me to get started with new wordpress plugin. If you have time, I think you should add more tutorial about this to help more user about this. Thanks