Skip to content

Instantly share code, notes, and snippets.

@coltjones
Created October 29, 2014 22:56
Show Gist options
  • Save coltjones/449389c701bb1012c6fb to your computer and use it in GitHub Desktop.
Save coltjones/449389c701bb1012c6fb to your computer and use it in GitHub Desktop.
Purge Facebooks cache for a newly published page/post in wordpress.
<?php
add_action( 'transition_post_status' , 'purge_future_post', 10, 3);
function purge_future_post( $new_status, $old_status, $post ) {
if($new_status == 'publish') {
purge_facebook_cache($post);
}
}
function purge_facebook_cache($post_id) {
$fbUrl = 'https://graph.facebook.com/';
$pageUrl = get_permalink($post_id);
$postData = array('id' => urlencode($pageUrl),
'scrape' => "true");
$postDataStr = http_build_query($postData);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$fbUrl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postDataStr);
$result = curl_exec($ch);
curl_close($ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment