如何将WordPress选项设置转为自定义字段以适配Yoast页面标题
Hey there, let's get that global dealcity option showing up in your Yoast page titles. The core issue here is that Yoast's %%cf_dealcity%% variable only works with post-specific custom fields (stored in wp_postmeta), while your dealcity is a site-wide option (stored in wp_options). Even if you tried manually adding it as a custom field, that'd only apply to individual posts—not globally.
Here are two solid solutions to fix this:
Option 1: Create a Custom Yoast Variable
This lets you add a dedicated %%dealcity%% variable to Yoast's replacement options, just like the built-in ones. Add this code to your theme's functions.php file (or a custom plugin for better portability):
// Add custom replacement logic for our dealcity option add_filter('wpseo_replacements', 'add_dealcity_yoast_variable', 10, 2); function add_dealcity_yoast_variable($replacements, $type) { // Target only title replacements (remove this check to use in meta descriptions too) if ($type === 'title') { $dealcity_value = get_option('dealcity'); // Sanitize the value for safe use in titles $replacements['%%dealcity%%'] = !empty($dealcity_value) ? esc_html($dealcity_value) : ''; } return $replacements; } // Register the variable in Yoast's backend so you can select it easily add_filter('wpseo_available_replacements', 'register_dealcity_yoast_variable'); function register_dealcity_yoast_variable($available_replacements) { $available_replacements['dealcity'] = 'Global Deal City Option'; return $available_replacements; }
How to use it:
- Save the code and refresh your WordPress admin.
- Go to any page/post's Yoast SEO settings, edit the title field.
- You'll now see
%%dealcity%%in the list of available variables—insert it wherever you want the value to appear.
Option 2: Directly Modify the Yoast Title Output
If you prefer a more straightforward approach without adding a new variable, use the wpseo_title filter to inject the dealcity value directly into the final title string:
add_filter('wpseo_title', 'insert_dealcity_into_title'); function insert_dealcity_into_title($original_title) { $dealcity_value = get_option('dealcity'); if (!empty($dealcity_value)) { // Example 1: Replace a placeholder in your Yoast title (e.g., {dealcity}) $modified_title = str_replace('{dealcity}', esc_html($dealcity_value), $original_title); // Example 2: Prepend the dealcity value to the title // $modified_title = esc_html($dealcity_value) . ' | ' . $original_title; return $modified_title; } // If no dealcity value exists, return the original title return $original_title; }
How to use it:
- Pick either example (or adjust to your needs) and add the code to
functions.php. - In your Yoast title settings, use
{dealcity}as a placeholder (if using Example 1) or let the code automatically prepend the value (Example 2).
Important Notes:
- Always backup your
functions.phpbefore editing it, or use a child theme to avoid losing changes when your parent theme updates. - Both methods pull the global
dealcityvalue directly viaget_option('dealcity'), so any updates to your custom settings page will automatically reflect in the Yoast titles.
内容的提问来源于stack exchange,提问作者chrislovessushi




