Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Replace Navigation Menu Plugin WordPress

  • SOLVED

Hey guys,

I want to create a plugin to replace the theme default navigation menu.

When the plugin is activated the default menu of the theme is disabled and a new one is added.

This new menu have to use the wp_nav_menu() in order bo be able to be edited via "Menus" on "Appearance" tab.

I think it can be done using wp_nav_menu() and a walker class to set a new "class" and a new "id".

As I'm new on WP I don't know how to replace it.

If somebody could share the function to replace the theme menu and add the new one with a custom "class" and "id" would be great.

Regards

Answers (2)

2012-05-31

Arnav Joy answers:

please see this link may be it will help to you

http://wordpress.stackexchange.com/questions/14037/menu-items-description-custom-walker-for-wp-nav-menu/14039#14039


vtimbuc comments:

To do this I need to edit the theme files directly, which is not what I want.

I need a function to replace the theme menu and create a new one with wp_nav_menu() and a walker class.


Arnav Joy comments:

I have created a plugin which for this

and replace this function at the place where you are calling wp_nav_menu();

<?php
if(function_exists('wp_nav_menu_overrride'))
wp_nav_menu_overrride();
else
wp_nav_menu( array( 'theme_location' => 'primary' ) );
?>


Arnav Joy comments:

//this is the code for it create a file and store it in your plugins directory
<?php
/*
Plugin Name: WP nav menu override

Description: This plugin overrides default wp_nav_menu().
Version: 1.1
*/
class description_walker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

$class_names = $value = '';

$classes = empty( $item->classes ) ? array() : (array) $item->classes;

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="'. esc_attr( $class_names ) . '"';

$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';

$prepend = '<strong>';
$append = '</strong>';
$description = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';

if($depth != 0)
{
$description = $append = $prepend = "";
}

$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
$item_output .= $description.$args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;

$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}

function wp_nav_menu_overrride(){
wp_nav_menu( array(
'container' =>false,
'menu_class' => 'nav',
'echo' => true,
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => new description_walker())
);
}
?>


vtimbuc comments:

Thanks Arnav but I don't want to edit any template files. Is there a way to replace the menu only if the plugin is activated, without edit the theme template files? Like UberMenu?


Arnav Joy comments:

check this

<?php
/*
Plugin Name: WP nav menu override

Description: This plugin overrides default wp_nav_menu().
Version: 1.1
*/

class new_walker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

$class_names = $value = '';

$classes = empty( $item->classes ) ? array() : (array) $item->classes;

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="'. esc_attr( $class_names ) . '"';

$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';

$prepend = '<strong>';
$append = '</strong>';
$description = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';

if($depth != 0)
{
$description = $append = $prepend = "";
}

$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
$item_output .= $description.$args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;

$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}

add_filter( 'wp_nav_menu_args' , 'my_new_menu' );
function my_new_menu( $args ) {
$args['walker'] = new new_walker();
return $args;
}
?>


vtimbuc comments:

It does the trick. Thanks.

I don't know how it functions but I've already voted for the award prize.

2012-05-31

Jatin Soni answers:

At the location of your menu code in the template file write something like this

if (function_exists("your plugin function")){
pluginfunction();
} else {

wp_nav_menu( array( 'theme_location' => 'yourmenu' ) );

}


you need to find your plugin function. usually to add navmenu plugin there must be code something like this <?php if (function_exists(navmenu)) { navmenu(); } ?> so same code you can use here and in else condition you can add your default theme menu.


vtimbuc comments:

Thanks Jatin but I don't want to edit any template files. Is there a way to replace the menu only if the plugin is activated, without edit the theme template files? Like UberMenu?


Jatin Soni comments:

I found this try may be it works for you. Let me know if it works or not, than will see more if needed.

http://aaron.jorb.in/blog/2010/06/how-to-remove-a-nav-menu-location-in-wordpress-3-0-0/#.T8dgM8WWmjQ


vtimbuc comments:

It does not work because I have to insert a location name, but every theme uses different location names so it does not work.


Jatin Soni comments:

Than you might need to work with remove_action();

try this function may be works
Codex:
http://codex.wordpress.org/Function_Reference/remove_action

Thesis theme example:
http://diythemes.com/thesis/rtfm/move-replace-nav-menu/