• Hi,
    On my website I am using wp members plugin.
    I want to only display some content when a custom field is not empty.
    Field is called wpmem_rnr. It is hidden during registration and can be filled by an admin.
    Using the shortcode [wpmem_field field=”wpmem_rnr”] will show the field to the loggedin user.
    Now I want to echo a specific text, only when this field is filled.
    I was using:

    function show_coin () {
      if( get_field('wpmem_rnr') ) {
    	  echo get_field('wpmem_rnr') . ' Ja het werkt' ;
      }
    }

    But this does not do the trick.

    Any help will be appreciated.
    Regards,
    Arjan

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

    (@cbutlerjr)

    There is in fact a get_field() function in WP, but that is not at all how it is used (it’s for post information, not users).

    The function you’re looking for would be WP’s get_user_meta() which retrieves user meta (all WP-Members custom fields are going to be user meta in WP’s db schema).

    The get_user_meta() function needs the field meta key (‘wpmem_rnr’ in your case), but it also needs the user ID. Depending on how/where you’re using this, you’ll need to consider if the user is the currently logged in user (in which case you can use WP’s get_current_user_id()), or if you need to pick up the user ID from somewhere else. I’m going to assume you’re displaying to the currently logged in user:

    function show_coin() {
        $user_id = get_current_user_id();
        $field = get_user_meta( $user_id, 'wpmem_rnr', true );
        if ( $field && '' != $field ) {
            echo $field . ' Ja het werkt';
        }
    }

    Alternative to using get_user_meta(), WP-Members has an API function wpmem_get_user_meta() which always passes “true” for get_user_meta()‘s $single parameter (since any field data WP stores is going to be $single = true. This allows you to shorthand the usage by one less argument in the function:

    function show_coin() {
        $user_id = get_current_user_id();
        $field = wpmem_get_user_meta( $user_id, 'wpmem_rnr' );
        if ( $field && '' != $field ) {
            echo $field . ' Ja het werkt';
        }
    }
    • This reply was modified 3 years, 11 months ago by Chad Butler.
Viewing 1 replies (of 1 total)

The topic ‘Echo when certainfield is not empty’ is closed to new replies.