Hide/replace menu item when user is logged in/out - php

I want to be able to hide or even replace the 'My account' button when users are logged out and I want to be able to hide or replace the 'Registration' button when users are logged in.
How would I go about doing this? I'm still a amateur at WordPress and I'm still learning, this is what I have so far in my nav-menus.php file.
if( is_user_logged_in() ) {
wp_nav_menu( array( 'My Account' => 'logged-users' ) );
} else {
wp_nav_menu( array( 'Registration' => 'not-logged-users' ) );
}
I know this isn't correct.

First, you need to create both menus, go to Appearance » Menus, create the two menus logged-in and logged-out.
After creating the menus, add this code in your theme’s functions.php file or a site-specific plugin:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged-in'; //This value stands for the actual name you give to the menu when you create it.
} else {
$args['menu'] = 'logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
}
That's all.

If you only need to hide them, then I would do this mainly via CSS. WP adds the class logged-in to the body element when there is a logged-in user, so you can use that to format elements inside of body differently.
Add classes to your menu items via the admin backend, like for example hide-when-logged-in and hide-when-not-logged-in.
Then you can use
body.logged-in .hide-when-logged-in,
body:not(logged-in) .hide-when-not-logged-in {
display: none;
}
in your stylesheet to hide those elements under the appropriate condition.

Related

WordPress - Dynamically add user ID to the end of a URL

I'm looking for a way to have the current logged in username added to the end of a link.
example: www.mywebsite.com/custom-post-type/author-post-title
"author-post-title" would be replaced with the current logged in username
This link will lead to a private post that is automatically created using Gravity Forms. The post title is the same as the logged-in person's username. I would like this link to be in place dynamically beforehand. Each author is limited to only one post and users already have to be logged in to see the link in nav menu.
I'm thinking there is a way to code this where the author name is a placeholder and is replaced by the currently logged in user ID, but I have not been able to figure it out. Something like... but this is way off as I am new to php.
<?php
function replace_text($text) {
$text = str_replace('author-post-title', '$user_id', $text);
$user_id = get_current_user_id();
return $text;
}
?>
I would recommend you to make generic page which has same URL for all users and modify it's content for specific user. It will be easier to attach this link to nav menu.
But, if you really need to create this kind of link you may create it using function:
function get_user_specific_url() {
if ( is_user_logged_in() ) {
return sprintf(
'%s/custom-post-type/%s',
get_site_url(),
wp_get_current_user()->user_nicename
);
}
// Redirect home in case if not logged in
return get_site_url();
}
Then you may want to create custom rewrite rule for this link structure to display specific page for user:
add_action( 'init', function() {
add_rewrite_tag("%user_nicename%", '([^/]*)');
add_rewrite_rule(
'^custom-post-type/([^/]*)/?',
'index.php?page_id=123&user_nicename=$matches[1]',
'top'
);
} );
You could try adding the following shortcode to your page:
[replace_link_w_user link="add your link here without username" button="Add button text here"]
Then add the following to your functions.php:
add_shortcode( 'replace_link_w_user', 'rl_w_user' );
function rl_w_user($atts = array()) {
$details = shortcode_atts( array(
"link"=>'',
"button_text"=>'Search'
), $atts );
if ( is_user_logged_in() ) {
$button='
<form action="'.$details['link'].'/'.wp_get_current_user()->user_nicename.'">
<input type="submit" value="'.$details['button_text'].'" />
</form>';
return $button;
} else {
return;
}
}

Adding a dynamic logo to Genesis primary nav

I would like to add a logo ( get_custom_logo() ) in the Genesis navigation bar but when it's blank, there's to be text. I've achieved this in a similar fashion for the header but cannot for the primary nav. The end result DOES work but when the user goes to the "customize" part of "site identity" the change is not visible, until you hit "publish" then refresh the actual page.
I need the change to be seen in "customize" mode as the logo is being changed. Here's the code the semi-functions...
add_filter( 'wp_nav_menu_items', 'prefix_add_menu_item', 10, 2 );
/**
* Add Menu Item to end of menu
*/
function prefix_add_menu_item ( $items, $args ) {
if( $args->theme_location == 'primary' ) {
if (( has_custom_logo())) {
$thelogo = get_custom_logo();
$items .= '<li class="menu-item">' . $thelogo . '</li>';
} else {
$items .= '<li class="menu-item">Theme Name</li>';
return $items;
}
return $items;
}
}
Ideally, I'd like to apply this method throughout the site for other hooks. If anyone can point to what I'm doing wrong, that would help.
Cheers.
It won't refresh in preview unless you add a custom setting to $wp_customize for your image or you add a new PHP class for the site logo. Either way, it's not an easy fix.

How can I edit this code so that it only changes the Top Menu bar on my homepage when a user is logged in without changing the Main Menu?

I am using OceanWP theme to create my site.
I have two different menus on my homepage, I have a top menu and I also have a main menu. http://prntscr.com/pofn5r
I would like for the top menu to display different options for users who are logged in and users who are logged out.
I have used the following code which I placed in the functions.php file. I have also created two different menus for logged in and logged out users:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged_in';
} else {
$args['menu'] = 'logged_out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
It seems to work in terms of showing different menus for users which are logged in and users which are logged out but the problem is that it also changes the main menu as well as the top menu.
prntscr.com/podv5e
I wanted the main menu to remain the same and just have the top menu change.
I was wandering whether there was a way I could adjust the code so that it would only apply to the top menu and not the main menu?
You could do something like:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
echo "Here you put HTML code when logged in";
} else {
echo "Here you put HTML code when logged out"
}
}
since echo will work like it is writted directly into HTML

Is there is a function that checks if a menu page is loaded in wordpress?

I created a menu to wordpress dashboard
add_menu_page( __('locations'), __('Locations'), 'manage_options', 'manage-locationss', 'locations_page');
function locations_page() {
require_once(locations.php);
}
I want to add a style sheet to this page only , Is there is a function that cheks if this page is opened then loads a style sheet , Like this function is_page_template().
Or I have to add the css using wp_enqueue_style() without any checks?
If i understand what you try to achieve, you need something like that
if(is_page( 'location' )){ // your name of your page
wp_enqueue_style() // do stuff with you right parameters and files
}
if(is_page( 'location' )){} // Checks if the Page with a post_title of "Location" is being displayed. In this way You can Check whatever page you need.
You can use WP Screen and get_current_screen():
$current_screen = get_current_screen();
if ( $current_screen->id === 'location' ) {
// Your code
}
https://codex.wordpress.org/Class_Reference/WP_Screen
https://codex.wordpress.org/Function_Reference/get_current_screen

Wordpress Main Menu - Hide 'Home' link on homepage only

I need to hide the "Home" link that's generated from Wordpress main menu on the homepage, and show it on the rest of the site.
I tried creating my own menu with no "Home" link and adding the "Home" link manually on the header.php file but it goes to the end of the menu and does not look like a pretty solution.
Any ideas? Using latest Wordpress 3.2
If you only want to hide it to the users, i suggest using the following CSS:
body.home a[title="Home"] {
display: none;
}
Explanation: Wordpress generates several classes for the body tag. The home class is used to hide all links with the title Home on the homepage.
Working Example (code taken from the default theme): http://jsfiddle.net/yJVyK/1/
Note: The attribute selector does not work in IE6
There is an another solution with PHP which more correct way in my opinion.
add_filter( 'wp_nav_menu_objects', 'amc_filter_menu', 10, 2 );
/**
* Filters to remove Home Link on Front Page
*/
function amc_filter_menu( $objects, $args ) {
// Return Default Value if the Menu isn't Main Menu
// Replace "Navigation_location" with your target location
if ( 'Navigation_location' !== $args->theme_location ) {
return $objects;
}
// Detect the Menu which equeal site URL
foreach ( $objects as $key => $object ) :
if ( get_site_url( null, '/' ) === $object->url && is_front_page() || get_site_url() === $object->url && is_front_page() ) :
unset( $objects[ $key ] );
endif;
endforeach;
// Return the menu objects
return $objects;
}
Source

Categories