• Resolved klivie

    (@klivie)


    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/

Viewing 1 replies (of 1 total)
  • Plugin Author Chad Butler

    (@cbutlerjr)

    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. 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() 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.