40268

Assign Wordpress Short code to PHP Variable?

Question:

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!

Answer1:

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 <a href="http://wordpress.org/extend/plugins/user-meta-shortcodes/" rel="nofollow">description of the plugin</a> says the following:

<blockquote>

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

</blockquote>

So your code should look like that:

$email = 'someone@something.com'; $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);

Answer2:

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

$email = 'someone@something.com'; 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.

Answer3:

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 = 'someone@something.com'; $user_email = 'someone@something.com'; $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.

Recommend

  • How to create separate log-in and registration pages in WooCommerce
  • Getting an array from object using SimpleXMLElement
  • How to create a Wordpress Shortcode that does nothing?
  • PyParsing: Is it possible to globally suppress all Literals?
  • Creating Wordpress like shortcode in laravel blade
  • Validate field and throw exception in JSF, but attach error message to another field?
  • JS: Prevent Unresponsive Script Warnings
  • Avoiding try/catch hell in my web pages
  • AngularJs ng-repeat filtering by a deeper tier of data
  • SQL Server 2008 R2 - Islands and Gaps [closed]
  • SQL Server: +(unary) operator on non-numeric Strings
  • Android application not restoring state when installed from .apk, works fine from eclipse
  • Efficient & Pythonic way of finding all possible sublists of a list in given range and the minim
  • Python cosine function precision [duplicate]
  • HttpClient: disabling chunked encoding
  • Trying to get the char code of ENTER key
  • Get one-time binding to work for ng-if
  • Regex thinks I'm nesting, but I'm not
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Volley JsonObjectRequest send headers in GET Request
  • Modifying destination and filename of gulp-svg-sprite
  • Fill an image in a square container while keeping aspect ratio
  • Importing jscolor library in angular 2
  • Rearranging Cells in UITableView Bug & Saving Changes
  • GridView Sorting works once only
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • embed rChart in Markdown
  • apache spark aggregate function using min value
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Sorting a 2D array using the second column C++
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app