-
Notifications
You must be signed in to change notification settings - Fork 877
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
Add a default social image for posts belonging to a taxonomy #21349
Comments
Hi @JosVelasco , Thank you for suggesting a new feature for one of our plugins! You are the first to have requested this feature! What's next? |
Is your feature request related to a problem?
Some websites could benefit from adding a default social image to a group of posts. The site-wide fallback image is not enough for big sites.
Describe the solution you'd like
When editing the Social media appearance tag of a taxonomy, add a checkbox to apply these settings to the posts that belong to this group of posts that haven't been assigned something.
Why do you think this feature is something we should consider for the Yoast SEO plugins?
It could complement the social appearance interface and work as a starting point for dynamically generating social images in the style of GitHub cards.
Additional context
I tried to accomplish something similar with this snippet with no luck.
`function custom_og_image( $image ) {
// Check for Yoast user-defined Facebook image
if ( empty( $image ) ) {
$image = get_post_meta( get_the_ID(), '_yoast_wpseo_opengraph_image', true );
}
// If there is no Yoast image, check for the featured image
if ( empty( $image ) ) {
$featured_image = get_the_post_thumbnail_url( get_post_thumbnail_id() );
if ( $featured_image ) {
$image = $featured_image;
}
}
// Check for social image in categories (recursive) with fallback to Yoast metafield for category
if ( empty( $image ) ) {
$image = get_category_image( get_the_category(), $image );
}
// Check for site-wide fallback image (replace 'fallback_image_url' with your actual URL)
if ( empty( $image ) && defined( 'FALLBACK_OG_IMAGE' ) ) {
$image = FALLBACK_OG_IMAGE;
}
return $image;
}
add_filter( 'wpseo_opengraph_image', 'custom_og_image' );
// Function to get social image from categories (recursive) using Yoast metafield
function get_category_image( $categories, $image ) {
foreach ( $categories as $category ) {
$cat_meta = get_term_meta( $category->term_id, 'yoast_wpseo_opengraph_image', true );
if ( $cat_meta ) {
$image = $cat_meta;
break; // Stop searching after finding an image
}
$parent = get_parent_category( $category );
if ( $parent ) {
$image = get_category_image( array( $parent ), $image );
}
}
return $image;
}`
The text was updated successfully, but these errors were encountered: