Articles

article How to Add A Shortcode in WordPress?

How to Add A Shortcode in WordPress?

When a shortcode is inserted in a WordPress post or page, it is replaced with some other content. In other words, we instruct WordPress to find the macro that is in square brackets ([]) and replace it with the appropriate dynamic content, which is produced by a PHP function. Using shortcodes is very easy. To use one, create a new post (or edit an existing one), switch the editor to HTML mode and type a shortcode in brackets, such as:

The thing to remember with shortcodes is that they’re very easy to create. If you know how to write a basic PHP function, then you already know how to create a WordPress shortcode. For our first one, let’s create the well-known “Hello, World” message.

  1. Open the functions.php file in your theme. If the file doesn’t exists, create it.
  2. First, we have to create a function to return the “Hello World” string. Paste this in your functions.php file:
    function hello() {
        return 'Hello, World!';
    }
  3. Now that we have a function, we have to turn it into a shortcode. Thanks to the add_shortcode() function, this is very easy to do. Paste this line after our hello() function, then save and close the functions.php file:
    add_shortcode('hw', 'hello');

    The first parameter is the shortcode name, and the second is the function to be called.

  4. Now that the shortcode is created, we can use it in blog posts and on pages. To use it, simply switch the editor to HTML mode and type the following:
    [hw]

    You’re done! Of course, this is a very basic shortcode, but it is a good example of how easy it is to create one.

Source: WordPress: Création de shortcode avancé

Contact wordpress designer for some expert guidance.

ServiceWordPress Maintenance & Support

Maintenance, Security & Support for your WordPress Site