Skip Navigation

[Resolved] Echo custom field in Yoast meta

This support ticket is created 5 years, 2 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Waqar 5 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#1339139

Hi Toolset,

I have this code:

// define the custom replacement callback
function get_job_availability() {
$job_availability = get_post_meta($post_id, 'wpcf-job-availability', true);
return $job_availability;
}
// define the action for register yoast_variable replacements
function register_custom_yoast_variables() {
wpseo_register_var_replacement( '%%job-availability%%', 'get_job_availability', 'advanced', 'some help text' );
}
// Add action
add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');

But it doesn't get the value from the custom field. Any idea how to solve this?

Thanks,
Menno

#1339457

Hi Menno,

Thank you for contacting us and I'd be happy to assist.

A few points related to your question:

1. The simple custom fields like a single line type can be used directly in snippet variables, in this format:


%%cf_<custom-field-name>%%

For example, if you have a Toolset custom field with the slug "job-availability", it can be used like this:


%%cf_wpcf-job-availability%%

Note: By default, Toolset custom fields are saved with a "wpcf-" prefix appended to their slugs.

2. However, for other complex type custom fields like radio buttons, checkboxes, etc, you'll need to register a custom template variable using the "wpseo_register_extra_replacements" hook, as you're using in your code snippet:
hidden link
hidden link

3. The code snippet that you've shared seems fine and the only thing missing is that it is using "$post_id" to pass on the current post's ID, but that is not defined in the function.

To make it use the current post's ID, you can replace the line:


$job_availability = get_post_meta($post_id, 'wpcf-job-availability', true);

With:


$job_availability = get_post_meta(do_shortcode('[wpv-post-id]'), 'wpcf-job-availability', true);

Note: To get the Toolset custom field values in your custom code, you can alternatively use "Types Fields API", instead of "get_post_meta" function:
https://toolset.com/documentation/customizing-sites-using-php/functions/

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1339795
Schermafbeelding 2019-09-13 om 15.10.14.png

Hi Waqar,

Thanks for your response!

%%cf_wpcf-job-availability%%

Yes, i use this with simple fields and it works. But for wpcf-job-availability i use a select field and with this field type it doesn't work.

// define the custom replacement callback
function get_job_availability() {
	$job_availability = get_post_meta(do_shortcode('[wpv-post-id]'), 'wpcf-job-availability', true);
    return $job_availability;
}
// define the action for register yoast_variable replacments
function register_custom_yoast_variables() {
    wpseo_register_var_replacement( '%%job-availability%%', 'get_job_availability', 'advanced', 'Job availability' );
}
// Add action
add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');

I have this code now and i use %%job-availability%% in the Yoast meta description but it doesn't show the value.

Regards,
Menno

#1339847

Hi Menno,

Thanks for writing back.

Are you checking the output of "%%job-availability%%" only in the snippet preview section on the post edit screen in the admin area or have you also checked it in the actual front-end markup?

Please note that a custom registered placeholder doesn't show the corresponding value in the snippet preview section, which is a limitation on the part of Yoast SEO plugin and is mentioned in the comments at:
hidden link

regards,
Waqar

#1339957

Hi Waqar,

I was testing the front-end markup. I looks like it's working now! This is my final code:

// define the custom replacement callback
function get_job_availability() {
	$job_availability = get_post_meta(do_shortcode('[wpv-post-id]'), 'wpcf-job-availability', true);
	if  (!empty( $job_availability )) {
    	return 'Availability: ' . $job_availability . '.';
    }
}

// define the action for register yoast_variable replacments
function register_custom_yoast_variables() {
    wpseo_register_var_replacement( '%%job-availability%%', 'get_job_availability', 'advanced', 'Job availability' );
}

// Add action
add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');

Thanks,
Menno

#1340725

Hi Menno,

Thanks for sharing the update and glad that it is working now.

For a new question/concern, you're welcome to start a new ticket and you can mark this one as resolved.

regards,
Waqar