Membership Functions not available in Admin side?

I'm trying to use current_user_on_subscription() when loading the dashboard to allow access based on membership status. If that function returns false, then they will be redirected to a subscription page.

However, that function will only return a fatal "undefined function" error.

I've tried hooking it in later in the process, but even at "shutdown" time the function still has not been defined.

I also tried to circumvent the function and call direct with...

$user = wp_get_current_user();

	$member = Membership_Plugin::factory()->get_member( $user->ID );

...which results in "Class 'Membership_Plugin' not found"

Am I wrong in thinking these functions should be available on the admin side of things?

Any ideas from the developers on this one?

Here's the full function I'm trying to use, placed in the functions.php of my active theme.

add_action( 'admin_init', 'showbiz_check_subscription' );
function showbiz_check_subscription(){
    if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {

        if (!current_user_on_subscription(1)){ // not an active subscriber

            wp_redirect( home_url('/subscribe/') );
            exit;

        }

    }

}
  • Ryan
    • Flash Drive

    Well I gave up on waiting!

    I dug into the Membership code to get to the root of the function, and ended up calling the database query directly.

    add_action( 'admin_init', 'check_subscription' );
    function check_subscription(){
        global $wpdb;
    
        if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
    
            // check for active subscription (ID: 1) because current_user_on_subscription(1) ain't workin'
            $result = $wpdb->get_col( sprintf(
                'SELECT rel_id FROM %s WHERE user_id = %d AND sub_id = %d',
                'wp_m_membership_relationships',
                get_current_user_id(),
                1
            ) );
    
            if (empty($result)){ // not an active subscriber on plan #1
                // no dashboard access... send them to signup page
                wp_redirect( home_url('/subscribe/') );
                exit;
    
            }
    
        }
    
    }

    Not the elegant solution, but it works good enough for now.

  • Ash
    • WordPress Hacker

    Hello @Ryan

    I am extremely sorry for the delay here.

    You have attached your function with admin_init hook. But when the hook is called no plugin is loaded, so the function is unidentified.

    How's about calling the function in template_redirect hook? Like:

    add_action( 'template_redirect', 'showbiz_check_subscription' );
    function showbiz_check_subscription(){
        if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
    
            if (!current_user_on_subscription(1)){ // not an active subscriber
    
                wp_redirect( home_url('/subscribe/') );
                exit;
    
            }
    
        }
    
    }

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • Saurabh
    • New Recruit

    Hi @Ryan,

    Hope you are well!

    Sorry about the delay here.

    I have copy pasted your original exact code on a demo site and it worked like a charm. The admin_init action is run much after the plugins_loaded action (when all the plugins are loaded. So, that is not the issue.

    The code that you have used later that directly accesses the database is fine, but ideally the original code should work just as fine. If we could look at the website, we might be able to find out what could be causing this.

    It could be an error that might interfere with more things in the future.

    Best

    Saurabh

  • Ryan
    • Flash Drive

    Thanks for the responses @Ashok and @Saurabh Shukla

    Just as I was getting ready to post I had the "ah-ha" moment. This Membership plugin is installed in a Multisite environment, and is only active on the primary site.

    I'm trying to block access to dashboards on other sites in the network that do not have Membership active. So, obviously, the plugin would not be getting initialized.

    Please allow me to slap my forehead. I apologize for dragging you folks in to my own mistake.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.