Edge case when determining if a logged-in user is a subscriber
-
Hi Ján,
Thanks for your help! What you gave me in an earlier thread to show on the front end if the current logged-in user is a subscriber is this:$current_user_email = wp_get_current_user();
$email = $current_user_email->user_email;
$list_id = '4'; // You can get this ID from the URL when editing the list
if ( class_exists( \MailPoet\API\API::class ) ) {
$mailpoet_api = \MailPoet\API\API::MP( 'v1' );
try {
// Find subscriber
$subscriber = $mailpoet_api->getSubscriber( $email );
// Check if the subscriber is subscribed to the specific list
$is_subscribed = false;
foreach ( $subscriber[ 'subscriptions' ] as $subscription ) {
if ( $subscription[ 'segment_id' ] === $list_id ) {
$is_subscribed = true;
break;
}
}
if ( $is_subscribed ) {
echo "The subscriber is subscribed to the list with ID $list_id.";
} else {
echo "The subscriber is NOT subscribed to the list with ID $list_id.";
}
} catch ( Exception $e ) {
if ( $e->getCode() === 4 ) {
echo 'Subscriber not found.';
} else {
echo "An error occurred: " . $e->getMessage();
}
}
}and it mostly works, except for one issue.
But what I found might be an edge case: some users who are unsubscribed still show as subscribed with the above function on the front end, i.e. this user in this screenshot
https://ibb.co/VYB6sLvX
that is from/wp-admin/admin.php?page=mailpoet-subscribers
is shown to be subscribed.What is the difference between a user that is “Unsubscribed”, yet also shows to be in the New Post Emails list as shown in the screenshot?
Thanks!
- You must be logged in to reply to this topic.