Update: SEO with WordPress and ACF

<h3 class="entry-title">Update: SEO with WordPress and ACF</h3>
Posted on



Background

In my previous post, Dual-Language Breadcrumbs for WordPress, I briefly detailed how I implemented a dual language site with rewrites which necessitated a custom implementation of breadcrumbs to help provide to Search Engines the hierarchy of my content.

It's been about a month since that post and results can already be seen. That being said, there are some issues that have arisen that I was not aware of before. Take a look at the screenshots and explanations below.

Spanish Search Query

In this first example, the search query is araña saltadora en cuenca ecuador which should return results for the Spanish version of the site. The URL does display the Spanish version as well as the Breadcrumbs. However, both Titles are display an English variation.

If you follow the first link it navigates to https://encuencaecuador.com/bugs/spiders/ which is the category page for Spiders ... but the English version. It should ideally link to https://encuencaecuador.com/bichos/arañas instead.





The second result navigates to the appropriate path and langauge.

English Search Query




The query orbweaver in cuenca ecuador displayed better results. The only error is that the Spanish domain of the site is also returned. Upon inspection it became apparent that the Title of the page is loading the English variant

  <title>Marbled Orbweaver – In Cuenca Ecuador</title>



Next Steps

In attempts to address this, I am planning on using ACF to create custom fields for the Page Title, Description and Keywords. I'll hook into wp_head
action and the pre_get_document_title filter to accomplish this

Tweaking Keywords and Description

/**
 * SEO related tags. These depend on ACF being installed but does have a 
 * fallback just in case it doesnt. PHP short circuits in conditional evaluations 
 * and will not call the get_field() method if the plugin is not installed.
 */
function _set_meta_tag()
{
    $output = '';
    if ( class_exists('ACF') && get_field('page_description')) {
        $output .= '<meta property="description" content="' . get_field('page_description') . '" />';
    } else {
        $output .= '<meta property="description" content="' . get_bloginfo('description') . '" />';
    }
    if ( class_exists('ACF') && get_field('page_keywords')) {
        $output .= '<meta property="keywords" content="' . get_field('page_keywords') . '" />';
    } else {
        $output .= '<meta property="keywords" content="' . get_bloginfo('name') . '" />';
    }
    echo $output;
}
add_action('wp_head', '_set_meta_tag');

Tweaking the Page Title

/**
 * Allow the page/post titles to be configured via ACF in the backend. If ACF doesnt exist or
 * the field has not been defined for the page/post fallback to the blog name and description
 * as defined in the options
 */
add_filter('pre_get_document_title', function ($title) {
    if (class_exists('ACF') && get_field('page_title')) {
        $title = get_field('page_title') . ' | ' . get_bloginfo('name');
    } else {
        $title = get_bloginfo('name') . ' ' . get_bloginfo('description');
    }
    return $title;
}, 999, 1);