How to add custom variables in Yoast SEO
If you’re using Yoast SEO and ACF, you’ll certainly need to use ACF fields in Yoast to obtain a customised title and description.
Simply create a snippet variable and then integrate it into your Yoast SEO configuration. To do this, add a function to your plugin:
<?php // create custom variables for Yoast SEO, based on CPT & ACF add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables'); function register_custom_yoast_variables() { wpseo_register_var_replacement( '%%Catégorie du véhicule%%', 'get_rent_category', 'advanced', 'Catégorie du véhicule' ); wpseo_register_var_replacement( '%%Description du véhicule%%', 'get_rent_description', 'advanced', 'Description courte du véhicule' ); } function get_rent_category() { global $post; if($post&&($post->post_type=='rent')): $type = get_field('type', $post->ID); return $type['label']; endif; } function get_rent_description() { global $post; if($post&&($post->post_type=='rent')): $description = get_field('description', $post->ID); return wp_trim_words($description,18); endif; } ?>
Variables are now available in Yoast SEO:
You can find here the list of available snippet variables in Yoast SEO.