WordPress shortcodes for posts & widgets

Hello!

This is a nice short code snippet/tutorial on using shortcodes in wordpress widget areas and for the like, in posts.
One thing I have always found handy is to have two shortcodes that don’t need to be updated when you move a site from local to production or from a live server to your own or local.

So first things first, open up your functions.php file of your theme and simply add this:

function base_url_func( $atts ){
    $url = get_bloginfo('url') . '/';
    return $url;
}
add_shortcode( 'base_url', 'base_url_func' );

function template_url_func( $atts ){
    $url = get_bloginfo('template_url') . '/';
    return $url;
}
add_shortcode( 'template_url', 'template_url_func' );

This now allows for you to use the base and template url and always reference properly e.g. localhost/sitename or www.sitename.com
In posts all you would need to use is:

https://www.paulmarkquinn.co.uk/ or https://www.paulmarkquinn.co.uk:443/wp-content/themes/quinn/

but wait, didn’t I say for widgets too? of course I did! Well you may already be aware of this but for those who do not know, you can tell wordpress to accept shortcodes in widgets. Again in your functions.php file add this:

// shortcodes in widgets ////////////////
add_filter('widget_text', 'do_shortcode');

Well that should hopefully help you on your way.
Have a nice day.




Random Posts