Skip to content

Instantly share code, notes, and snippets.

@bryanwillis
Last active August 29, 2015 14:04
Show Gist options
  • Save bryanwillis/94cd5e9757f76299a7c9 to your computer and use it in GitHub Desktop.
Save bryanwillis/94cd5e9757f76299a7c9 to your computer and use it in GitHub Desktop.
Add shortcodes and html to description field to override menu
<?php
/**
* Nav Menu Shortcodes
*
* @package Nav Menu Shortcodes
* @author Bryan Willis <[email protected]>
* @license GPL-2.0+
* @link http://www.canddibusiness.com
*
* @wordpress-plugin
* Plugin Name: Nav Menu Shortcodes
* Plugin URI: @TODO
* Description: Adds options to add html and shortcodes in nav menu fields. The description field will be used as an override field for any html or shortcodes. Also adds Permalink shortcode plugin, and widgets shortcode to add shortcodes as widgets.
* Version: 1.0.0
* Author: Bryan Willis
* Author URI: http://profiles.wordpress.org/codecandid
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* GitHub Plugin URI: https://gist.github.com/bryanwillis/69af4952f85287cce322/
*/
// Probably not useful
//add_filter('wp_nav_menu', 'do_shortcode', 11);
/*-----------------------------------------------------------------------------------*/
/* HTML in Menu Descriptions */
/*-----------------------------------------------------------------------------------*/
//*
remove_filter('nav_menu_description', 'wptexturize');
remove_filter('nav_menu_description', 'strip_tags');
add_filter( 'wp_setup_nav_menu_item', 'html_nav_menu_description' );
function html_nav_menu_description($menu_item) {
// $menu_item->description = apply_filters( 'nav_menu_description', $menu_item->description );
// NOTICE - Undefined property: stdClass::$post_content
$menu_item->description = apply_filters( 'nav_menu_description', $menu_item->post_content );
return $menu_item;
}
// */
/*-----------------------------------------------------------------------------------*/
/* Menu Description Shortcode */
/*-----------------------------------------------------------------------------------*/
//*
// Since HTML in Menu Descriptions shortcodes produce PHP Notice this might be a better temporary solution
Plugin Name: Shortcodes in Menus
Description: Allows you to add shortcodes in WordPress Navigation Menus
Plugin URI: http://wordpress.org/plugins/shortcode-in-menus/
Version: 1.0
Author URI: http://gagan.pro
Author: Gagan Deep Singh
https://wordpress.org/plugins/shortcode-in-menus/
if( !function_exists('shortcode_my_menu_description' ) {
function shortcode_my_menu_description( $item_output, $item ) {
if ( !empty($item->description)) {
$output = do_shortcode($item->description);
if ( $output != $item->description )
$item_output = $output;
}
return $item_output;
}
add_filter("walker_nav_menu_start_el", "shortcode_my_menu_description" , 10 , 2);
}
// */
/*-----------------------------------------------------------------------------------*/
/* Sidebar Widget */
/*-----------------------------------------------------------------------------------*/
// [widget name="Kalender" instance="title=Hello,World!"]
// [widget classname="Calendar" instance="title=Hello,World!"]
// [sidebar name="Allgemeine Sidebar"]
if (! class_exists( 'widget_or_sidebar_per_shortcode' ) ) {
class widget_or_sidebar_per_shortcode {
function __construct() {
add_shortcode( 'widget', array( 'widget_or_sidebar_per_shortcode', 'WidgetShortcode' ) );
add_shortcode( 'sidebar', array( 'widget_or_sidebar_per_shortcode', 'SidebarShortcode' ) );
}
static public function WidgetShortcode( array $atts ) {
global $wp_registered_widgets;
$back = '';
$widget_class = '';
extract( shortcode_atts( array( 'name' => '', 'instance' => '', 'classname' => '' ), $atts ) );
$instance = html_entity_decode( $instance );
if ( $name != '' ) {
foreach ( $wp_registered_widgets as $widget ) {
if ( $widget[ 'name' ] == $name ) {
$widget_class = get_class( $widget[ 'callback' ][ 0 ] );
continue;
}
}
} elseif ( $classname != '' ) {
foreach ( $wp_registered_widgets as $widget ) {
if ( get_class( $widget[ 'callback' ][ 0 ] ) == $classname ) {
$widget_class = $classname;
continue;
}
}
}
if ( $widget_class != '' ) {
$back = "<div id='" . str_replace( " ", "_", $name ) . "' class='widget_shortcode'>";
ob_start();
the_widget( $widget_class, $instance );
$back .= ob_get_contents();
ob_end_clean();
$back .= "</div>";
}
return $back;
}
static public function SidebarShortcode( array $atts ) {
extract( shortcode_atts( array( 'name' => '1' ), $atts ) );
$back = "<div id='" . str_replace( " ", "_", $name ) . "' class='sidebar_shortcode'>";
ob_start();
if ( ! function_exists( 'dynamic_sidebar' ) || ! dynamic_sidebar( $name ) ) {}
$back .= ob_get_contents();
ob_end_clean();
$back .= "</div>";
return $back;
}
}
$widget_or_sidebar_per_shortcode = new widget_or_sidebar_per_shortcode();
}
// */
/*-----------------------------------------------------------------------------------*/
/* Search Shortcode for Description Field */
/*-----------------------------------------------------------------------------------*/
// [search]
add_shortcode('search', 'get_search_form');
/*-----------------------------------------------------------------------------------*/
/* Additional Shortcodes */
/*-----------------------------------------------------------------------------------*/
/**
* @plugin 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
*/
// [admin_url]
function admin_url_shortcode() {
$url = admin_url();
return $url;
}
add_shortcode('admin_url', 'admin_url_shortcode');
// [home_url]
function home_url_shortcode() {
$url = home_url();
return $url;
}
add_shortcode('home_url', 'home_url_shortcode');
// [site_url]
function site_url_shortcode() {
$url = site_url();
return $url;
}
add_shortcode('site_url', 'site_url_shortcode');
// [wp_logout text="Please Log Out"]
function logout_func($atts, $content = null) {
extract(shortcode_atts(array(
'text' => 'Log Out',
'class' => 'logoutlink-text',
), $atts));
$logoutlink = wp_logout_url( home_url() );
return '<a href="'.$logoutlink.'" class="'.$class.'" title="Logout">'.$text.'</a>';
}
add_shortcode ('wp_logout', 'logout_func');
// [logout_to_home]
function logout_url_func() {
return wp_logout_url( home_url() );
}
add_shortcode ('logout_to_home', 'logout_url_func');
/*-----------------------------------------------------------------------------------*/
/* Add A Menu With A Shortcode */
/*-----------------------------------------------------------------------------------*/
/*
[menu_shortcode menu="menu name goes here" menu_id="menu id goes here"]
[menu_shortcode menu="quick footer" menu_id="footer_menu"]
*/
/*
'container_class' => 'menu container classname',
'container_id' => menu container id',
'menu_class' => 'menu class',
'menu_id' => 'menu id',
'before' => 'before menu html',
'after' => 'after menu html',
'link_before' => 'before link title',
'link_after' => 'after link title'
*/
function list_menu_add_shortcode($atts, $content = null) {
extract(shortcode_atts(array(
'menu' => '',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => '',
'theme_location' => ''),
$atts));
return wp_nav_menu( array(
'menu' => $menu,
'container' => $container,
'container_class' => $container_class,
'container_id' => $container_id,
'menu_class' => $menu_class,
'menu_id' => $menu_id,
'echo' => false,
'fallback_cb' => $fallback_cb,
'before' => $before,
'after' => $after,
'link_before' => $link_before,
'link_after' => $link_after,
'depth' => $depth,
'walker' => $walker,
'theme_location' => $theme_location));
}
add_shortcode("menu_shortcode", "list_menu_add_shortcode");
<?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