Can I change a menu after I logged in, in Wordpress? - php

I've got this wordpress site. And i've made two menu's. One which is supposed to be shown when a user isn't logged in, and one for when a user is logged in. And i'm talking about a login to the website it self, not the dashboard.
How do I make the menu's change, from the one I have where I am not logged in, to the one where I am?
I've tried adding the following code to the functions.php page, but it isn't working.
function my_wp_nav_menu_args( $args = '' ) {
if(session_status() != PHP_SESSION_NONE ) {
$args['menu'] = 'logged-in';
} else {
$args['menu'] = 'logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
I'm not sure where to look next, and I can't find any solutions on google. Hope someone can help me out!
I've also made sure i got the session_start(); at the top of every .php page.

You can make 2 different menus and use a simple if else :
<?php
if ( is_user_logged_in() ) {
wp_nav_menu( array( 'theme_location' => 'registered-menu' ) );
} else {
wp_nav_menu( array( 'theme_location' => 'visitor-menu' ) );
}
?>

Related

WORDPRESS astra theme Different header menu for log in and log out user

the question is just on the title, but well here's the problem i tried this code on my function.php which at first works fine, but suddenly i realized that the menus on footer changed to be the same as the header menus.
here is the code :
function wpc_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged-out';
} else {
$args['menu'] = 'Main Menu';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'wpc_wp_nav_menu_args' );
What i want is to have a different header menus for user when they log in and log out, but the footer menus stays the same. i am currently learning how to add the conditional logic on the functions.php atm, but if there are other ways please let me know, thx in advance
Probably a bit late for a reply but I just did this in the Astra theme today, so thought I would share the know-how for others who might need it. It can easily be adjusted for any WordPress theme actually:
You can check the location of the menu and only change the menu in the location(s) you decide (in this example 'primary' and 'mobile_menu' which is what Astra theme calls the the main nav locations for desktop and mobile) :
function custom_wp_nav_menu_args( $args = '' ) {
if( $args['theme_location'] == 'primary' || $args['theme_location'] == 'mobile_menu' ) {
if( ! is_user_logged_in() ) {
$args['menu'] = 'logged-out';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_wp_nav_menu_args' );
If you dont want to use the explicit position of the menu (in my case the nav was in a widget in the footer and not a set theme location), you can also do the same thing by using the id, name or slug of the menu. For example if you want to change the menu 'test-menu' for 'test-menu-logged-out' if the user is logged out, you can use this:
function custom_2_wp_nav_menu_args ( $args = '' ) {
$menu_slug = $args['menu']->slug;
if ( ! is_user_logged_in() && $menu_slug == 'test-menu' ){
$args['menu'] = 'test-menu-logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'custom_2_wp_nav_menu_args' );

WooCommerce - Redirect Logged In Users Based on their Role

This is a query based around WooCommerce Membership and Subscriptions.
I must add that I'm also trying to decide if it's good UX to do what I'm doing.
There are many solutions to redirecting users after they log in but I have a situation where I want to redirect a user with the role of 'subscriber' when they click on specific links to pages that describe and allow you to become a member. So although I don't want to hide 'join now' etc I just want those to redirect to the my-account page.
Again there are various roles and redirect plugins but none seem to help in this specific scenario. So the source of the code I've used is here: SOURCE and I'm looking to do something like this:
function eks_redirect_users_by_role() {
global $post;
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if ( 'subscriber' === $role_name && $post->ID == 47145) {
wp_redirect( '/my-account' );
}
}
add_action( 'admin_init', 'eks_redirect_users_by_role' );
So if the user role is a subscriber and they try and visit the page idea it's redirected.
At the current time, it does fall back to a product page which says 'you already have a membership' but it's multiple steps to arrive.
This might be more useful and proper way to achieve your request.
function redirection_based_user_role(){
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
$postid = get_the_ID();
if ( 'subscriber' === $role_name && $postid == 47145 ) {
wp_redirect( home_url( '/my-account' ), 301 );
exit;
}
}
add_action( 'template_redirect', 'redirection_based_user_role' );
Hope this works.
I was able to achieve want I wanted to the following way:
function eks_redirect_user() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
$postid = get_the_ID();
if ( 'subscriber' === $role_name && $postid == 47145 ) { ?>
<script>
function redirectPage() {
window.location = "/my-account/";
}
redirectPage();
</script>
<?php
exit;
}
}
add_action('wp_footer', 'eks_redirect_user' );
The issue is it's a fairly untidy solution as the redirect feels odd. I'm not sure if using wp_redirect would work better. I decided to do was just disable the button on the page with the main membership information rather than redirecting every call to action to the account page which seems like a more elegant solution.

How to redirect a button to a different page based on used login on Wordpress

I have a button that I have that leads users to a page on WordPress called /portfolio/, so is there a way that I can redirect users to the /registration/ page until their logged in?
I'm using a plugin on WordPress that uses the below code to change the menu per user logged in/logged out.
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' );
Is there a way that I can use the if( is_user_logged_in() ) to do the same with a redirect? I can't seem to lock the page down to all users.
You could so something like this?
if(!is_user_logged_in()) {
//If user not logged in
echo "<script> location.href='http://somesite/registration/'; </script>";
}
This should work if you arent using jquery as its JavaScript.

Auto redirect a user who isn't logged into the Wordpress site

Does anyone know how to set functions.php when redirection to login page when a user isn't logged in?
Here's my code which creates some disabilities like no preview on article page etc...
add_filter( 'pre_get_posts', 'swpm_auto_redirect_non_members' );
function swpm_auto_redirect_non_members() {
if( !is_admin() && !SwpmMemberUtils::is_member_logged_in() && !is_page( array( 'membership-login', 'membership-join' )) ) {
wp_redirect( 'http://example.com/membership-login/' );
exit;
}
}
It should work only when website is in service not admin mode.
Thanks in advance.
try to make $_SESSION["member],
and make this
if(!isset($_SESSION["member"]))
{
echo "<script>location='/login.php'</script>";
}

(Wordpress Multisite) Limit function to given sites

I have a little problem with my website, sorry for my noobish question, but I really can't solve the issue:
How can I, using multisite, limit a function to only given site? I use such function to adapt homepage regarding user state:
function switch_homepage() {
if ( is_user_logged_in() ) {
$page = 4284; // for logged in users
update_option( 'page_on_front', $page );
update_option( 'show_on_front', 'page' );
} else {
$page = 4133; // for logged out users
update_option( 'page_on_front', $page );
update_option( 'show_on_front', 'page' );
}
}
I want the code to just work on a main site. Thanks for you help!
You can test if the current site is the main site with this code:
if ( is_main_site() ) {
// Do stuff only for the main site
}
Another option is to add your code in a plugin and then activate the plugin only on the main site.

Categories