admin管理员组

文章数量:1125900

I want to check if user is subscribed to email is checked. It uses "email_subscriber" meta key from a plugin.

If subscribed then assign role Email-Subscriber otherwise remove from role list.

This is the code I am using but not working.

    add_filter( 'insert_user_meta', function( $meta, $user, $update ) {

  if( true !== $update ) return $meta;
 
  $email_subscriber_meta = get_user_meta($user, 'email_subscriber', true);

    // Check if the array is not empty and doesn't contain the specific false condition
    $is_email_subscriber = !empty($email_subscriber_meta) 
        && !(isset($email_subscriber_meta[0])
        && $email_subscriber_meta[0] === 'email_subscriber');

    if ($is_email_subscriber) {
        // Add the role if it doesn't exist
        if (!in_array('Email-Subscriber', $user->roles)) {
            $user->add_role('Email-Subscriber');
        }
    } else {
        // Remove the role if it exists
        if (in_array('Email-Subscriber', $user->roles)) {
            $user->remove_role('Email-Subscriber');
        }
    }
  
}, 10, 3 );

I want to check if user is subscribed to email is checked. It uses "email_subscriber" meta key from a plugin.

If subscribed then assign role Email-Subscriber otherwise remove from role list.

This is the code I am using but not working.

    add_filter( 'insert_user_meta', function( $meta, $user, $update ) {

  if( true !== $update ) return $meta;
 
  $email_subscriber_meta = get_user_meta($user, 'email_subscriber', true);

    // Check if the array is not empty and doesn't contain the specific false condition
    $is_email_subscriber = !empty($email_subscriber_meta) 
        && !(isset($email_subscriber_meta[0])
        && $email_subscriber_meta[0] === 'email_subscriber');

    if ($is_email_subscriber) {
        // Add the role if it doesn't exist
        if (!in_array('Email-Subscriber', $user->roles)) {
            $user->add_role('Email-Subscriber');
        }
    } else {
        // Remove the role if it exists
        if (in_array('Email-Subscriber', $user->roles)) {
            $user->remove_role('Email-Subscriber');
        }
    }
  
}, 10, 3 );
Share Improve this question edited Jan 26, 2024 at 13:31 Tom J Nowell 60.7k7 gold badges77 silver badges147 bronze badges asked Jan 26, 2024 at 12:33 AliAli 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Roles in wordpress are case sensitive, I used the lowercase text for the same.

Your function must return a value.

    add_filter('insert_user_meta', function ($meta, $user, $update) {
    if (true !== $update) return $meta;

    //Check 'email_subscriber' meta-key for the user
    $email_subscriber_meta = get_user_meta($user, 'email_subscriber', true);

    // Check the user if it is subscribed by meta-value if checkbox value is something else replace 1 with that value
    $is_email_subscriber = !empty($email_subscriber_meta) && $email_subscriber_meta === '1';

    if ($is_email_subscriber) {
        // Add the role incase itdoesn't exist
        if (!in_array('email_subscriber', $user->roles)) {
            $user->add_role('email_subscriber');
        }
    } else {
        // Remove the role if it exists
        if (in_array('email_subscriber', $user->roles)) {
            $user->remove_role('email_subscriber');
        }
    }

    return $meta;
}, 10, 3);

本文标签: phpOn profile update check if user is Subscribed to emails