Create a Simple WordPress Plugin Tutorial

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 file structure

wordpress plugin file structure

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

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.

See, we call function simple_shortcode_register_shortcodes when the wordpress system init and we need to define this function.

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:

 

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

8 comments

  1. Nice tutorial ..easy and clean..thnks

  2. This has helped me get started. Thanks.

  3. I work very well. Keep continue the tutorial, I like the way you teach me very much. Thanks

  4. Can you make another tutorial about create a simple wordpress theme? I need it to get started. Thanks

  5. 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

CDN by MaxCDN