Search code examples
phpwordpressshortcode

Assign Wordpress Short code to PHP Variable?


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!


Solution

  • 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);