WordPress.org

Ready to get started?Download WordPress

Forums

Membership
[resolved] How to target users by subscription plan to redirect them on login? (20 posts)

  1. miguelcortereal
    Member
    Posted 8 months ago #

    Using the free version of membership and using 2 subscription plans, one free and another paid.

    The paid plan gives access to Buddypress.

    I want to redirect free plan users to dashboard, paid plan users to Buddypress member profile page after login.

    The function below displayed at WP Codex is preety much what i need if users were targeted by role, which isn't true with membership.

    So, how do I target users by subscription plan to redirect them to either bp profile page or dashboard?

    function my_login_redirect( $redirect_to, $request, $user ){
        //is there a user to check?
        global $user;
        if( isset( $user->roles ) && is_array( $user->roles ) ) {
            //check for admins
            if( in_array( "administrator", $user->roles ) ) {
                // redirect them to the default place
                return $redirect_to;
            } else {
                return home_url();
            }
        }
        else {
            return $redirect_to;
        }
    }
    add_filter("login_redirect", "my_login_redirect", 10, 3);

    http://wordpress.org/plugins/membership/

  2. WPMU DEV
    Member
    Posted 8 months ago #

    Hi @miguelcortereal,

    I hope you are well today and thank you for your question.

    To achieve this add following code in functions.php file of your child theme or add it in your site using any of the following plugins.

    In the following code just replace $sub_id with the paid subscription id.

    function my_login_redirect( $redirect_to, $request, $user ){
    
        global $user;
        if( function_exists('current_user_has_subscription') && current_user_has_subscription() ) {
            //check for paid subscription
            if( function_exists('current_user_on_subscription') && current_user_on_subscription( $sub_id ) ) {
              if( functions_exists('bp_core_get_user_domain') )
                 return bp_core_get_user_domain($user->ID);
            } else {
                return get_admin_url();
            }
        }
        else {
            return $redirect_to;
        }
    }
    add_filter("login_redirect", "my_login_redirect", 10, 3);

    http://wordpress.org/plugins/add-actions-and-filters/
    http://wordpress.org/plugins/code-snippets/

    Best Regards,

  3. miguelcortereal
    Member
    Posted 8 months ago #

    Thanks a lot for your quick reply.

    Your function does the job.

    For future readers interested in use this function, there's a typo on 3rd if-condition:

    Replace 'functions_exists' by 'function_exists'.

  4. WPMU DEV
    Member
    Posted 8 months ago #

    Hi @miguelcortereal,

    You are most welcome and thanks for notifying about the typo.

    Here's the new code correcting typo:

    function my_login_redirect( $redirect_to, $request, $user ){
    
        global $user;
        if( function_exists('current_user_has_subscription') && current_user_has_subscription() ) {
            //check for paid subscription
            if( function_exists('current_user_on_subscription') && current_user_on_subscription( $sub_id ) ) {
              if( function_exists('bp_core_get_user_domain') )
                 return bp_core_get_user_domain($user->ID);
            } else {
                return get_admin_url();
            }
        }
        else {
            return $redirect_to;
        }
    }
    add_filter("login_redirect", "my_login_redirect", 10, 3);

    Cheers,

  5. miguelcortereal
    Member
    Posted 7 months ago #

    Hi again

    I've implemented the function according my needs, but I'm having a strange behaviour that I don't know what can be causing it.

    Here's my function:

    function members_redirect($redirect_to, $request, $user){
    
        global $user;
        if ( function_exists('current_user_has_subscription')&& current_user_has_subscription()) {
            if ( function_exists('current_user_on_subscription')) && current_user_on_subscription('1') {
            	if ( function_exists('bp_core_get_user_domain') {
              	  return bp_core_get_user_domain($user->ID);
              	}
            } else { return admin_url(); }
            } else {
            	return 'http://www.mysite.com/subscriptions/'; }
    }
    
    add_filter('login_redirect', 'members_redirect', 10, 3);

    The returning values of the conditionals are:

    - function_exists('current_user_has_subscription'), TRUE
    - current_user_has_subscription(), FALSE
    - function_exists('current_user_on_subscription'), TRUE
    - current_user_on_subscription('1'), FALSE

    The user that is logging for whom those values are returned has subscription and its $sub_id is 1, so the returned FALSES should be TRUES.

    What am I missing here?

  6. David
    WPMU DEV Support Staff
    Posted 7 months ago #

    Hi @miguelcortereal,

    Your $sub_id needs to be numeric but you specified it as a string using '1'. Perhaps this will sort it?

    <?php
    function members_redirect($redirect_to, $request, $user){
    	global $user;
    	if ( function_exists('current_user_has_subscription')&& current_user_has_subscription()) {
    		if ( function_exists('current_user_on_subscription')) && current_user_on_subscription(1) {
    			if ( function_exists('bp_core_get_user_domain') {
    				return bp_core_get_user_domain($user->ID);
    			}
    		} else {
    			return admin_url();
    		}
    	} else {
    		return 'http://www.mysite.com/subscriptions/';
    	}
    }
    
    add_filter('login_redirect', 'members_redirect', 10, 3);
    ?>

    Cheers,
    David

  7. miguelcortereal
    Member
    Posted 7 months ago #

    Thanks a lot for your quick reply David.

    Unfortunately it keeps returning FALSE replacing the string by the value.

    I've tested each one of the conditionals by removing them all, then apply one by one within the function. I confirm that:

    current_user_has_subscription() and current_user_on_subscription(1)

    are returning FALSE, the others are returning TRUE and of course using a member account assigned to $sub_id 1.

    I've also, changed the priority parameter from 10 to 100, I've used the built in plugin routines to test and repair plugin's tables at database, everything looks fine but no success.

    In order to get it working, I guess I should replace those two membership functions mentioned above by code that collects from database the value assigned to current user and then make a conditional for redirection.

    What would be the code for that?

  8. miguelcortereal
    Member
    Posted 7 months ago #

    BTW, I've switched on debug mode and it returns this notice at subscriptions page:

    Notice: Undefined index: action in /wp-content/plugins/membership/membershipincludes/classes/membershippublic.php on line 1322

    Not sure if it is related with two shortcodes of my own working in this page and not contemplated with membership plugin.

    Other than that, I don't see nothing else that might be preventing those conditionals to work.

  9. WPMU DEV
    Member
    Posted 7 months ago #

    Hi @miguelcortereal,

    This is a simple PHP Notice and i don't think it is affecting the plugin functionality.

    I used current_user_has_subscription() and current_user_on_subscription() functions on my test site using latest version of Membership plugin and it worked fine.

    Are you facing this problem with only a particular user or with all users?

    Could you please try creating new test user and assigning him a subscription?

    Kind Regards,

  10. miguelcortereal
    Member
    Posted 7 months ago #

    Hi again,

    I'm facing this problem with all users assigned to both Free and Paid plans defined with Membership and the only two allowed because I'm using your free version plugin.

    Paid plan is $sub_id 1
    Free plan is $sub_id 3

    With the code shown above everyone goes to subscriptions page, when should be distributed between bp profile page and dashboard page.

    I've tried to create a new user account as you suggested and no success again.

    Exploring a little more Debug log errors, I found this one too:

    Notice: bp_setup_current_user was called incorrectly. The current user is being initialized without using $wp->init(). Please see Debugging in WordPress for more information. (This message was added in version 1.7.) in /home1/xxxxxxx/public_html/xxxxxxx.com/wp-includes/functions.php on line 3049.

    Don't know if it might be related.

    Any turn around to get it working?

  11. WPMU DEV
    Member
    Posted 7 months ago #

    Hi @miguelcortereal,

    Could you please add following code in any theme template file and check whether it contains proper subscription id in the printed output?

    <?php
    $user = wp_get_current_user();
    $member = new M_Membership( $user->ID );
    print_r($member);
    ?>

    Also try re installing plugin and creating new subscription.

    Regards,

  12. miguelcortereal
    Member
    Posted 7 months ago #

    I did re installed the plugin. created a new subscription with the fresh install and got the same behaviour.

    Pasted that code in a template and it returns the right $sub_id for users.

    Also deactivated most plugins and still no success.

    There's also another detail that I've checked which is my hosting company keeps PHP version 5.2.17 from 6-Jan-2011, three years old and likely outdated.

  13. WPMU DEV
    Member
    Posted 7 months ago #

    Hi @miguelcortereal,

    Thank you for your reply.

    There may be a compatibility issue with the PHP version you are using.

    Could you please test it on another WordPress install and hosting server / local host to confirm this is the issue with your hosting server or WordPress install?

    Regards,

  14. miguelcortereal
    Member
    Posted 7 months ago #

    My hosting account is upgraded to PHP 5.4.24

    No changes.

    I've also just added to that template just after your given code this one:

    echo 'Has subscription ' . current_user_has_subscription();
    echo ' Has paid subscription  ' . current_user_on_subscription(1);

    Both echoes return 1, which means these functions are working correctly,

  15. miguelcortereal
    Member
    Posted 7 months ago #

    Moved the redirection function from functions.php to a plugin file and same result.

    Made a few tests more and looks like both current_user_has_subscription() and current_user_on_subscription(1) are returning "".

    So, they work in a template file but doesn't in fuctions.php or a plugin file.

  16. David
    WPMU DEV Support Staff
    Posted 7 months ago #

    Hi @miguelcortereal, that's a good question and without having actually tested specifically that, I wonder if a higher priority might fix this:
    add_filter('login_redirect', 'members_redirect', 90);

    Could you let me know how that works?

    Thanks,
    David

  17. miguelcortereal
    Member
    Posted 7 months ago #

    Doesn't change anything. I've tried a priority of 90, 100 and even 500.

    It looks like I've reached a dead end. There must be anywhere specific configurations triggering this issue which I'm not able find for now and respond to.

    Is there any other ways to code that function so that different $sub_id members can be targeted, or any other method to have login redirections running?

  18. WPMU DEV
    Member
    Posted 7 months ago #

    Hi @miguelcortereal,

    Could you please try using following code in functions.php file of your theme?

    Make sure the buddypress plugin is active.

    function my_login_redirect( $redirect_to, $request, $user ){
    
        global $wpdb;
    	$sql = $wpdb->prepare( "SELECT count(*) FROM {$wpdb->prefix}m_membership_relationships WHERE user_id = %d AND sub_id != 0", $user->ID );
    	$res = $wpdb->get_var($sql);
    
        if( $res > 0 ) {
            //check for paid subscription
    		$sql = $wpdb->prepare( "SELECT rel_id FROM {$wpdb->prefix}m_membership_relationships WHERE user_id = %d AND sub_id = %d", $this->ID, 1 );
    		$result = $wpdb->get_col( $sql );
    
    		if( !empty($result)) {
              if( function_exists('bp_core_get_user_domain') )
                 return bp_core_get_user_domain($user->ID);
            } else {
                return get_admin_url();
            }
        }
        else {
            return $redirect_to;
        }
    }
    add_filter("login_redirect", "my_login_redirect", 10, 3);

    I have tested the above code with default WordPress theme Twenty Twelve and it worked fine.

    If it doesn't work for your current theme then try using default WordPress theme Twenty Twelve.

    Regards,

  19. miguelcortereal
    Member
    Posted 7 months ago #

    Great solution, its working now.

    Just had to replace $this by $user and it got working fine.

    Thanks a lot for your nice support.

  20. WPMU DEV
    Member
    Posted 7 months ago #

    Hi @miguelcortereal,

    Glad it's working for you now :)

    There's again a typo in the code.

    Here's the new code correcting typo:

    function my_login_redirect( $redirect_to, $request, $user ){
    
        global $wpdb;
    	$sql = $wpdb->prepare( "SELECT count(*) FROM {$wpdb->prefix}m_membership_relationships WHERE user_id = %d AND sub_id != 0", $user->ID );
    	$res = $wpdb->get_var($sql);
    
        if( $res > 0 ) {
            //check for paid subscription
    		$sql = $wpdb->prepare( "SELECT rel_id FROM {$wpdb->prefix}m_membership_relationships WHERE user_id = %d AND sub_id = %d", $user->ID, 1 );
    		$result = $wpdb->get_col( $sql );
    
    		if( !empty($result)) {
              if( function_exists('bp_core_get_user_domain') )
                 return bp_core_get_user_domain($user->ID);
            } else {
                return get_admin_url();
            }
        }
        else {
            return $redirect_to;
        }
    }
    add_filter("login_redirect", "my_login_redirect", 10, 3);

    You are most welcome, if I can be of any further assistance please don't hesitate to ask :)

    Cheers,

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.