Skip to content

Instantly share code, notes, and snippets.

@brunohaveroth
Created October 19, 2015 21:43
Show Gist options
  • Save brunohaveroth/76687962e1e1d461688a to your computer and use it in GitHub Desktop.
Save brunohaveroth/76687962e1e1d461688a to your computer and use it in GitHub Desktop.
arquivo javascript
//code ajax of loading feed of facebook
var skipFb = 0;
$('.button-feed-fb').click(function(e) {
var idFeedFb = e.target.id;
if (idFeedFb === 'prev-feed-fb') {
if (skipFb > 0) {
skipFb--;
}
}else {
skipFb++;
}
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
'action' : 'get_feed_fb',
'skip' : skipFb
},
success: function( response ){
$('.facebook-content').html(response);
}
});
});
funcao do arquivo function.php
// Get the feed if facebook with paginate
function get_feed_fb() {
$skip = $_POST['skip'];
$urlRequest = "https://graph.facebook.com/tagon8/feed?access_token=989691144406052|xGG3uZqUmL9b1ycUlqbXj6MPlEw&limit=1&offset=$skip";
$JSONcontent = file_get_contents($urlRequest);
$graphObject = json_decode($JSONcontent);
?>
<p class="bolder content-title">
<?php
setlocale(LC_ALL, "pt_BR", "pt_BR.iso-8859-1", "pt_BR.utf-8", "portuguese");
date_default_timezone_set('America/Sao_Paulo');
echo strftime("%A, %d de %B de %Y", strtotime( reset($graphObject->data)->created_time ));
?>
</p>
<p class="content-text">
<?php echo reset($graphObject->data)->message; ?>
</p>
<?php
wp_die();
}
add_action('wp_ajax_nopriv_get_feed_fb', 'get_feed_fb');
@vitorbritto
Copy link

Arquivo JavaScript

  • Chama um método error para ver qual tipo de erro ele retorna no console.
...
error: function(errorThrown) {
    console.error(errorThrown);
} 

functions.php

  • Passa um escape no POST.
$skip = mysql_real_escape_string($_POST['skip']);
  • No final:
add_action('wp_ajax_nopriv_get_feed_fb', 'get_feed_fb');
add_action('wp_ajax_get_feed_fb', 'get_feed_fb');

@vitorbritto
Copy link

Tenta passar: async: false no objeto XHR do seu script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment