WordPress.org

Support

Support » Plugins and Hacks » Membership 2 » [Resolved] How to target users by subscription plan to redirect them on login?

[Resolved] How to target users by subscription plan to redirect them on login?

  • 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/

Viewing 15 replies - 1 through 15 (of 19 total)
  • 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,

    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’.

    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,

    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?

    David

    @ugotsta

    WPMU DEV Support Staff

    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

    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?

    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.

    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,

    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?

    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,

    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.

    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,

    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,

    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.

    David

    @ugotsta

    WPMU DEV Support Staff

    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

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘[Resolved] How to target users by subscription plan to redirect them on login?’ is closed to new replies.
Skip to toolbar