Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Last active February 13, 2020 13:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thefuxia/4412306 to your computer and use it in GitHub Desktop.
Save thefuxia/4412306 to your computer and use it in GitHub Desktop.
T5 Nav Menu Log-in LinkAdds a log-in or log-out link to the first level of the first WordPress nav menu
<?php
/**
* Plugin Name: T5 Nav Menu Log-in Link
* Description: Adds a log-in or log-out link to the first level of the first nav menu
* Plugin URI: http://wordpress.stackexchange.com/q/77614
* Version: 2012.12.30
* Author: Fuxia Scholz
* Author URI: https://fuxia.me
* Licence: MIT
* License URI: http://opensource.org/licenses/MIT
*/
add_filter( 'wp_nav_menu_objects', 't5_menu_log_link', 100, 2 );
/**
* Add a link to the nav menu.
*
* @wp-hook wp_nav_menu_objects
* @param array $sorted_menu_items Existing nav menu items
* @param object $args Nav menu arguments. If $args->add_loginout is FALSE,
* the function does nothing.
* @return array Nav menu items
*/
function t5_menu_log_link( $sorted_menu_items, $args )
{
static $done = FALSE;
if ( ! isset ( $args->add_loginout ) && $done )
return $sorted_menu_items;
if ( isset ( $args->add_loginout ) && ! $args->add_loginout )
return $sorted_menu_items;
$done = TRUE;
$here = esc_url( home_url( $_SERVER['REQUEST_URI'] ) );
$link = new stdClass;
$link->menu_item_parent = 0;
$link->ID = '';
$link->db_id = '';
if ( is_user_logged_in() )
{
$link->url = wp_logout_url( $here );
$link->title = __( 'Log Out' );
}
else
{
$link->url = wp_login_url( $here );
$link->title = __( 'Log In' );
}
$sorted_menu_items[] = $link;
return $sorted_menu_items;
}
@boje
Copy link

boje commented Dec 30, 2012

Not working for me.
Fatal error: Cannot redeclare t6_menu_log_link() in /customers/d/b/b/musikfest.dk/httpd.www/kursus/wp-content/plugins/o_add_menu_item/o_add_men_item.php on line 24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment