(Wordpress Multisite) Limit function to given sites - php

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.

Related

Redirect WordPress User based on user role when accessing a page

I'm trying to create a simple redirect when someone tries accessing a certain page depending on user role.
I've created a site with membership functionality that offers certain things to paid members. Free members can only access one page after login but if they know the URL to the paid area they can see the content. I was wondering if there's a way to create a 301 redirect so that when the free member tries to go on the paid page, it redirects them back to the free area.
Is this possible? If you need more info, please ask!
Thanks
David
You can also try like this and make it more wordpress friendly.
function role_redirections() {
$loggedin_user = wp_get_current_user();
if (!is_user_logged_in() || !in_array( 'memorial_user', (array) $loggedin_user->roles )) {
$location1 = get_option( 'siteurl' ) .'/member-portal/';
header('Location: '.$location1.'');
}
}
add_action('wp_head', 'role_redirections');
paste this code into your activate theme or chilled theme function.php file
function role_redirections() {
$logedin_user = wp_get_current_user();
if (!is_user_logged_in() || !in_array( 'shop_manager', (array) $logedin_user->roles )) {
$location1 = home_url();
header('Location: '.$location1.'');
}
}
add_action('wp_head', 'role_redirections');
#rajat.gite
I tried you answer but it didn't quite work how I wanted so I modified the code as below:
function role_redirections() {
$loggedin_user = wp_get_current_user();
if (!is_user_logged_in() || !in_array( 'memorial_user', (array) $loggedin_user->roles )) {
$location1 = 'https://www.myswansong.com/member-portal/';
header('Location: '.$location1.'');
}
}
add_action('wp_head', 'role_redirections');
The only real difference is amending the home_url(); to a string with the full URL I need.
Thanks for your help.

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.

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>";
}

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

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' ) );
}
?>

How to redirect new WordPress multisite user inside their admin panel after they log in?

Here is the code in my functions.php that redirects users (to their new "Sample Page") when they log in:
function admin_default_page() {
return '/wp-admin/post.php?post=2&action=edit';
}
add_filter('login_redirect', 'admin_default_page');
The problem is that the url to which we redirect is missing the users site url.
For example, if a user creates a site "Bobs Trucks", it redirects them to
example.com/wp-admin/post.php?post=2&action=edit (does not work)
instead of
example.com/bobstrucks/wp-admin/post.php?post=2&action=edit
I've tried all the WordPress functions to get the full url needed (example.com/bobstrucs/url-to-redirect-to) but have not succeeded.
I'm trying a rather hackish solution where I get the user ID form the logged in user, and get all the blogs the user has (= just one, "Bobs Trucks"):
function admin_default_page() {
global $current_user;
get_currentuserinfo();
$blogs = get_blogs_of_user( $current_user->ID);
foreach($blogs as $blog) { $userblog = $blog->siteurl; }
return $userblog .'/wp-admin/post.php?post=2&action=edit';
}
add_filter('login_redirect', 'admin_default_page');
But this one has the problem of not working the first time user logs in.
Thank you very much!
Try something like this:
function admin_default_page() {
if ( is_user_logged_in() && is_admin() ) {
$blog_id = get_current_blog_id();
$blog = get_blog_details( array( 'blog_id' => $blog_id ) );
$location = '/' . $blog->blogname . '/wp-admin/post.php?post=2&action=edit';
wp_redirect( $location );
exit;
}
}
add_filter( 'login_redirect', 'admin_default_page' );
Refs:
http://codex.wordpress.org/Function_Reference/get_blog_details
http://codex.wordpress.org/Function_Reference/wp_redirect

Categories