The mbp_autopost_post_args
filter allows you to alter the post arguments sent to Google My Business when the plugin creates an automatic post. It has 2 parameters, $args
containing the post data and $location
containing the GMB location ID.
function do_something_with_the_post_args($args, $location){
//Alter the post arguments
return $args;
}
add_filter('mbp_autopost_post_args', 'do_something_with_the_post_args', 10, 2);
Out of the box, Post to Google My Business will only auto-post “What’s New” posts. But using this filter you can make it automatically create product, event and offer posts. This is very useful if you have a plugin that manages such content and you want to automatically publish it to Google My Business.
Samples
Strip Visual Composer, Divi and other shortcodes
Visual editors such as Divi, Visual Composer and others often clutter the post content with shortcodes. The following snippet will strip out the shortcodes.
function autopost_strip_shortcodes($args, $location){ $args['summary'] = preg_replace("~(?:\[/?)[^\]]+/?\]~s", '', $args['summary']); return $args; } add_filter('mbp_autopost_post_args', 'autopost_strip_shortcodes', 10, 2);
Fetch GMB image from post content
With this snippet the plugin will fetch the first image from the post content and use that as the GMB post image. This will only work for images that have been directly uploaded to that specific post (selecting a previously used image from the Media Library will not work).
function mbp_get_image_from_content($args, $location){ global $post; //Get the current post being edited $images = get_attached_media('image', $post->ID); //Retrieve the images from this post if(!$image = reset($images)){ return $args; } //If there are no images attached to the post, return unaltered gmb post arguments, or populate $image with the first image that is found $image_details = wp_get_attachment_image_src($image->ID, 'large'); //Retrieve details for large thumbnail of the image $args['media'] = [ 'mediaFormat' => 'PHOTO', 'sourceUrl' => reset($image_details) ];//Alter the image URL being sent to Google return $args; //Return the altered arguments } add_filter('mbp_autopost_post_args', 'mbp_get_image_from_content', 10, 2);
Remove call to action button
function autopost_remove_button($args, $location){
unset($args['callToAction']);
return $args;
}
add_filter('mbp_autopost_post_args', 'autopost_remove_button', 10, 2);
Alter call to action URL
function autopost_change_button_url($args, $location){
$args['callToAction']['url'] = 'https://example.com';
return $args;
}
add_filter('mbp_autopost_post_args', 'autopost_change_button_url', 10, 2);
Use Yoast OpenGraph image as GMB post image
function autopost_use_yoast_og_image($args, $location){ $args['media']['sourceUrl'] = esc_url($_POST['yoast_wpseo_opengraph-image']); return $args; } add_filter('mbp_autopost_post_args', 'autopost_use_yoast_og_image', 10, 2);
Only publish posts with specific tag
Only publishes the WordPress post to GMB when the gmbpost tag is present
function only_post_from_specific_tag($args, $location){ global $post; if(!has_tag('gmbpost', $post)){ return false; } return $args; } add_filter('mbp_autopost_post_args', 'only_post_from_specific_tag', 10, 2);
Only publish posts within a specific category
Only publishes the WordPress post to GMB when it is in the gmbpost category
function only_post_within_specific_category($args, $location){ global $post; if(!in_category('gmbpost', $post)){ return false; } return $args; } add_filter('mbp_autopost_post_args', 'only_post_within_specific_category', 10, 2);