Created
March 14, 2016 20:10
-
-
Save AnnaMariaEriksson/e7f042dae9529b431a9d to your computer and use it in GitHub Desktop.
Get rid of ugly links on Facebook when sharing a post from WordPress via Publicize or some other fetching service.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment