0

can anyone give me some idea about how I can use the value of wp_logout_url() within the menu creator page of wordpress? As the menu creation page doesn't support php I cannot just put echo wp_logout_url( home_url() ) under custom link.

Also I cannot use functions like

add_filter( 'wp_nav_menu_items', 'wti_loginout_menu_link', 10, 2 );

function wti_loginout_menu_link( $items, $args ) {
   if ($args->theme_location == 'primary') {
      if (is_user_logged_in()) {
         $items .= '<li class="right"><a href="'. wp_logout_url() .'">Log Out</a></li>';
      } else {
         $items .= '<li class="right"><a href="'. wp_login_url(get_permalink()) .'">Log In</a></li>';
      }
   }
   return $items;
} 

because it doesn't fit my need, I have to add the logout link as a dropdown menu not as parent menu and also will show up in certain pages only. I can hel these things if anyone can show me the way how to fetch the wp_logout_url() data within the wp menu creation page.

Any help is highly appreciated.

  • But the link is saying your code should work. :( – Mayeenul Islam Oct 19 '15 at 12:57
  • No the link is showing how to add it as the top menu, but not as sub menu or sub menu of sub menu of sub menu. Thinks can be a lot complecated. Thats why I'm looking for a way to fetch the url directly into the menu editor page so that I can create a menu normally from there, without going through all these hassel. – iSaumya Oct 19 '15 at 13:02
4

Add submenu with a custom link, /wp-login?action=logout, like the image below:

enter image description here

Add code like below in functions.php:

function wpa_remove_menu_item( $items, $menu, $args ) {
    if ( is_admin() || ! is_user_logged_in() ) 
        return $items;
    foreach ( $items as $key => $item ) {
        if ( 'Login / Register' == $item->title ) 
            unset( $items[$key] );
        if ( 'Logout' == $item->title ) {
            $items[$key]->url = wp_logout_url();
        }
    }
    return $items;
}
add_filter( 'wp_get_nav_menu_items', 'wpa_remove_menu_item', 10, 3 );
  • Hi Jas, I think I got an better way to do it, which I'm testing now, will post here in a bit. Thanks for your help though:) – iSaumya Oct 19 '15 at 14:03
0

Here is what I did to get rid of this issue and I think while solving this issue, I got an idea to fix all similar issues.

I downloaded an amazing plugin called Shortcodes in Menus.

Then in my functions.php file, I created a small shortcode to get rid of this issue:

/* Shortcode to fetch the logout URL */
add_shortcode( 'fetch_logout_url', function() {
    return wp_logout_url( home_url() );
} );

Voila! Problem solved, and crazy simple to do.

  • 1
    Hi iSaumya, thats also interesting approach but this is also adding a code (even more code than mentioned in my answer :) ) to run shortcodes in menus via the plugin you used. However, its great you found your solution :) – jas Oct 19 '15 at 14:20
  • Yah, but look at the positive side of it, now you can do anything you want. This thing is not just stick for logout url. You can literally do anything you want. And trust me thought I'm completely against adding bunch of plugins into a site, but I think this plugin is worth having mate :) – iSaumya Oct 19 '15 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.