Making a wordrpess plugin to produce short codes

  • Making a wordrpess plugin to produce short codes

    These are useful for times that we need to reuse pieces of code on multiple pages or websites, or if we need to use php on a page. We can place php within a short code, and place the short code in a WordPress page or post and have the php execute. There are some plugins that allow you to use php on the page or post itself, but if anyone at all has access to wordpress, then they have full access to your database as well. So what I do, is create my own plugin for all of the shortcodes I need in a project, and call them on the pages or posts where I need them.

    For this example, Im making it as easy as possible. All it will do, is when you type the shortcode [helloWorld] it will output “Hello world!”. You can make it output whatever you want. Toss in some loops and grab the data from the database and really your imagination is the only limit.

    So first thing is first. Come up with a name for your plugin. Probably HelloWorld would work great for this example, because it is a hello world plugin. So, create a folder called HelloWorld. Next, open up your favorite text editor, and save it as helloWorld.php. Add the php tags, and our comment header so that wordpress recognizes the plugin as something it can actually use.

    CODE IMAGE

    So at this point, we have a plugin, that accomplishes nothing. Nice. Lets create a shortcode function that we want to use on some page. Call it hello_world_func(). Add the _func because after the function, we hook the function to the shortcode, and the function and shortcode have to be different and it seems to be the standard for actually naming the functions for shortcodes. Inside the function, let’s echo ‘Hello World!’ After the function, we hook this function to a shortcode, using one simple line.

    CODE IMAGE

    Just change your function name, and the shortcode and you can make your own shortcodes.

    mikegustafson

    Leave a comment