-
-
Save leandersikma/1c5507d2cbd29e580e94 to your computer and use it in GitHub Desktop.
<?php | |
// Issues with sharing posts on Facebook: http://www.passwordincorrect.com/issue-with-sharing-wordpress-posts-to-facebook/ | |
// Add this chunck of code in your functions.php or anywhere else in your theme files. | |
// Register action for post status transitions | |
add_action( 'transition_post_status' , 'purge_future_post', 10, 3); | |
// Check if the new transition is publish, for correctness you could check if $old_status == 'pending', but I want that every post (which is published) is cached again (just to be sure). | |
function purge_future_post( $new_status, $old_status, $post ) { | |
if($new_status == 'publish') { | |
purge_facebook_cache($post); | |
} | |
} | |
// Ping Facebook to recache the URL. | |
function purge_facebook_cache($post_id) { | |
$url = get_permalink($post_id); | |
$fb_graph_url = "https://graph.facebook.com/?id=". urlencode($url) ."&scrape=true"; | |
$result = wp_remote_post($fb_graph_url); | |
} | |
?> |
You need to add it to your theme's functions file (functions.php)
You do not need the opening and closing php tags as the functions file will already have it.
PS: Thanks @leandersikma for the code. Hopefully this resolves the issues I am had with Facebook incorrectly caching a post as a 404.
This works great but you might sometimes get the FB API rate-limiting you. If that happens you can create a Facebook app and use that to authenticate:
wp_remote_post(
'https://graph.facebook.com/',
[
'body' => [
'id' => get_permalink($post_id),
'scrape' => 'true',
'access_token' => FACEBOOK_APP_ID . '|' . FACEBOOK_APP_SECRET,
]
]
);
I just used your script within my Theme Functions.php (funtions.php) and my complete blog turned blank. It doesn't really matter what I do it won't come back up unless I skip a few pages back. I deleted the script and updated it but it's still blank. What now?
EDIT-
I managed to get rid of the script and website is working again. But still 404 page not found....
Can the same thing be used for Google+? This seems to work wonders, but g+ is acting wonky, so if an example with g+ could help, that'd be great!