Skip to content

Instantly share code, notes, and snippets.

@claudioweb
Last active July 5, 2017 14:04
Show Gist options
  • Save claudioweb/c5e309c993079eb477a16cce0e6d5a05 to your computer and use it in GitHub Desktop.
Save claudioweb/c5e309c993079eb477a16cce0e6d5a05 to your computer and use it in GitHub Desktop.
Retorno em Json Wordpress Ajax
<?php
//Nome da Action
$action_name = 'posts';
// Definindo action ajax
add_action('wp_ajax_'.$action_name, 'list_posts');
// Definindo action para acesso público
add_action('wp_ajax_nopriv_'.$action_name, 'list_posts');
//Função que irá retornar o JSON
function list_posts(){
// Definindo o retorno como JSON
header( "Content-type: application/json");
$args = array(
'post_type' => 'post',
'posts_per_page' => -1
);
$posts = get_posts($args);
die(json_encode($posts));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment