Skip to content

Instantly share code, notes, and snippets.

@EricBusch
Created March 3, 2013 17:39
Show Gist options
  • Save EricBusch/5077151 to your computer and use it in GitHub Desktop.
Save EricBusch/5077151 to your computer and use it in GitHub Desktop.
Create your own WordPress Permalink shortcode.
<?php
/**
* Creates a shortcode that links to other
* content on your site.
*/
function my_permalink_shortcode($atts) {
// Check that $id exists.
$id = intval($atts['id']);
if ($id <= 0) { return; }
// Check that $id has a URL.
$url = get_permalink($id);
if ($url == '') { return; }
// Get link option and title.
$link = ($atts['link'] == '1') ? true : false;
$title = (trim($atts['title']) == '') ? get_the_title($id) : $atts['title'];
// Determine if we create a link.
if ($link) {
return '<a href="'.$url.'">'.$title.'</a>';
} else {
return $url;
}
}
add_shortcode('pl', 'my_permalink_shortcode');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment