Last active
November 6, 2022 11:05
-
-
Save EricBusch/5074078 to your computer and use it in GitHub Desktop.
Adding items to a WordPress menu dynamically. http://www.datafeedr.com/?p=1639
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adds a link to the Primary Menu based on the | |
* user's logged in status. This is to be placed | |
* in your theme's functions.php file or in your | |
* own plugin file. | |
* | |
* This example works for the WordPress Twenty- | |
* Twelve theme. | |
*/ | |
function twentytwelve_wp_nav_menu_items($items, $args) { | |
// Make sure this is the Primary Menu. | |
// You may need to modify this condition | |
// depending on your theme. | |
if ($args->theme_location == 'primary') { | |
// CSS class to use for <li> item. | |
$class = 'menu-item'; | |
if (is_user_logged_in()) { | |
// User is logged in, link to welcome page. | |
$extra = ' | |
<li id="menu-item-logged-in-user" class="'.$class.'"> | |
<a href="/welcome"> | |
'.__('Welcome').', '.wp_get_current_user()->user_login.'! | |
</a> | |
</li> | |
'; | |
} else { | |
// User is guest, link to login page. | |
$extra = ' | |
<li id="menu-item-logged-out-user" class="'.$class.'"> | |
<a href="/wp-login.php"> | |
'.__('Log in').' | |
</a> | |
</li> | |
'; | |
} | |
// Add extra link to existing menu. | |
$items = $items . $extra; | |
} | |
// Return menu items. | |
return $items; | |
} | |
// Hook into wp_nav_menu_items. | |
add_filter( 'wp_nav_menu_items', 'twentytwelve_wp_nav_menu_items', 10, 2 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment