• Resolved Win Win

    (@aminericher)


    Ok I set up a function limiting the acces to certain capabilities (in the example below: edit_posts ). My question is if there is any way to reset the count of these limited capability based on the ‘s2member_last_payment_time’. I mean the count is reset to zero if the member renewed his membership (last_payment_time ley(s say 5 seconds for example) so that the member can access this capability again 5 seconds after the payment. Any ideas or suggestions ?

    function check_post_limit(){
           if(current_user_can('edit_posts')) {
    
           global $userdata;
           global $post_type;
           global $wpdb;
           $item_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = $userdata->ID" );
    		if(( $item_count >= 2 ) && (current_user_is("s2member_level1")) )
          { wp_die( 'Do not create too much posts.','check-limit' ); }
    
    	return;   }
        }

    https://www.ads-software.com/plugins/s2member/

Viewing 9 replies - 1 through 9 (of 9 total)
  • You can set a custom URL at “API / Notifications” -> “Payment notifications” to be possible to know when the exactly paiment is made, and who pay. Save this info in user’s metas, then use it in your function.

    Thread Starter Win Win

    (@aminericher)

    Looking to couple of discussions out there I manage to write a ‘final code’ which i think should work. However, it doesn’t. The user is now unlimited! So can you help me please and tell me if there is something wrong or if I missed something, or if this is not working simply because $s2member_paid_registration_times doesn’t work on a test mode (using sandbow).
    NOTE: The code in the first comment works but it limits the access even if the member renew his membership. This second code doesn’t work and does not limit the access at all. I can’t figure it out why !

    function check_post_limit(){
           if(current_user_can('edit_posts')) {
    
           global $userdata;
           global $post_type;
           global $wpdb;
           $postintervall = date('Y-m-d H:i:s', $s2member_last_payment_time );
           $item_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = $userdata->ID AND post_date > '$postintervall'" );
    		if(( $item_count >= 2 ) && (current_user_is("s2member_level1")) )
          { wp_die( 'Do not create too much posts.','check-limit' ); }
    
    	return;   }
        }
    Thread Starter Win Win

    (@aminericher)

    Thanks @krumch for the suggestion, but how can I do that? I created a mu-plugin for the s2_payment_notification. I’m just a beginner in coding and I can’t see what to put in there, and how to use it in my function. Any help please?

    In your second code: where $s2member_last_payment_time gets value?

    Thread Starter Win Win

    (@aminericher)

    Regarding the $s2member_last_payment_time I assume that s2Member keeps a record of on-site. If not, how should I record it in a table?

    Sure, but the variable value must be imported to your function, like every other variable value to any PHP function. You should find where you can get it from. Maybe user’s metas…

    Thread Starter Win Win

    (@aminericher)

    Yes it is in the user’s metas, I tried adding:
    $s2member_last_payment_time= get_meta_user($user_id, 'wp_meds2member_last_payment_time', true);
    Nothing work.
    See the usermeta table
    Any suggestions? Thanks

    Where you set $user_id value?

    Print (or mail to yourself) the value of $s2member_last_payment_time to see what you actually get. May be an error object. Or another surprise…

    I don’t know the problem, but this is the way I fix unknown problems.

    Thread Starter Win Win

    (@aminericher)

    The problem was that I was adding a double check of the (post_type=’course’) in the condition of the function and in the $item_count. When I remove it from the condition, it works. Thank you for the guidance @krumch !
    This is the working function, maybe it can inspire someone:

    function check_post_limit(){
       if(current_user_can('edit_posts')) {
       global $userdata;
       global $wpdb;
       $s2member_last_payment_time = get_user_meta( get_current_user_id(), 'wp_s2member_last_payment_time', true );
       $postintervall = date('Y-m-d H:i:s', $s2member_last_payment_time );
       $item_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = $userdata->ID AND post_date > '$postintervall'" );
        if(( $item_count >= 2 ) && (current_user_is("s2member_level1")) )
      { wp_die( 'Do not create too much posts.','check-limit' ); }
    return;   }
    }

    N.B: Change ‘wp_s2member_last_payment_time’ by the custom meta_key of s2member_last_payment_time in your table, it depends on the prefix you used in your database !

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Limit access based on last payment’ is closed to new replies.