search ]

Yoast SEO Filters: Customizing SEO Data in WordPress

Yoast SEO is one of the most popular WordPress plugins for optimizing website content for search engines. However, developers often need to customize the data Yoast generates, such as titles, meta descriptions, or breadcrumbs, to fit specific use cases.

In this guide, we’ll explore how to use Yoast filters to manipulate SEO data dynamically in WordPress themes.

What Are Yoast Filters?

Filters in WordPress allow you to modify or add to the output of WordPress functions. Yoast SEO provides several filters that developers can hook into, making it possible to customize the SEO data it generates.

Common filters include wpseo_title for modifying titles and wpseo_metadesc for customizing meta descriptions.

Tip: Filters are an essential part of WordPress development. Understanding them can significantly expand your customization capabilities.

Best Practices for Using Yoast Filters

When using Yoast filters, adhering to best practices ensures that your customizations are effective, maintainable, and error-free.

Filters provide significant flexibility, but they also require careful implementation to avoid unintended consequences, such as incorrect SEO data or performance issues. The following tips will help you optimize your use of Yoast filters for the best results:

  • Use Conditionals: Leverage WordPress conditionals like is_singular() or is_category() to target specific pages or content types.
  • Test Changes: Use browser developer tools to inspect titles and meta tags.
  • Use Debugging Tools: Validate Open Graph or schema changes using tools like Google’s Rich Results Test.

Practical Examples for using Yoast Filters

Yoast SEO provides filters to customize titles, meta descriptions, canonical URLs, and more. Below are practical examples to help you tailor its output to your SEO strategy.

1. Modifying SEO Titles

Customizing the titles generated by Yoast can help improve clarity or align them with specific SEO strategies. Below are examples of how to achieve this:

Example 1: Append the Site Name to Titles

function custom_yoast_title( $title ) {
    return $title . ' | My Custom Site';
}
add_filter( 'wpseo_title', 'custom_yoast_title' );

Explanation: This appends the site name to all titles generated by Yoast, ensuring consistent branding.

Example 2: Add Pagination Information

function custom_pagination_title( $title ) {
    if ( is_paged() ) {
        $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
        return $title . ' - Page ' . $paged;
    }
    return $title;
}
add_filter( 'wpseo_title', 'custom_pagination_title' );

Explanation: This function appends the current page number to the title on paginated content.

2. Customizing Meta Descriptions

Meta descriptions play a vital role in search engine click-through rates. Here’s how to customize them:

Example: Add Category Descriptions

function custom_meta_description( $description ) {
    if ( is_category() ) {
        $category = get_queried_object();
        return $category->description ? $category->description : $description;
    }
    return $description;
}
add_filter( 'wpseo_metadesc', 'custom_meta_description' );

Explanation: This dynamically replaces the meta description with the category description on category archive pages.

3. Adding Open Graph Titles for Social Sharing

Open Graph data ensures that your content appears correctly when shared on social platforms like Facebook or Twitter.

function custom_og_title( $og_title ) {
    if ( is_single() ) {
        return get_the_title() . ' - Shared on My Site';
    }
    return $og_title;
}
add_filter( 'wpseo_opengraph_title', 'custom_og_title' );

Explanation: This function modifies the Open Graph title specifically for single posts.

Pro Tip: Always test your Open Graph data using Facebook’s Sharing Debugger or Twitter’s Card Validator.

4. Excluding Yoast Data on Specific Pages

function exclude_yoast_meta( $meta ) {
    if ( is_page_template( 'landing-page.php' ) ) {
        return '';
    }
    return $meta;
}
add_filter( 'wpseo_metadesc', 'exclude_yoast_meta' );

Explanation: This removes Yoast meta descriptions from pages using a specific template, such as landing pages.

5. Adding Custom Canonical URLs

function custom_canonical_url( $canonical ) {
    if ( is_singular( 'custom_post_type' ) ) {
        return 'https://example.com/custom-url/';
    }
    return $canonical;
}
add_filter( 'wpseo_canonical', 'custom_canonical_url' );

Explanation: This sets a custom canonical URL for posts of a specific post type.

Yoast filters can be invaluable for addressing real-life scenarios where default SEO settings need customization. Here are a couple of additional use cases where you might need to use these filters:

6. Modifying Breadcrumbs for Specific Pages

Breadcrumbs improve navigation and provide context to users and search engines. You might want to customize them for specific pages or post types.

function custom_breadcrumbs( $breadcrumbs ) {
    if ( is_page( 'about-us' ) ) {
        $breadcrumbs[1]['text'] = 'Who We Are';
    }
    return $breadcrumbs;
}
add_filter( 'wpseo_breadcrumb_links', 'custom_breadcrumbs' );

Explanation: This code modifies the breadcrumb text for the “About Us” page to make it more descriptive.

7. Dynamically Adjusting Robots Meta Tags

For SEO purposes, you might need to prevent specific pages from being indexed or followed.

function custom_robots_meta( $robots ) {
    if ( is_post_type_archive( 'events' ) ) {
        $robots['index'] = 'noindex';
        $robots['follow'] = 'nofollow';
    }
    return $robots;
}
add_filter( 'wpseo_robots', 'custom_robots_meta' );

Explanation: This function adds “noindex” and “nofollow” directives for the archive of a custom post type called “events,” ensuring it doesn’t appear in search engine results.

8. Adding Custom Open Graph Description for Events

If you manage events on your site, having a custom Open Graph description can improve how these events appear when shared on social media.

function custom_event_og_description( $og_description ) {
    if ( is_singular( 'events' ) ) {
        return 'Join us for this amazing event happening soon!';
    }
    return $og_description;
}
add_filter( 'wpseo_opengraph_desc', 'custom_event_og_description' );

Explanation: This filter dynamically adds a custom Open Graph description for single event posts to attract social media clicks.

Conclusion

Yoast filters provide a powerful way to customize the SEO data generated by your WordPress site. Whether you’re modifying titles, meta descriptions, or Open Graph data, these examples showcase the flexibility of Yoast SEO and WordPress filters.

Explore these techniques to enhance your website’s SEO strategy and deliver tailored content to search engines and social platforms. If you have questions or customizations you’d like to share, feel free to leave a comment below!

Roee Yossef
Roee Yossef

I develop pixel-perfect custom WordPress themes, delivering high-performance, SEO-optimized websites. Have a project in mind? need assistance? Feel free to contact me!

0 Comments...

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!