You can use our API filter hook aiosp_opengraph_meta to edit social meta programatically.
https://semperplugins.com/documentation/aiosp_opengraph_meta/
Thanks Michael!
I tried this hook. It works fine for url (as shown in the example) but not for og:image. At least not the way I tried:
if( $field == 'image' ) $value = 'http://mynewurl.com';
return $value;
This return nothing. What field name should I use for Open Graph images?
-
This reply was modified 6 years, 6 months ago by
jrvcosta.
@jrvcosta the field name is thumbnail and twitter_thumbnail for the Twitter image.
Thank you very much! Now it worked.
I just don’t appreciate one thing: this hook override all social settings. If I save a different og:image link in All in One SEO Image Settings (on a particular post/page) this will be ignored. I would prefer if it change settings only if those field settings were empty in a post/page.
That can be done with the code using the filter. The filter hook is just a tool, how you want to customize the usage of it is up to you.
I understand Michael. Thank you again.
Just one more question, please: how do I tell the filter to fill a field name only if it’s empty? Certainly $field == '' will not work because $field is already 'thumbnail'.
-
This reply was modified 6 years, 6 months ago by
jrvcosta.
-
This reply was modified 6 years, 6 months ago by
jrvcosta.
@jrvcosta just check if $value is empty. For the OG:image you’ll also want to check if the it isn’t the default image.
All Ok now! Thank you, guys!
And to share with other colleagues, here´s the code that works for me:
add_filter('aiosp_opengraph_meta','aioseop_change_settings', 10, 5);
function aioseop_change_settings ( $value, $type, $field, $v, $extra_params )
{
if ( in_category('my-category') ) {
if (!has_post_thumbnail() && $field == 'thumbnail' && $value == '') $value = 'https://www.mydomain.com/wp-content/uploads/new-og-image.jpg';
if ( $field == 'width' && $value == '' ) $value = '1200';
if ( $field == 'height' && $value == '' ) $value = '630';
return $value;
}
}