]*[^>]*>)(.*)(<\/a>)/siU', '$1$2 ' . $item->description . '$3', $item_output );
if ( $depth == 0 ) $menus_with_desc[$args->theme_location] = 1;
}
}
return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'graphene_walker_nav_menu_start_el', 10, 4 );
/**
* Add custom class to menu wrapper for menu with descriptions
*/
function graphene_menu_class( $nav_menu, $args ) {
global $menus_with_desc;
if ( $menus_with_desc && array_key_exists( $args->theme_location, $menus_with_desc ) ) {
$nav_menu = str_replace( $args->menu_class, $args->menu_class . ' has-desc', $nav_menu );
}
return $nav_menu;
}
add_filter( 'wp_nav_menu', 'graphene_menu_class', 10, 2 );
/**
* Add custom fields to custom menu
*/
class graphene_Menu_Item_Custom_Fields {
protected static $fields = array();
public static function init() {
add_action( 'wp_nav_menu_item_custom_fields', array( __CLASS__, '_fields' ), 10, 4 );
add_action( 'wp_update_nav_menu_item', array( __CLASS__, '_save' ), 10, 3 );
add_filter( 'manage_nav-menus_columns', array( __CLASS__, '_columns' ), 99 );
self::$fields = array(
'icon' => __( 'FontAwesome Icon Name', 'graphene' ),
);
}
/**
* Save custom field value
*/
public static function _save( $menu_id, $menu_item_db_id, $menu_item_args ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) return;
check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
foreach ( self::$fields as $_key => $label ) {
$key = sprintf( 'menu-item-%s', $_key );
// Sanitize
if ( ! empty( $_POST[ $key ][ $menu_item_db_id ] ) ) {
$value = sanitize_text_field( trim( $_POST[ $key ][ $menu_item_db_id ] ) );
} else {
$value = null;
}
// Update
if ( ! is_null( $value ) ) update_post_meta( $menu_item_db_id, $key, $value );
else delete_post_meta( $menu_item_db_id, $key );
}
}
/**
* Print field
*/
public static function _fields( $id, $item, $depth, $args ) {
foreach ( self::$fields as $_key => $label ) :
$key = sprintf( 'menu-item-%s', $_key );
$id = sprintf( 'edit-%s-%s', $key, $item->ID );
$name = sprintf( '%s[%s]', $key, $item->ID );
$value = get_post_meta( $item->ID, $key, true );
$class = sprintf( 'field-%s', $_key );
?>
%2$s
'
. sprintf( __( 'Choose from 580+ %s', 'graphene' ), '' . __( 'available icons', 'graphene' ) . '.' ),
esc_attr( $id ),
esc_html( $label ),
esc_attr( $name ),
esc_attr( $value )
) ?>
'menu_order, post_title',
'menu_class' => 'menu',
'echo' => true,
'link_before' => '',
'link_after' => ''
);
$args = apply_filters( 'wp_page_menu_args', wp_parse_args( $args, $defaults ) );
$menu = '';
$list_args = $args;
/* Add Home link to the menu */
$text = __( 'Home', 'graphene' );
$class = '';
if ( is_front_page() && !is_paged() ) $class = 'class="current_page_item"';
$menu .= '' . $args['link_before'] . $text . $args['link_after'] . '';
/* If the front page is a page, add it to the exclude list */
if ( get_option( 'show_on_front' ) == 'page' ) {
if ( ! empty( $list_args['exclude'] ) ) $list_args['exclude'] .= ',';
else $list_args['exclude'] = '';
$list_args['exclude'] .= get_option( 'page_on_front' );
}
$list_args['echo'] = false;
$list_args['title_li'] = '';
$menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages( $list_args ) );
if ( $menu ) $menu = '';
$menu = apply_filters( 'wp_page_menu', $menu, $args );
if ( $args['echo'] ) echo $menu;
else return $menu;
}
/**
* Customise default navigation menu
*
* Improvements over the default Walker_Page class:
* 1. Changed all classes to match Walker_Nav_Menu
* 2. Added link attributes filter
* 3. Added item output filter
*/
class Graphene_Walker_Page extends Walker_Page {
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent