Last active
August 29, 2015 14:04
-
-
Save bryanwillis/94cd5e9757f76299a7c9 to your computer and use it in GitHub Desktop.
Add shortcodes and html to description field to override menu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*-----------------------------------------------------------------------------------*/ | |
/* Shortcodes in Link - Allow Shortcode in URLS (note that clean_url is deprecated ) */ | |
/*-----------------------------------------------------------------------------------*/ | |
/** | |
* @plugins Shortcodes in Menus | |
* @description Allows you to add shortcodes in WordPress Navigation Menus | |
* @version 1.0 | |
* @author Gagan Deep Singh | |
* @support http://wordpress.org/support/plugin/shortcode-in-menus | |
*/ | |
//* | |
if( !function_exists('gs_sim_saving_shortcode_custom_links' ) { | |
function gs_sim_saving_shortcode_custom_links( $url, $orig_url, $context ){ | |
if($context=='db'){ | |
return $orig_url; | |
} | |
return $url; | |
} | |
} | |
if( !function_exists('gs_sim_security_check' ) { | |
function gs_sim_security_check(){ | |
// $role = get_role('editor'); | |
// $role->add_cap('edit_theme_options'); | |
if(current_user_can('edit_theme_options')){ | |
add_filter( 'esc_url', 'gs_sim_saving_shortcode_custom_links', 99, 3 ); | |
} | |
} | |
add_action('wp_loaded','gs_sim_security_check'); | |
} | |
if( !function_exists('gs_sim_allow_display_shortcode_custom_links' ) { | |
function gs_sim_allow_display_shortcode_custom_links( $url, $orig_url, $context ){ | |
if( $context=='display' ){ | |
return do_shortcode($orig_url); | |
} | |
return $url; | |
} | |
add_filter( 'esc_url', 'gs_sim_allow_display_shortcode_custom_links', 1, 3 ); | |
} | |
// */ | |
/*-----------------------------------------------------------------------------------*/ | |
/* URL SHORTCODES for NAV-MENU url field */ | |
/*-----------------------------------------------------------------------------------*/ | |
/** | |
* @plugins Permalink Shortcode | |
* @description Provides a shortcode that allows you to insert permalinks into your content. | |
* @version 1.0.0 | |
* @author Ryan Lange | |
* @support http://wordpress.org/support/plugin/permalink-shortcode | |
*/ | |
/* | |
* URLS = wpid, query, fragment, slug, title | |
* | |
* [permalink wpid='22'] | |
* [permalink title='About'] | |
* [permalink slug='About'] | |
* | |
* Attributes: | |
* | |
* accesskey | |
* charset | |
* class | |
* dir | |
* hreflang | |
* id | |
* lang | |
* media | |
* rel | |
* rev | |
* style | |
* target | |
* title | |
* type | |
* | |
* Enclosing: | |
* | |
* [permalink wpid="123"]link text[/permalink] | |
* | |
* <a href="https://example.com/foo/bar/[permalink wpid='123' fragment='comments']">link text</a> | |
* | |
* [permalink wpid="123" class="my-class" title="Some Other Page"]link text[/permalink] | |
*/ | |
if( !class_exists( 'PermalinkShortcode' ) ) { | |
final class PermalinkShortcode { | |
private $defaults = array( | |
'link' => array( | |
'wpid' => 0, | |
'query' => null, | |
'fragment' => null | |
), | |
'html' => array( | |
'accesskey' => null, | |
'charset' => null, | |
'class' => null, | |
'dir' => null, | |
'hreflang' => null, | |
'id' => null, | |
'lang' => null, | |
'media' => null, | |
'rel' => null, | |
'rev' => null, | |
'style' => null, | |
'target' => null, | |
'title' => null, | |
'type' => null | |
) | |
); | |
private static $instance = null; | |
private function __construct() | |
{ | |
add_shortcode( 'permalink', array( $this, 'doShortcode' ) ); | |
} | |
public function doShortcode( $attributes, $content = null ) | |
{ | |
$link_attributes = shortcode_atts( $this->defaults['link'], $attributes ); | |
if( empty( $content ) ) { | |
$output = $this->getUri( $link_attributes ); | |
} else { | |
$output = '<a href="' . $this->getUri( $link_attributes ) . '"'; | |
foreach( shortcode_atts( $this->defaults['html'], $attributes ) as $attribute => $value ) { | |
if( 'class' == $attribute ) { | |
$value = trim( "permalink-shortcode {$value}" ); | |
} elseif( ( 'title' == $attribute ) && ( null === $value ) ) { | |
$value = get_the_title( $link_attributes['wpid'] ); | |
} | |
if( null !== $value ) { | |
$output .= ' ' . $attribute . '="' . esc_attr( $value ) . '"'; | |
} | |
} | |
$output .= '>' . do_shortcode( $content ) . '</a>'; | |
} | |
return $output; | |
} | |
private function getUri( array $link_attributes ) | |
{ | |
$permalink = get_permalink( $link_attributes['wpid'] ); | |
$query = $link_attributes['query']; | |
if( ( null !== $query ) && ( '?' !== substr( $query, 0, 1 ) ) ) { | |
$query = "?{$query}"; | |
} | |
$fragment = $link_attributes['fragment']; | |
if( ( null !== $fragment ) && ( '#' !== substr( $fragment, 0, 1 ) ) ) { | |
$fragment = "#{$fragment}"; | |
} | |
return $permalink . $query . $fragment; | |
} | |
public static function init() | |
{ | |
if( null === self::$instance ) { | |
self::$instance = new self(); | |
} | |
} | |
private function __clone() {} | |
} | |
PermalinkShortcode::init(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment