0

Is it possible to use a function in functions.php that would automatically change user role when a membership goes from status "active" to "expired" ?

For example : if an active membership switches to expired, I want the user role to change automatically from "Subscriber" to "Blocked" for the concerned user.

I've tried the following hook but it's not doing anything when I update the user membership to expired. I think my problem comes from the meta data "expired" which is registered on table "rcp_membership" (field "status") and not on "usermeta" table.

add_action( 'profile_update', 'my_profile_update', 10, 2 );

function my_profile_update( $user_id, $old_user_data ) {

  if( get_the_author_meta( "rcp_status", $user_id ) == "expired") {
     $u = new WP_User( $user_id );
     $u->set_role('blocked');
   }
}

And data I have to update (what I try through function "set_role()" in the code) is in the database table "metadata", with this SQL request :

UPDATE `wp_usermeta` SET `meta_value` = 'a:1:{s:11:\"bbp_blocked\";b:1;}' WHERE `wp_usermeta`.`user_id` = 29;
2
  • You could either 1) hook the state change, either as it's updated in the user record or when the subscription code changes it (but that would be specific to whichever subscription code you're using) or 2) schedule a daily job to check that all memberships and roles are consistent and block expired subscriptions.
    – Rup
    Mar 11, 2021 at 9:28
  • I think i would be more confortable with solution 1 and a hook when state changes. I've updated the post with solution I tried and I would need help to know how to make a condition on a specific value of database.
    – nevax
    Mar 15, 2021 at 19:39

1 Answer 1

0

If you need to get the value of a field in a custom database table you can try using an SQL query. You'll have to check the column or table name to construct final version of the query.

add_action( 'profile_update', 'my_profile_update', 10, 2 );
function my_profile_update( $user_id, $old_user_data ) {

    global $wpdb;
    $status = $wpdb->get_var( $wpdb->prepare(
        " SELECT rcp_status FROM rcp_membership WHERE ID = %d ",
        $user_id
    ) );

    if( $status == "expired") {
        $new_role = array( 'bbp_blocked' => 1 );
        $data = serialize( $new_role ); 
        $updated = update_user_meta( $user_id, 'wp_capabilities', $data );

        // check and make sure the stored value matches $new_role.
        if ( $data != get_user_meta( $user_id, 'wp_capabilities', true ) ) {
            wp_die( __( 'user role not updated', 'textdomain' ) );
        }
    }

}
2
  • Thanks we are almost here ! It's updating role value to "blocked" so far because I think the value must be updated inside the table "wp_usermeta" and another SQL request could do the job. Do you know how I can replace function set_role() by an SQL request to directly update the right field in database ?
    – nevax
    Mar 16, 2021 at 19:18
  • I've updated my description with SQL request that updates the role value in database
    – nevax
    Mar 16, 2021 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.