Pro Sites - show content based on pro sites status and users

I needs some help with extending this piece of code for Pro Sites:

<?php
$blog_id = get_current_blog_id();
$level = 0;

if ( is_pro_site($blog_id, $level) ) { ?>
// pro content
<?php }
else {
?>
// other content
<?php
} ?>

This code checks if current site (blog) is pro site or not. If it is, it shows some content and if not it shows different content.

The point is that this code should also check if a logged in user does have any pro site level sites. So even if the given site is not pro site (it's a free site) but the currently logged in user does have any pro site in the network - the code should show the content meant for pro site to that user.

  • Panos

    Hi Mathias ,

    You mentioned:

    the currently logged in user does have any pro site in the network

    so I suppose you mean that this user should be the owner of a pro site, not only an amdinistrator right?

    In such case you could check
    1. the blogs of the user:
    $user_blogs = get_blogs_of_user( $user_id );
    2. then for each blog check if it is a pro site:
    if ( is_pro_site( $blog_id ) && ! is_pro_trial( $blog_id ) ) {
    3. and finally if the user is the "owner" of that blog:
    $mail = get_blog_option( $blog->userblog_id, 'admin_email' );

    I have wrapped it up in a shortcode snippet which you can view/download from here:
    https://gist.github.com/panoslyrakis/78da5ebc76b883f2c67f93792e646fcc

    It provides a shortcode
    [wpmudev_ps_notice]
    which you can use in any page you want to display this message. The shortcode can be used wihtout any parameter which will use current user, or you can add the user_id attribute if you want to show for specific id ( you won't be needing that I guess ) eg:
    [wpmudev_ps_notice user_id="3"]

    To test it, you can download that file and upload it to your wp-content/mu-plugins folder ( if that folder doesn't exist you can simply create it ). Before uploading you'll need to modify the messages first.

    Hope this helps :slight_smile:

    Cheers!

  • Panos

    Hi Mathias,

    Sure you can!

    The basic functionality you need is in method ps_notice( $atts )

    First you need to get the user blogs:
    $user_blogs = get_blogs_of_user( $user_id );

    Then go through all blogs and check if user's email is set in the blog's option admin_email:

    $mail = get_blog_option( $blog->userblog_id, 'admin_email' );
    $user_from_email = get_user_by( 'email', $mail );
    if( $user_id == $user_from_email->ID ){
    //do stuff
    }

    Hope this helps :slight_smile:

  • Panos

    Hi Mathias ,

    I'm afraid this request exceeds the scope of our support, as it is a custom requirement. I can give another usage example which hopefully would help you out. In the following example I'm using the same code in the wp_footer action. So in the footer of each page you should see a message :

    add_action( 'wp_footer', function(){
    
    	// 1st check if user is NOT logged in
    	if( ! is_user_logged_in() ){
    		//Here you can display something before the return
    		return;
    	}
    
    	//Set a variable wich will hold true if user has at least one pro site else it will store false:
    	$has_a_pro_site = false;
    
    	// 2. get user id:
    	$user_id = get_current_user_id();
    
    	// 3. Then we get the user's blogs
    	$user_blogs = get_blogs_of_user( $user_id );
    
    	// 4. Check all blogs if they are pro sites:
    	foreach ( $user_blogs as $blog ) {
    
    		if ( is_pro_site( $blog->userblog_id ) && ! is_pro_trial( $blog->userblog_id ) ) {
    			$mail = get_blog_option( $blog->userblog_id, 'admin_email' );
    			$user_from_email = get_user_by( 'email', $mail );
    			if( $user_id == $user_from_email->ID ){
    				$has_a_pro_site = true;
    				break;
    			}
    		}
    	}
    
    	// 5. Display message according to if $has_a_pro_site is true or false
    	if( $has_a_pro_site ){
    		echo 'You got at least one pro site';
    	}
    	else{
    		echo 'Sorry you don\'t seem to have any pro site';
    	}
    
    } );

    So you can copy the exact code inside the action ( don't copy the "add_action(..." line nor the closing line "});" ) and use it where you need.

    In case you still have difficulties implementing the code on your site I would recommend to hire a dev from here:
    https://premium.wpmudev.org/wordpress-development/
    who could help you out better here and provide a complete solution.

    Thanks!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.