WordPress shortcode – How to create shortcode in WordPress

WordPress shortcode – How to create shortcode in WordPress

WordPress provides good functionality WordPress shortcode. Using the shortcode we can easily add dynamic code into WP posts, pages and widgets. Today, We are going to learn how to use the shortcode and create WordPress shortcode. Also, We are going to see how to execute shortcode using WordPress do_shortcode.

First of all, We are going to create a shortcode. After that, I will tell you how to use the WordPress shortcode. However, Many themes and plugin provide the shortcode snippets functionality for Galler, HTML sections, Contact form, Blog listing etc…

WordPress shortcode

WordPress introduced the shortcode from 2.5 version. Shortcode functionality allows a developer to create dynamic code then register it as a shortcode. After that, User can easily access that functionality using a shortcode like [tryvary_shortcode].

WP find the shortcode and replace it with the actual function output. The shortcode is very useful functionality for the developer to add dynamic content in WP.

First of all, We are going to learn how to create a function and register it as a shortcode in WP.

How to create WordPress shortcode

First of all, We are going to create a WP shortcode. We are going to create a PHP function After that, We register it as a WP shortcode.

Just open the functions.php file from the currently active theme directory. Then just paste the below code.

function tryvary_shortcode_function( $atts, $content = "" ) {

    // Attributes
    $atts = shortcode_atts(
        array(
            'name' => 'Renish Khunt',
            'age' => '27',
        ),
        $atts
    );

    return $content." Hi there!, My Name is ".$atts['name']." and I am ".$atts['age']." years old.";

}

Now, Let’s register this function as a WP shortcode by following below line.

add_shortcode( 'tryvary_shortcode', 'tryvary_shortcode_function' );

Finally, Our shortcode is ready to use. Let’s see how we use the WordPress shortcode.

[tryvary_shortcode name="Renish Khunt" age="27"]
This is the first line content. Display it as a content in shortcode.
[/tryvary_shortcode]

Just add this code line into WordPress any posts, pages or widget then check WordPress convert it into output.

By Default, Shortcode doesn’t work directly in WP widget. You can enable shortcode in the widget by following this tutorial.

WordPress do_shortcode

We can execute the WP shortcode by PHP code using the WordPress do_shortcode function. Let’s see how we can execute the shortcode using PHP code.

print do_shortcode('[tryvary_shortcode name="Renish Khunt" age="27"]
This is the first line content. Display it as a content in shortcode.
[/tryvary_shortcode]');

In conclusion, Today, We learn about the create a WP shortcode creation and use it in WP. I hope you learn everything regarding the shortcode. If you have any question please write the comment. We are always ready to help you.

Related Posts

  1. Email unique validation in Laravel ignore id on update
  2. WordPress Button Shortcode – Add button to WordPress editor
  3. WordPress change URL in database using MySQL Query
  4. WordPress jQuery is Not Defined or undefined
  5. PHP remove HTML tags from string using strip_tags
  6. Enable shortcodes in widget WordPress
  7. PHP remove non-empty directory programmatically
  8. PHP File upload step by step with example
  9. How to use jQuery noConflict mode in WordPress
  10. Laravel 6 integrate AdminLte admin theme jeroennoten

1 Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search Posts

Subscribe To Newsletter

Get Early Access To New Articles, Plugins, Discount Codes And Quickly Brief Updates about tips and tricks

Join 308 other subscribers