-
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
Yoast wpseo_opengraph_image filter doesn't run if no default image #1060
Comments
Thanks for the reply jrfnl. The page is really just a "hook" for a plugin that then delivers the appropriate page content based on rewrite rules. So it's one page, but it's really hundreds of pages and a handful of different templates. Based on different conditions (mostly templates) I need to show different og:images based on rule-driven paths. |
Well, the filter is meant to filter (=change) a variable, not to add one. It might be an idea to add an action where you can add images yourself, but as this is the only request for something like that that I've come across, I'm hesitant to do so, especially as this can be solved easily in another way. You can solve your particular issue by calling the WPSEO image output function directly from your own function using code along the lines of the below. Hope this helps. function my_own_og_function() {
/*
Put the logic to determine which image to display here (set the $my_image_url variable)
*/
$GLOBALS['wpseo_og']->image_output( $my_image_url ); // This will echo out the og tag in line with other WPSEO og tags
}
add_action( 'wpseo_opengraph', 'my_own_og_function', 29 ); |
Just checking: did my suggestion solve your issue ? If so, let's close it. |
Hi, sorry for slow reply. Just tested. Works like a champ. Thank you. For future reference in case it comes up for anyone else, I did a variation of the code you provided using an anonymous function. In this way $my_image_url can be set outside of the function. var $my_image_url = 'http:https://www.mydomain.com/image.png';
add_action('wpseo_opengraph', function() use($my_image_url ){
$GLOBALS['wpseo_og']->image_output( $my_image_url ); ;
}); |
Looks good, glad to hear it works. Just as a little sitenote: this does make your theme incompatible with the WP minimum requirements as PHP5.2.x does not support anonymous functions, but I presume you knew that already. |
This doesn't seem to work anymore in version 2.1.1. function my_own_og_function() {
$GLOBALS['wpseo_og']->image_output( $my_image_url );
}
add_action( 'wpseo_opengraph', 'my_own_og_function', 29 ); Now outputs the meta property og:image three times like this: <meta property="og:image" content="http:https://domain.com/media/default.jpg" />
<meta property="og:image" content="http:https://domain.com/media/my-custom-ogimage.jpg" />
<meta property="og:image" content="http:https://domain.com/media/default.jpg" /> Resulting in Facebook displaying the default image instead of the custom one (where default.jpg is the default image set in the social facebook settings of the plugin). I try to see what changed in the plugin function, but can't seem to find it. add_action( 'wpseo_opengraph', array( $this, 'image' ), 30 ); Any ideas anyone? |
As far as I follow this thread it's inherently about producing custom output, not changing existing one. If you want to prevent native output from showing you could probably achieve that by unhooking respective method: |
Correct, it should change the existing og:image, not add one. |
It might have been due to order of output, not preventing output. It's somewhat puzzling why do you have default twice there, the single native one at priority 30 should be after your custom output at priority 29. |
But then Facebook will still display the single native one, it should replace the default image entirely when custom output is present? |
I am not following what exactly is your desired output. I think between being able to remove plugin's own output and to add your own — from technical point of view you can have sufficient control over it. If you think code has issues or could be improved in some way please elaborate, beyond that I am afraid is getting into support territory and not in scope of this channel. |
Ok thanks. I used to be able to 'swap' out the og:image with the I just switched back to the /*
* Replace facebook og:image based upon page type
* Only works if default image is set in Social Media > Facebook settings
*/
add_filter('wpseo_opengraph_image', function() use($ogimage){
global $wp_query;
$post_id = get_the_ID();
// Homepage
if(is_front_page()) {
$wp_query = new WP_Query( array(
'post_type' => 'project',
'meta_key' => 'project_date',
'meta_query' => array(
'key' => 'project_date',
'type' => 'date'
),
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
$first_project = $wp_query->posts[0];
$featured = get_field('project_feature', $first_project->ID);
if ($featured == 'image') $projectimage = get_field('project_image', $first_project->ID);
$ogimage = $projectimage['sizes']['large'];
wp_reset_query();
// Project archive
} elseif(is_post_type_archive('project')) {
// Get client projects
$clientslug = get_query_var('klant');
if ($clientslug) {
$args = array(
'name' => $clientslug,
'post_type' => 'client'
);
$clients = get_posts($args);
if ($clients) {
$clientid = $clients[0]->ID;
$wp_query = new WP_Query( array(
'connected_type' => 'project-client',
'connected_items' => $clientid,
'meta_key' => 'project_date',
'meta_query' => array(
'key' => 'project_date',
'type' => 'date'
),
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
}
} else {
// Get all projects
$wp_query = new WP_Query( array(
'post_type' => 'project',
'meta_key' => 'project_date',
'meta_query' => array(
'key' => 'project_date',
'type' => 'date'
),
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
}
$first_project = $wp_query->posts[0];
$featured = get_field('project_feature', $first_project->ID);
if ($featured == 'image') $projectimage = get_field('project_image', $first_project->ID);
$ogimage = $projectimage['sizes']['large'];
wp_reset_query();
// Discipline archive
} elseif(is_tax('discipline')) {
$term = $wp_query->get_queried_object();
if (ICL_LANGUAGE_CODE != 'nl') {
remove_all_filters('get_term');
$termid_org = icl_object_id($term->term_id, 'discipline', true, 'nl');
$term = get_term($termid_org, 'discipline');
}
$clientslug = get_query_var('klant');
// Get client term projects
if ($clientslug) {
$args = array(
'name' => $clientslug,
'post_type' => 'client'
);
$clients = get_posts($args);
if ($clients) {
$clientid = $clients[0]->ID;
$wp_query = new WP_Query( array(
'connected_type' => 'project-client',
'connected_items' => $clientid,
'discipline' => $term->slug,
'meta_key' => 'project_date',
'meta_query' => array(
'key' => 'project_date',
'type' => 'date'
),
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
}
} else {
// Get all term projects
$wp_query = new WP_Query( array(
'post_type' => 'project',
'discipline' => $term->slug,
'meta_key' => 'project_date',
'meta_query' => array(
'key' => 'project_date',
'type' => 'date'
),
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
}
$first_project = $wp_query->posts[0];
$featured = get_field('project_feature', $first_project->ID);
if ($featured == 'image') $projectimage = get_field('project_image', $first_project->ID);
$ogimage = $projectimage['sizes']['large'];
wp_reset_query();
// Client archive
} elseif(is_post_type_archive('client')) {
$wp_query = new WP_Query( array(
'post_type' => 'client',
'orderby' => 'name',
'order' => 'ASC',
'nopaging' => true
));
$first_client = $wp_query->posts[0];
$clientimage = get_field('client_image', $first_client->ID);
$ogimage = $clientimage['sizes']['large'];
wp_reset_query();
// Publication archive
} elseif(is_post_type_archive('publication')) {
$wp_query = new WP_Query( array(
'post_type' => 'publication',
'meta_key' => 'publication_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
$first_pub = $wp_query->posts[0];
$featured = get_field('publication_feature', $first_pub->ID);
if ($featured == 'image') $pubimage = get_field('publication_image', $first_pub->ID);
$ogimage = $pubimage['sizes']['large'];
wp_reset_query();
// Single project
} elseif (is_singular('project')) {
$featured = get_field('project_feature', $post_id);
if ($featured == 'image') $projectimage = get_field('project_image');
$ogimage = $projectimage['sizes']['large'];
// Single client
} elseif(is_singular('client')) {
$clientimage = get_field('client_image', $post_id);
$ogimage = $clientimage['sizes']['large'];
// Single publication
} elseif(is_singular('publication')) {
$featured = get_field('publication_feature', $post_id);
if ($featured == 'image') $pubimage = get_field('publication_image');
$ogimage = $pubimage['sizes']['large'];
}
// Use default if custom image is not set
if ($ogimage) {
//$GLOBALS['wpseo_og']->image_output($ogimage);
} else {
$ogimage = $GLOBALS['wpseo_og']->options['og_default_image'];
}
return $ogimage;
}); |
So this is still an issue in 2019 but there is a workaround. You must have a Facebook image set in your Yoast Settings (Yoast->Social->Facebook->Image URL), if you don't then the image hooks will not fire (unless you do indeed have a featured image on the post). Once that default image is set you can use the hook to serve whatever image you would like. Here is my version for example:
|
You can do it with the action
The action is located here: class-opengraph-image.php line 581
|
@santerref Any reason why you don't use the action |
This bug still persists for me. wpseo_opengraph_image only worked after adding a FB default image. |
It's 2021 and this is still an issue 7 years later. The docs talk about the In case anyone else lands here looking for a solution to this issue, here's my workaround that utilizes a combination of the answer @santerref provided with the original
|
Removes the prepare hook in the package.json.
Confirmed; it's now almost 12/2021 and the issue still exist.... @toddlevy |
+1 bug still exists, @pocketcolin workaround works! |
2023 and this bug still exists... @pocketcolin's workaround is still working as of version 20.8 |
We blew past the 🎂🎉 10 year anniversary 🎂🎉 of this issue without any fanfare or celebration! Confirming that @pocketcolin's workaround still works. |
WordPress SEO Version 1.5.2.7
Wordpress 3.8.x
Attempting to add an og:image tag via the wpseo_opengraph_image using the following code...
This is on a page template for a page that has no featured image.
If there is no default image URL on the Facebook tab of the social settings, the filter does NOT run.
If there IS a default image URL, the filter DOES run.
Would like to be able to hook into this filter without having to specify a default image URL.
The text was updated successfully, but these errors were encountered: