How To Create Permalink Shortcode in WordPress

When you are building a theme or plugin, and the circumstance comes up where you need to create a link to a specific page hard-baked right into the theme, there is a function you should be using.

Wordpress Permalink Shortcode

With the WordPress permalinks shortcode the generated URL will be updated automatically, when original permalinks on a page or post change.
function do_permalink($atts) {
extract(shortcode_atts(array(
'id' => 1,
'text' => "" // default value if none supplied
), $atts));

if ($text) {
$url = get_permalink($id);
return "$text";
} 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.
This shortcode can be used in two ways:

Basic Usage

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

0SHARES
0
0

Comments