Creating A WordPress Shortcode Using A Custom Functions Plugin

WordPress comes with a all the blogging functionality you need to create a basic blog but that might just not be enough at times. You might need to do much more than have a static page or posts on your pages. Many developers wish they could write a little PHP in the Page/Post editor. Well one way to that is to write a shortcode.

Many WordPress plugins come with shortcodes. Just in case you don’t want to install a plugin or you just want to create yours, it’s quite easy.

Where do you place your function?

Creating a shortcode requires you to write a simple function. The big question is “where do you put the function?” You can place your function either in your theme’s function.php or create a custom functions plugin. Placing your function in your theme’s function.php is not a bad idea especially if the code is directly related to the theme you’re using.

For code not necessarily tied to the theme such as shortcodes, placing them in the theme’s function file may not be the best idea since you might want to retain those functionalities if you switch themes. Of course, you can transfer one theme’s functions.php code over to another theme, but why not make it even easier, requiring no transfer of code.

Creating a custom functions plugin

In your wp-content folder, create a folder mu-plugins. The MU stands for “must use” What this means is that any plugins in this folder automatically run on all sites (single site or all sites on a multi-site install). No activation required.

Now create a file called custom-functions.php in the mu-plugins and place the following code

<?php
/**
 * Plugin Name: Custom Functions
 * Plugin URI: http://yoursite.com
 * Description: This is an awesome custom plugin with functionality that I'd like to keep when switching things.
 * Author: Your Name
 * Author URI: http://yoursite.com
 * Version: 1.0
 */

/* Place your custom code below this line. */
?>
If you check your plugins page in the WordPress admin you will see a new option Must-Use(1) as shown below

Writing the Shortcode

In the custom functions file place the following code

function hello_func( $atts ){

    return "Hello, I’m a shortcode";

}
add_shortcode( 'hello', 'hello_func' );
Now in your post/page editor place the shortcode [hello] and view the page on the browser. Cool right?

Passing parameter to a shortcode

Just like functions you can pass a parameter to shortcodes. The code below shows a simple way to prep your function to receive parameters.

function hello_func( $atts ){
    extract(shortcode_atts(array(
    "message" => "Default message",
    "tag" => "p",
  ), $atts));
        $msg = "<{$tag}>You passed {$message}</{$tag}>"; 
    return $msg;
}
add_shortcode( 'hello', 'hello_func' );
The extract(shortcode_atts(array())) function is used to assign default values to your parameters incase no parameter is specified

You can now use the shortcode [hello message=Hi tag=p]

A Web designer and developer. Speaks PHP, Java, Javascript, HTML

Leave a Reply

Your email address will not be published. Required fields are marked *