You Can Make A Plugin & A Shortcode Too

 

you can create a plugin and a shortcode too.

Much of this code and these examples can be found in the Plugin Developer’s Handbook area of the WordPress Codex.  This post is a guide for our in-person meetup but can be used to dig deeper later.

WordPress Plugins

Writing a plugin is one of those abilities that will give you superpowers in WordPress.   Of the 46,000 + plugins that are currently on the WordPress Directory you are bound to find a plugin to fit your need.  The great thing about WordPress Is that it is infinitely extendible and even if you do not find a plugin to fit your need you can always create your own plugin.

If you are setting out on the road to create your own plugin, what are the absolute basics that you will need?

A Basic Plugin

A WordPress Plugin is made out of PHP so for the basics of a plugin all you really need is a PHP file.   You can use whatever name you like but you will need to add  some very specific code to this PHP file.

/*
Plugin Name: My Plugin
*/

Yes, this is all you actually need in a PHP file to begin your first plugin.  This actually does not do anything.  For your plugin to actually have functionality you will need to add functions to the plugin.  Before we  actually place functions in your code you will need to know where you will need to place the plugin in your file directory.

All of your plugins will be located in the WordPress plugin directory.  The plugin directory is located here (In relation to your root directory):

  • /root directory
    • /wp-content
      • /themes
      • /plugins
        • my-plugin.php
    • /wp-admin
    • /wp-includes

Plugins Functions

Functions in your plugin can range from the simple to incredibly complex.  This Post will not go over how to write PHP functions but if you are familiar with PHP then you can definitely write your own.  The best way for you to proceed in developing a WordPress Plugin that will play nicely with all of the other 48 K plus plugins in the WordPress directory is for you to follow the WordPress coding guidelines and use all of the current WordPress API”s that are available to you.

WordPress Coding Standards

The WordPress Coding Standards are a set of rules & best practices that are put out by the community that ensure consistent quality of code regardless of who the code is written by.  You can find the complete documentation and begin to familiarize yourself with all of the WordPress Coding Standards for HTML, CSS, PHP & JS in the WordPress Codex.

WordPress API’s

If you are actually going to dig into the WordPress API’s further than you will discover true power for controlling the technology behind WordPress.  In order for you to find out more about the WordPress API’s you can go to the WordPress Codex and read about them.   WordPress API Codex

Writing A Simple ShortCode

From the Plugin Developers Handbook:

Shortcodes are registered with WordPress by declaring a shortcode and assigning a callback. At it’s simplest, here’s a Hello Dolly example:

function hello_dolly_shortcode() {
return "Hello Dolly";
}
add_shortcode( 'hello-dolly', 'hello_dolly_shortcode' );

[hello-dolly] is your new shortcode, and use of the shortcode will trigger the hello_dolly_shortcode() callback function.