Title: Shortcode errors if blank
Last modified: November 7, 2018

---

# Shortcode errors if blank

 *  Resolved [klivie](https://wordpress.org/support/users/klivie/)
 * (@klivie)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/shortcode-errors-if-blank/)
 * So I’m curious if there’s any kind of conditional check we can do against fields,
   when using the shortcode.
 * I’ve used the following to output a “title” field (a select field) within my 
   template file:
 * $title = do_shortcode(‘[wpmem_field title id=”‘.$user_id.'”]’);
 * However, this is throwing up the following error if the field is left as blank
   on the user:
 * Undefined index: in \plugins\wp-members\inc\class-wp-members-shortcodes.php on
   line 559
 * Obviously I still need to store the shortcode in my variable because some users
   have it and others don’t, but is there a way i can stop it from throwing up errors?
 * My default value is setup like so:
 * Please Select|,
 * As per documentation found at: [https://rocketgeek.com/plugins/wp-members/docs/registration/choosing-fields/](https://rocketgeek.com/plugins/wp-members/docs/registration/choosing-fields/)

Viewing 1 replies (of 1 total)

 *  Plugin Author [Chad Butler](https://wordpress.org/support/users/cbutlerjr/)
 * (@cbutlerjr)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/shortcode-errors-if-blank/#post-10857759)
 * Using do_shortcode() has to run a sizeable regex that cycles through every registered
   shortcode in your install. That’s unnecessary since WP already has a function
   to retrieve a user meta – get_user_meta().
 * `$title = get_user_meta( $user_id, 'title', true );`
 * WP pro tip – anytime you find yourself using do_shortcode(), dig into the core
   code of what you’re using – plugin, theme, etc – and find out what is really 
   being done. Then look for a better way to do it, either by calling the shortcode’s
   callback function directly, or some other method. WP-Members actually has an 
   API function wpmem_do_shortcode() that is a utility that allows you to run a 
   shortcode directly without using do_shortcode(). It’s based on a script that 
   I used in [an answer to a shortcode question on StackExchange](https://wordpress.stackexchange.com/questions/316546/shortcode-is-not-working-in-homepage-page-templatecustom-front-page/316549#316549).
   Anyone could use the function given in that answer, or if you’re using WP-Members,
   you can use wpmem_do_shortcode() for any shortcode, whether shortcode belongs
   to WP-Members or not. In your case, that would be as follows (I think):
 * `$title = wpmem_do_shortcode( 'wpmem_field', array( 'field'=>'title','id'=>$user_id));`
 * But even that isn’t necessary in this case because you can just use [get_user_meta()](https://codex.wordpress.org/Function_Reference/get_user_meta)
   directly (like I mentioned above).
 * Now… all of that being said, there may be some possibility for improvement in
   the plugin here. In /inc/class-wp-members-shortcodes.php at line 559, change 
   it to this:
 * `$result = ( isset( $field ) ) ? $user_info->{$field} : '';`
 * I’d be open to that as a potential change in the future.

Viewing 1 replies (of 1 total)

The topic ‘Shortcode errors if blank’ is closed to new replies.

 * ![](https://ps.w.org/wp-members/assets/icon-256x256.png?rev=1226414)
 * [WP-Members Membership Plugin](https://wordpress.org/plugins/wp-members/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-members/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-members/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-members/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-members/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-members/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Chad Butler](https://wordpress.org/support/users/cbutlerjr/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/shortcode-errors-if-blank/#post-10857759)
 * Status: resolved