Custom Wordpress Shortcode Plugin - Tutorial / Howto

PHPWordpress

Please contribute by voting. Thanks!
1

This is how you create your own shortcode plugin in Wordpress. It's very easy, as you will see in a moment.

Wordpress Shortcodes are a simple and nice way to enrich your posts and pages. The Shortcodes are in the following form [this_is_a_shortcode] and will be replaced with whatever data that you desire when the page / post is rendered (viewed). You can read more about Shortcodes at Wordpress.org.

Please note that PHP and Wordpress have been around for quite some time and have both evolved, be aware of old patterns and information in outdated articles that might be insecure / not work at all.

Step 1 - Create your Wordpress plugin

  1. Find you plugin folder (typically under wp-content/plugins/)
  2. Create a new folder under the plugin folder, name it after your plugin (e.g. wp-content/plugins/testshortcode)
  3. Create a new php-file in that folder, name it after your plugin (e.g. wp-content/plugins/testshortcode/testshortcode.php)
  4. Add plugin header / manifest to your plugin's PHP file by copying and pasting the following content into the file:

testshortcode.php

<?php
/*
Plugin Name: TestShortcode
Plugin URI:  http://your.url
Description: Wordpress-plugin for testing Wordpress shortcodes
Version:     1.0.0
Author:      Your Name
Author URI:  http://your.url
License:     GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wporg
Domain Path: /languages
*/

// todo: plugin code goes here...

?>

So now you have your own Wordpress plugin which should be listed in your Wordpress installation's plugin administration.

Step 2 - Add your Shortcode code

Time to add some code! The first thing that we will do is to create a function that will return some html. The function will get called by Wordpress when a page / post that contains is about to be rendered. Add the following code into your file testshortcode.php:

funtion get_shortcode_this_is_a_shortcode()
{
    return "<p>Hello from <b>Custom Shortcode Plugin</b></p>";
}

The second piece of code is where we register our function with Wordpress. The call connects our Shortcode text [this_is_a_shortcode] with our callback function. Just add the following code into testshortcode.php:

add_shortcode('this_is_a_shortcode', 'get_shortcode_this_is_a_shortcode');

Easy right!

The testshortcode.php file should now look like this:

<?php
/*
Plugin Name: TestShortcode
Plugin URI:  http://your.url
Description: Wordpress-plugin for testing Wordpress shortcodes
Version:     1.0.0
Author:      Your Name
Author URI:  http://your.url
License:     GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wporg
Domain Path: /languages
*/

funtion get_shortcode_this_is_a_shortcode()
{
    return "<p>Hello from <b>Custom Shortcode Plugin</b></p>";
}

add_shortcode('this_is_a_shortcode', 'get_shortcode_this_is_a_shortcode');

?>

Step 3 - Enable your plugin

Now it's time to head over to the administrative panel of your Wordpress installation. Go to the plugins section and list all installed plugins. Locate your plugin and press the Activate link.

Step 4 - Test your plugin

Edit an existing page / post or create a new one, then add your shortcode token [this_is_a_shortcode] somewhere in the body. If you save and view the page / post your will now see the text:

Hello from Custom Shortcode Plugin

Article created: Jan 4 at 07:53. Edited Jan 9 at 06:32.

Your comment

You need to sign up / log in to comment this article

Author

Created by Robert Bengtsson [107] Jan 4 at 07:53

Share article

Do you know about

Domain-driven Design (DDD)?

Write an article