All about the Shortcodes in WordPress

Untitled design 36

WordPress Shortcodes are powerful and special tags that save users from writing a code and still add various functionalities to their content. It adds elements like social sharing, pricing grid, call to action, event calendar, etc. into the user’s WordPress site. A lot of themes and plugins use Shortcodes to add those extra elements to their framework. In this blog post, we share some Shortcodes tricks that will make your WordPress lives simpler and more efficient.

shortcodes

Find the Shortcode in Your WordPress Theme

To begin playing with Shortcodes, you need to first locate them. To do so, open the theme folder of your WordPress site. You can find the folder wp-content>themes>your-theme-name. Dig in the functions.php file or the folder included within your site. Now, when you found the right file, just open it and search for the term add_shortcode.

function my_shortcode_function() {
$i = '<p>Hello World!</p>';
return $i;
}
add_shortcode('my shortcode, 'my_shortcode_function');

This code creates a shortcode ‘my-shortcode’, which returns a simple text greeting and can be embedded into a WordPress post or page like this:

[my-shortcode]

The right use of Shortcodes

Shortcodes are amazing but looking toward adding more and more Shortcodes to all your posts will only tie you up with that specific theme that provides for Shortcodes. To avoid using Shortcodes, you might not mind actually hiring a developer or just using a plugin meant to serve this function. The major drawback of using too many Shortcodes in all your posts is that you will have to manually edit the posts to remove all of them.

Future Proof Your Shortcodes

A theme provides for the usage of Shortcodes and you want to employ it completely to your advantage; that is a great thought. But think twice. We are telling you so because whenever you plan to use a different theme, you might not be offered the same Shortcode. You can deal with this fix by simply adding a site-specific plugin. Just copy the shortcode snippet from your theme’s functions.php file and paste it into your site-specific plugin.

Using Shortcodes in Widgets

Shortcodes are not applicable for extending functionalities to posts and pages, but you can also use them for your WordPress text widgets. Just drag and drop a text widget to your sidebar and add your shortcode inside it. This is not a default WordPress feature, so if you find to see your shortcode in a widget, just add this code to your theme’s functions.php file or a site-specific plugin.[code]add_filter(‘widget_text’, ‘do_shortcode’);[/code]

Add Shortcode in Theme Files

If you want to output the shortcode inside a non-widget area of your theme, simply add your shortcode, like this:

Let’s assume you have created a custom page template, and you want to include a shortcode to display a contact form. Simply add your shortcode, like this:

<?php PHPho do_shortcode("[example_shortcode]"); ?>

Hiding a Broken Shortcode

Often users find that the old shortcodes in their themes are not working anymore and notice odd text there. They can manually remove the shortcode from every post individually by finding all posts that are using that shortcode.[code]function wpb_find_shortcode($atts, $content=null) {
ob_start();
extract( shortcode_atts( array(
‘find’ => ”,
), $atts ) );

$string = $atts[‘find’];

$args = array(
‘s’ => $string,
);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
echo ‘

    ‘;

 

    while ( $the_query->have_posts() ) {

 

    $the_query->the_post(); ?>

}
echo ‘

‘;
} else {
echo “Sorry no posts found”;
}

wp_reset_postdata();
return ob_get_clean();
}
add_shortcode(‘shortcodefinder’, ‘wpb_find_shortcode’); [/code]Also, you can simply choose to hide the broken shortcode.

You can also go through the shortcode reference plugin to find a list and details about available shortcodes for your WordPress site.

Facebook
Twitter
LinkedIn
Pinterest

Newsletter

Get tips, product updates, and discounts straight to your inbox.

Hidden

Name
Privacy(Required)
This field is for validation purposes and should be left unchanged.

Leave a Reply

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

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