Last active
February 8, 2016 14:22
-
-
Save lenivene/369216afb584be46cd9a to your computer and use it in GitHub Desktop.
Adiciona informações do facebook a campos personalizados WordPress
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
<?php | |
/** | |
* Author: Rhuan Carlos | |
*/ | |
function insert_likes_custom_field( $post_ID ) { | |
global $wpdb; | |
if ( !wp_is_post_revision( $post_ID ) ){ | |
add_post_meta( $post_ID, 'likes_count', '0', true ); | |
} | |
} | |
add_action( 'publish_page', 'insert_likes_custom_field' ); | |
add_action( 'publish_post', 'insert_likes_custom_field' ); | |
/** | |
* Extrai número de likes do artigo | |
* | |
*/ | |
function update_facebook_likes( $content = '' ) { | |
global $wp_query; | |
$post_ID = $wp_query->post->ID; | |
$data = file_get_contents( 'http://graph.facebook.com/?id='. get_permalink() ); | |
if( $data !== false ){ | |
$json = json_decode( $data ); | |
$likes = $json->{'comments'}; | |
$meta_values = get_post_meta( $post_ID, 'likes_count', true ); | |
if ( empty( $likes ) ) { | |
$likes = '0'; | |
} | |
if( $meta_values > $likes ){ | |
update_post_meta( $post_ID, 'likes_count', $likes, false); | |
} | |
} | |
return $content; | |
} | |
add_action( 'the_content', 'update_facebook_likes' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment