0

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 = '[email protected]';
$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!

6
  • What is the output of var_dump(strcmp($var, $var2)); ? Also Check individual string using var_dump. Commented Mar 8, 2013 at 5:34
  • @Rikesh When I dump the vars one says string(48) and the other one says string(18), what does that mean? Commented Mar 8, 2013 at 5:43
  • Seems that your do_shortcode is not working as expected. Commented Mar 8, 2013 at 5:45
  • @WilliamL.: The shortcode returns something like that: <span class="userinfo">[email protected]</span> (with var_dump you would have seen that). Commented Mar 8, 2013 at 5:48
  • @vstm Your right, it does show that! Is there a way to just get the email address? Commented Mar 8, 2013 at 5:50

3 Answers 3

3

By default the userinfo shortcode returns the data wrapped in a <span> tag. To suppress the span tag you can use the nospan-attribute.

The description of the plugin says the following:

[userinfo nospan="true"] should eliminate the surrounding span tag so the output can be used inside URLs or similar applications

So your code should look like that:

$email = '[email protected]';
$user_email = do_shortcode('[userinfo field="user_email" nospan="true"]');
$var = (string) $user_email; // Casts to string
$var2 = (string) $email; // Casts to string
echo var_dump(strcmp($var, $var2) === 0);
Sign up to request clarification or add additional context in comments.

Comments

1

You should not use the shortcode for that but just the Wordpress API function to obtain the current users email address:

$email = '[email protected]';

global $user_email;
get_currentuserinfo();

echo var_dump(strcmp($user_email, $email) === 0);

The Worpdress API function get_currentuserinfo() sets the global variable $user_email to the email address of the current user as a string.

Comments

0

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.

$email = '[email protected]';
$user_email = '[email protected]';
$var = (string) $user_email; // Casts to string
$var2 = (string) $email; // Casts to string
echo var_dump(strcmp($var, $var2) === 0);

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.

4 Comments

When I dump the vars one says string(48) and the other one says string(18), does this mean I need to trim the strings?
yes this means strings are not equal, what does a simple echo suggest? do they look equal? are there any unexpected characters?
The echo just shows string(48) followed by the email address and string(18) followed by the same email address, its exactly the same.
also view source not view in browser. there can be HTML tags that get hidden.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.