Adding the Permalinks Shortcode to WordPress

WordPress is made up of ThemesPlugins, Widgets, Shortcodes, PHP and GPL Licenses.

Did you know that, beside Widgets, another useful WordPress API is shortcode,

Now I will show you how to add a permalinks shortcode into our WordPress site.

Why is this shortcode useful?

With the permalinks shortcode the generated URL will be updated automatically, when original permalinks on a page or post change.

How-To-Added-Permalinks-Shortcode-460x350

function do_permalink($atts) {
	extract(shortcode_atts(array(
		'id' => 1,
		'text' => ""  // default value if none supplied
    ), $atts));

    if ($text) {
        $url = get_permalink($id);
        return "<a href='$url'>$text</a>";
    } else {
	   return get_permalink($id);
	}
}
add_shortcode('permalink', 'do_permalink');

This code below using get_permalink() functions, it’s standard WordPress functions and created shortcode version with code above.

How to use permalinks shortcode? Follow this tutorial

<a href="[permalink id=49]">Basic Usage</a>

Or you can use this method too

[permalink id=49 text='providing text']

Thanks to DigWP for sharing this code, and I hope this tutorial to add a permalinks shortcode is useful for your blog and makes it easy to add in links between your content (post or pages).

Please note, this is only a sample and we can make a lots with the WordPress shortcode. And we can make a much more complicated shortcode if we need it.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>