Created
November 11, 2015 10:34
-
-
Save chuckreynolds/764912cfe607b03ca64e to your computer and use it in GitHub Desktop.
Ping Facebook Graph to crawl (scrape) or rescrape the URL if a scheduled post goes live or a draft is published. Depending on this specific need I may just do it on any time status goes to publish regardless of the old status.
This file contains hidden or 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
<?php | |
/** | |
* If post goes from "future" (scheduled) or "draft" to "publish" then alert facebook to (re)crawl it. | |
* @see https://codex.wordpress.org/Post_Status_Transitions#transition_post_status_Hook | |
*/ | |
function chuck_notify_facebook_scraper( $new_status, $old_status, $post ) { | |
if ( $new_status != 'publish' ) { | |
return; | |
} | |
$url = get_permalink( $post->ID ); | |
$fbtoken = ''; // FB App Token - https://developers.facebook.com/tools/explorer/ | |
if ( 'draft' === $old_status || 'future' === $old_status ) { | |
chuck_facebook_recrawl_url( $url, $fbtoken ); | |
} | |
} | |
/** | |
* Fire POST to facebook Graph API as per Updating Objects docs. | |
* | |
* @see https://developers.facebook.com/docs/sharing/opengraph/using-objects#update | |
*/ | |
function chuck_facebook_recrawl_url( $url, $fbtoken ) { | |
$endpoint = sprintf( | |
'https://graph.facebook.com/?%s', | |
http_build_query( array( 'id' => $url, 'access_token' => $fbtoken, 'scrape' => true ) ) | |
); | |
$response = wp_remote_post( $endpoint, array() ); | |
} | |
add_action( 'transition_post_status', 'chuck_notify_facebook_scraper', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment