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
1 Answer
Reset to default 0Roles 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
版权声明:本文标题:php - On profile update check if user is Subscribed to emails? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736676073a1947186.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论