Archived topic
How to Have different HOME URL for logged-in users
7 replies · Started by Martin on February 6, 2020
Hi,
I would like to be able to specify a different HOME URL for when a user is logged in. This would be very useful for my Members Website, where I need to top LOGO IMAGE URL to point to a PREVIEW page when users are logged out (website home url) and a MEMBERS URL (for logged in users).
Is there a CSS I can use? Or any other simple trick?
Thank you!
Martin
Hi there,
CSS can only change the visual side of things so cannot be used to change the URL.
The theme itself doesn't control the URL/permalink structure as well.
I would recommend checking with the membership plugin support to see they can point you in the right direction.
Thank you for the reply.
Perhaps you would know of a way to achieve this via the function.php file that I could add in a child theme of generatepress?
Hi there,
Add this PHP snippet to your site:
add_action( 'wp', function() {
if ( is_front_page() && is_user_logged_in() ) {
wp_redirect( home_url( '/logged-in-home' ) );
die();
}
} );
https://docs.generatepress.com/article/adding-php/
change logged-in-home to the page slug you want to redirect to.
Finally I found a way to do it. I'm sharing it here in case someone in the future would like to do something similar.
1) Create a CHILD THEME from the used theme
2) In the Child Theme Folder:
A) Edit functions.php, and add this code:
// shortcode to display content for loggedout and loggedin users : shortcodes are [loggedout] [loggedin]
add_shortcode( 'loggedout', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return do_shortcode($content);
return '';
}
add_shortcode( 'loggedin', 'member_check_shortcode' );
function member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return do_shortcode($content);
return '';
}
B) Copy the file header.php from your theme to the child theme.
Then edit header.php:
Add the following in the top of the header.php file, somewhere before <body>:
<!-- CUSTOM HOME URL ON GP LOGO IMAGE IF USER IS LOGGED-IN -->
<?php
if ( is_user_logged_in() ) {
add_filter( 'generate_logo_href','generate_add_custom_logo_href' );
function generate_add_custom_logo_href()
{
return 'https://www.yourwebsite.com/logged-in-url';
} else {
}
?>
<!-- END CUSTOM HOME URL IF USER IS LOGGED-IN -->
Glad you found a solution and thanks for sharing
Here's a different approach I found, editing only the functions.php file in the child theme folder, which is preferred so the modification wont interfere with future updates of the main theme files:
Add this code to the functions.php file:
// LOGGED-IN USERS : HOME URL FOR TOP LOGO/HEADER IMAGE IN GENERATE PRESS
if ( is_user_logged_in() ) {
// set header logo image url when logged-in
add_filter( 'generate_logo_href','generate_add_custom_logo_href' );
function generate_add_custom_logo_href()
{
return 'https://www.yourwebsite.com/logged-in-url/';
}
}
I'll mark this topic as resolved.