I'm trying to compare a email address held in a PHP variable with a email address held in a short code in Wordpress, this is what I've tried so far:
$email = 'someone@something.com';
$user_email = do_shortcode('[userinfo field="user_email"]');
echo var_dump(strcmp($user_email, $email) === 0);
But the var_dump
always returns false
, I'm positive they are the exact same string!
You should not use the shortcode for that but just the Wordpress API function to obtain the current users email address:
The Worpdress API function
get_currentuserinfo()
sets the global variable$user_email
to the email address of the current user as a string.See if there are any spaces and if any of the string needs to be trimmed, because if both the strings are same then your code seems to work already.
Returns
bool(true)
Probably
do_shortcode('[userinfo field="user_email"]');
needs to be trimmed. Also you can simply echo$user_email
before comparison to see if there is any unexpected value there.By default the
userinfo
shortcode returns the data wrapped in a<span>
tag. To suppress the span tag you can use thenospan
-attribute.The description of the plugin says the following:
So your code should look like that: