Skip Navigation

[Resolved] Types usermta not working in wp types beta version

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue was that the customer was using our

[types usermeta='wpcf_last_visited_class' user_current='true' output='raw'][/types]

To try and retrieve a custom user meta that he had created manually and unfortunately it does not work.

Solution:

If you manually inserted the custom field into the database without the use of Types then you will need a custom shortcode to retrieve the custom value.

I was able to write a shortcode to help retrieve this.

[get_user_field user='[wpv-current-user format="meta" info="id"]' field='wpcf_last_visited_class']

Add the following to your functions.php file

// Add Shortcode
function get_user_field( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'field' => '',
            'user' => '',
        ),
        $atts
    );
 
     
    $user_last = get_user_meta( $atts['user'], $atts['field'], $single ); 
    var_dump($user_last);
    return $user_last[0];
 
}
add_shortcode( 'get_user_field', 'get_user_field' );

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by vimalS 6 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#626171

I am trying to display usermeta field value using :

[types usermeta='wpcf_last_visited_class' user_current='true' output='raw'][/types]

But is displaying blank.
Thanks

#626298

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Vimal,

Thank you for contacting our support forum.

I tried this on my test site and it works perfectly fine. Are you trying to use this in a view that is listing out Users?

If not could you let me know where you're using this field.

Thanks,
Shane

#626370

I am using it in content template after user logs in..

#626650

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Vimal,

Would you mind providing me with admin access to the website so that I can have a look at this for you ?

Also please let me know which page the issue is on.

Thanks,
Shane

#627004

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Vimal,

Thank you for the credentials.

I took a look at this for you and it seems that the field itself does not exist on the user.
hidden link

This is why its showing up blank. Also this field [types usermeta='wpcf_last_visited_class' user_current='true' output='raw'][/types] doesn't exist as user meta.

Could you let me know where this field was created?

Thanks,
Shane

#627203
Screenshot_3.png

Hi Shane,

I have added that field when new user registers using functions.php file.
Look at the code here

add_user_meta($user_id,'wpcf_last_visited_class',0);
update_user_meta(get_current_user_id(), 'wpcf_last_visited_class', $classid);

Value is also there in database for user id = 6 , see attached screenshot.

Thanks!!

#627293

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Vimal,

Did this work before using the Beta ?

I suspect that since you had created the field not through types then this wouldn't work. What you can do is to use this shortcode instead and it will retrieve the field for you.

[wpv-current-user format='meta' info="wpcf_last_visited_class"]

Please let me know if this helps.
Thanks,
Shane

#627319

Did this work before using the Beta ? - No, I am using beta first time.

[wpv-current-user format='meta' info="wpcf_last_visited_class"]

Above code displaying user's first name and last name.

#627369

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Vimal,

I manage to write a custom shortcode to retrieve this for you and added it to your page.

[get_user_field user='[wpv-current-user format="meta" info="id"]' field='wpcf_last_visited_class']

This is the code i've added to your functions.php file.

// Add Shortcode
function get_user_field( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'field' => '',
			'user' => '',
		),
		$atts
	);

	
	$user_last = get_user_meta( $atts['user'], $atts['field'], $single ); 
	var_dump($user_last);
	return $user_last[0];

}
add_shortcode( 'get_user_field', 'get_user_field' );

Please let me know if this helps.
Thanks,
Shane

#627632

Yes, It is working