-
Notifications
You must be signed in to change notification settings - Fork 891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changing the og:image for Custom Post Type archive pages #2416
Comments
In the meantime, I tried to create a filter using
|
I would expect that filter to work, would be my first thing to recommend. Try to dump your condition and see if it fires? What is resulting output in that case? Is it different image or no image at all? |
I've not found it possible to set an og:image for CPT archive pages. I confirmed via Xdebug that the following function does not fire on an archive page because the filter itself is not applied on archive pages. add_filter( 'wpseo_opengraph_image', 'prefix_filter_og_image', 10, 1 );
function prefix_filter_og_image( $img ) {
if( is_post_type_archive( 'my_cpt' ) )
$img = 'http://www.example.com/my-image.jpg';
return $img;
} The function does fire on other pages where the Social meta box is available in the Yoast UI. Ideally there would be way to do this in the admin UI, but at least applying the filter on archive pages would be a step forward. Thanks. |
This is the workaround I came up with using add_action( 'wp_head', 'prefix_add_og_image', 10, 1 );
function prefix_add_og_image( $img ) {
if( is_post_type_archive( 'my_cpt' ) ) {
echo '<meta property="og:image" content="http://example.com/my-image.jpg" />';
}
} |
Thanks for debugging. Great to hear that you have solved the problem. Unfortunately we're not able to implement this at short notice. Currently we're focusing on issues that affect many users. We've concluded that this issue is not experienced by many users, therefore we are not able to fix this short-term. However, if more users are affected by this bug, we'll of course revisit this issue. As you already have a solution for this issue, we would like to invite you to submit a patch. |
I believe the filter doesn't fire is because without an This is the workaround I used which takes advantage of
Personally, i think it would make a bit more sense to have a method similar to Still, it would be nice to have a method we could use to access this more directly. Maybe we could store multiple |
This is now causing a Fatal error. It would be amazing to understand how to achieve the same with the new 'wpseo_frontend_presenters' but docs seem lacking any examples on existing meta. |
You can hook into The filter is receiving an instance of an image container. You can use the method I've converted the example above to the new way: function example_cpt_archive_add_opengraph_image( $container ) {
if ( is_post_type_archive( 'example_cpt' ) ) {
$image_id = $some_id;
if ( $image_id ) {
$container->add_image_by_id( $some_id );
}
}
}
add_action( 'wpseo_add_opengraph_images', 'example_cpt_archive_add_opengraph_image', 29 ); Let me know if the given information is helpful. |
I see. Just copied the example, but forgot to check which method the filter is calling. It's fixed and should work:
|
It seems to be a filter now #15342 |
it blows my mind releasing such an upgrade without proper docs ... Also related to #1060 |
Sorry @andizer & thanks for bringing to my attention. I updated my example above with the right function name & with the filter switch. |
This has been changed again.
|
Hi i was strugling with this filter, wondering why my function was never fired : in fact this function is executed ONLY if you 've set a default image in the Yoast > social > facebook settings. Hope this help others |
@mistergraphx There's a workaround for this issue with |
As of v15.9 none of these solutions work unfortunately, at least not in the responses coming back from /wp-json/yoast/v1/get_head |
Hmm, I must admit we haven't documented this part very well. I'll ask our docs team to take a look at this usecase. From going through the code, I cannot see a reason why the filter FYI: We are working to add settings for this stuff in our Premium plugin. We've almost got that feature done and plan to ship it somewhere in the upcoming two to three months. |
Thanks for the reply.
I am trying to use it on a taxonomy archive page so
site.com/posttype/taxonomy I get the Facebook image from wp-admin general
yoast settings if it is set. Otherwise there is no og:image tag at all
appearing in the yoast get_head rest response.
I can post examples of with and without Facebook image set. My
understanding was that the filter wpseo_add_opengraph_images
<https://github.com/Yoast/wordpress-seo/blob/2117a8d186d92785b9656c02a412bb2e7523feb6/src/generators/open-graph-image-generator.php#L89>
Would change the Facebook image to something else for og:image but
behaviour is as I described.
…On Fri, 7 May 2021 at 13:27, Omar Reiss ***@***.***> wrote:
Hmm, I must admit we haven't documented this part very well. I'll ask our
docs team to take a look at this usecase.
From going through the code, I cannot see a reason why the filter
wpseo_add_opengraph_images
<https://github.com/Yoast/wordpress-seo/blob/2117a8d186d92785b9656c02a412bb2e7523feb6/src/generators/open-graph-image-generator.php#L89>
or the filter wpseo_add_opengraph_additional_images
<https://github.com/Yoast/wordpress-seo/blob/2117a8d186d92785b9656c02a412bb2e7523feb6/src/generators/open-graph-image-generator.php#L103>
wouldn't do the trick. Did you try one of thos @conkonig
<https://github.com/conkonig>? These should allow you to filter in any
opengraph image you like. @pocketcolin <https://github.com/pocketcolin>
is right that the wpseo_opengraph image only runs on images we have
detected for the url ourselves. So if no images have been detected, it will
not run.
FYI: We are working to add settings for this stuff in our Premium plugin.
We've almost got that feature done and plan to ship it somewhere in the
upcoming two to three months.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2416 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AC5GOU7KLKIPU3AC6BN2VGLTMPMCRANCNFSM4BDX2XIQ>
.
|
Hi, I think the best way to replace the og:image for a Custom post type is: `function fb_YOUR_CUSTOM_POST_TYPE_image( $tags ) {
} Best. |
@andresgomez-pulpo I'm afraid your answer isn't valid for this issue. Your function uses The function provided by @atlanteh on 4 Nov 2020 is the correct one. It still works correctly today, with Yoast SEO 20.4: |
Hi, I'd like to request the ability to change the og:image for an archive page, and in particular for a Custom Post Type archive page.
I'm using Genesis, and I have a CPT (generated by a plugin), and I enabled the Archive Settings sub-menu for it using this code (as described here):
WP-SEO automatically replaces the Genesis "SEO Settings" panel on this Archive Settings page, with its own SEO Settings panel, as shown here:
I would like to request an extra field in this "SEO Settings" panel for specifying a different og:image (ie. "Facebook Image") URL.
It would also be nice to be able to do the same for regular (non-CPT) archive pages too - unless that is already possible?
An alternative place for adding this new Facebook Image field could be at SEO > Titles & Metas > Post Types > "Custom Post Type Archives" (eg. under the respective Title and Meta Description fields there for each CPT Archive).
The text was updated successfully, but these errors were encountered: