Last active
September 9, 2020 11:29
-
-
Save fabriziofeitosa/34f1a1546b0be79b8f3276ca2a5f8598 to your computer and use it in GitHub Desktop.
(WP) Removendo e adiciona campos da resposta JSON | Removing and add fields from the JSON response
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 | |
// Adicionar campos à resposta JSON | Adding fields to the JSON response | |
function ffq_add_custom_meta_to_posts( $data, $post, $context ) { | |
// Queremos apenas modificar o contexto de 'visualização', para ler postagens | |
if ( $context !== 'view' || is_wp_error( $data ) ) { | |
return $data; | |
} | |
// Capturando o valor o campo "Source". Basta troca por algum outro campo que | |
// queria puxar o valor. | |
$source = get_post_meta( $post['ID'], 'Source', true ); // Campo "Source" | |
// Se não estiver vazio, captura e joga na resposta JSON. Configure como acima! | |
if ( ! empty( $source ) ) { | |
$data['custom_meta'] = array( 'Source' => $source ); // Referente ao campo "Source" | |
} | |
return $data; | |
} | |
add_filter( 'json_prepare_post', 'ffq_add_custom_meta_to_posts', 10, 3 ) |
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 | |
// Removendo campos da resposta JSON | Removing fields from the JSON response | |
function ffq_remove_extra_data( $data, $post, $context ) { | |
// Queremos apenas modificar o contexto de 'visualização', para ler postagens | |
if ( $context !== 'view' || is_wp_error( $data ) ) { | |
return $data; | |
} | |
// Aqui, removemos todos os dados que não queremos ver no front end: | |
unset( $data['author'] ); | |
unset( $data['status'] ); | |
// Continue 'removendo/unset' quaisquer outros campos que você deseja | |
// seguindo o código acima | |
return $data; | |
} | |
add_filter( 'json_prepare_post', 'ffq_remove_extra_data', 12, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment