• Resolved veganwebagency

    (@veganwebagency)


    Hi,

    Is it possible that we set a default og:image in the Yoast settings and replace it depending on whether a header image is selected via ACF or not?

    Is there a filter for this?

    That we could e.g. query the content type and see if there is set a header image via acf or not, if yes replace the og:image and otherwise use the default.

    Thanks for a short response,
    Chrigi

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Maybellyne

    (@maybellyne)

    Hello Chrigi,

    Thanks for reaching out regarding your og:image. To clarify your request, do you mean you want to use an image set in ACF as og:image? And if that does not exist, it should then default to whatever image you have set in the Yoast SEO plugin?

    Looking forward to your clarification.

    Thread Starter veganwebagency

    (@veganwebagency)

    Hi Maybellyne,

    Exactly.

    Optimally it would query the following in that order:
    #1 Is an og:image placed on the page / post under Yoast SEO -> Then take this one.
    #2 Is a header image placed with ACF -> Then take this one
    #3 If neither is placed, then take the default image under Yoast SEO settings.`

    So in code it would be something like:

    if ($yoast_ogimage) {
        // Take og:image from Yoast SEO page / post settings
    
    } else if (get_field('header_image')) {
        // Take headerimage as og:image
    
    } else {
        // Standard Yoast SEO og:image
    }

    Best Regards,
    Chrigi

    Plugin Support Maybellyne

    (@maybellyne)

    Thanks for the clarification, Chrigi.

    By default, Yoast SEO does not have the ability to select images from an ACF image field to be used as og:image and we thank you for suggesting a new feature for one of our plugins. Though we’re not working on it at the moment, you can follow the feature request at https://github.com/Yoast/yoast-acf-analysis/issues/205.

    Furthermore, you can use the wpseo_opengraph_image filter to customize this feature. An example code snippet to give you an idea of how to use the filter can be found here:
    https://gist.github.com/amboutwe/811e92b11e5277977047d44ea81ee9d4

    Thread Starter veganwebagency

    (@veganwebagency)

    Hi Maybellyne

    Thanks for the links, worked for me now.

    Here is my final code:

    function change_opengraph_image_url($url)
    {
    	if (get_field('social_media_image_choice') && !is_archive() && !is_404()) :
    		$url = wp_get_attachment_image_src(get_field('social_media_image'), 'size_1200_630')[0];
    		return $url;
    	elseif (get_field('header_active') && !is_archive() && !is_404()) :
    		$url = wp_get_attachment_image_src(get_field('header_image'), 'size_1200_630')[0];
    		return $url;
    	else :
    		return $url;
    	endif;
    }
    add_filter('wpseo_opengraph_image', 'change_opengraph_image_url');
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change og:image to acf field’ is closed to new replies.