Skip to content

New filter: wpseo_schema_main_image_id#22499

Merged
thijsoo merged 1 commit intoYoast:trunkfrom
JoryHogeveen:patch-1
Sep 1, 2025
Merged

New filter: wpseo_schema_main_image_id#22499
thijsoo merged 1 commit intoYoast:trunkfrom
JoryHogeveen:patch-1

Conversation

@JoryHogeveen
Copy link
Copy Markdown
Contributor

@JoryHogeveen JoryHogeveen commented Aug 13, 2025

Context

Allow changing the main image ID through a filter.
I have a client project which has custom featured images for taxonomies, many other tools, like WooCommerce also do this similarly.
This PR allows me to change the schema output correctly without doing more skechy changes to the full schema output using the wpseo_schema_webpage filter.

Summary

This PR can be summarized in the following changelog entry:

  • Allow changing the main image ID through a filter wpseo_schema_main_image_id. Props to @JoryHogeveen.

Relevant technical choices:

  • I based my changes on other filters already available in this class.

Test instructions

Test instructions for the acceptance test before the PR gets merged

This PR can be acceptance tested by following these steps:

  • Simply add a filter and change the main image ID to see changed in schema validators.

Filter example for when the category taxonomy has a custom image field named featured_image that returns the image ID:

add_filter( 'wpseo_schema_main_image_id', function( $image_id ) {
    if ( ! is_category() ) {
        return $image_id;
    }

    $cat_image_id = get_term_meta( get_queried_object_id(), 'featured_image', true );

    return $cat_image_id ?: $image_id;
} );

Relevant test scenarios

  • Changes should be tested with the browser console open
  • Changes should be tested on different posts/pages/taxonomies/custom post types/custom taxonomies
  • Changes should be tested on different editors (Default Block/Gutenberg/Classic/Elementor/other)
  • Changes should be tested on different browsers
  • Changes should be tested on multisite

No real relevant test scenarios I can think of other than a single filter change so make sure the results are as expected.

Test instructions for QA when the code is in the RC

  • QA should use the same steps as above.

QA can test this PR by following these steps:

  • Upload a new image and go to the wp_posts table and mark down its ID. For me after uploading an image it was 1031.
  • Create a post and assign a featured image to it that is not you newly uploaded image.
  • Publish and open the post and take a look at the schema. Take note of the Top level ImageObject schema. This will change with this filter.
  • Add the following filter to your site:
add_filter( 'wpseo_schema_main_image_id', function( $image_id ) {
	return <YOURID>;
} );
  • Take note the schema changes to the url of your newly uploaded image.

Impact check

This PR affects the following parts of the plugin, which may require extra testing:

None

UI changes

  • This PR changes the UI in the plugin. I have added the 'UI change' label to this PR.

Other environments

  • This PR also affects Shopify. I have added a changelog entry starting with [shopify-seo], added test instructions for Shopify and attached the Shopify label to this PR.

Documentation

  • I have written documentation for this change. For example, comments in the Relevant technical choices, comments in the code, documentation on Confluence / shared Google Drive / Yoast developer portal, or other.
  • I have added filter doc comments, similar as done with other filters in this class.

Quality assurance

  • I have tested this code to the best of my abilities.
  • During testing, I had activated all plugins that Yoast SEO provides integrations for.
  • I have added unit tests to verify the code works as intended.
  • If any part of the code is behind a feature flag, my test instructions also cover cases where the feature flag is switched off.
  • I have written this PR in accordance with my team's definition of done.
  • I have checked that the base branch is correctly set.

Innovation

  • No innovation project is applicable for this PR.
  • This PR falls under an innovation project. I have attached the innovation label.
  • I have added my hours to the WBSO document.

Fixes #

@JoryHogeveen
Copy link
Copy Markdown
Contributor Author

Not sure why the checks are failing as the reason seems unrelated to my changes.

@thijsoo thijsoo self-assigned this Aug 27, 2025
@thijsoo thijsoo added the changelog: non-user-facing Needs to be included in the 'Non-userfacing' category in the changelog label Aug 27, 2025
Copy link
Copy Markdown
Contributor

@thijsoo thijsoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your submission @JoryHogeveen I will merge this so it can go into a next release. It should land in 26.0.

@thijsoo thijsoo added this to the 26.0 milestone Sep 1, 2025
@thijsoo thijsoo merged commit a19ebb3 into Yoast:trunk Sep 1, 2025
23 of 33 checks passed
@JoryHogeveen
Copy link
Copy Markdown
Contributor Author

Hi Thijs, perfect, thank you!

@enricobattocchi enricobattocchi added changelog: other Needs to be included in the 'Other' category in the changelog and removed changelog: non-user-facing Needs to be included in the 'Non-userfacing' category in the changelog labels Sep 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: other Needs to be included in the 'Other' category in the changelog community-patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants