WordPress.org

Forums

WP REST API (WP API)
the_content filter to add custom fields to JSON response (1 post)

  1. kentverger
    Member
    Posted 1 year ago #

    I am little desesperate with this the_content filter for display custom field in JSON API.

    I am using this plugin http://wordpress.org/plugins/json-rest-api/ to have JSON response from my custom post types. These custom post types have custom fields that I have to show in a mobile app.

    To implement this I wrote this code, that use the_content filter to replace the original content to only show the custom post type with HTML tags:

    add_filter( 'the_content', 'add_custom_post_fields_to_the_content' );
    
    function add_custom_post_fields_to_the_content( $content ){
    
        global $post;
    
        $custom_fields = get_post_custom($post->ID);
    
        $content = '<img id="provider-logo" src="'.$custom_fields["wpcf-logo"][0].'" />';
        $content = $content.'<img id="provider-image" src="'.$custom_fields["wpcf-fotos"][0].'" />';
        $content = $content.'<h1 id="provider-name">'.$post->post_title.'</h1>';
        $content = $content.'<p id="provider-address">'.$custom_fields["wpcf-direccion"][0].'</p>';
        $content = $content.'<p id="provider-phone">'.$custom_fields["wpcf-phone"][0].'</p>';
        $content = $content.'<p id="provider-facebook">'.$custom_fields["wpcf-facebook"][0].'</p>';
    
        return $content;
    }

    So, then when I request the info via browser, this is an example http://bride2be.com.mx/ceremonia/ the custom fields are displayed well, but when I request the JSON data only shows the HTML without the values of the custom fields.

    Here iss an example:

    http://bride2be.com.mx/wp-json.php/posts?type=ceremonia

    I am little lost with this, someone can help me?

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WP REST API (WP API)
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic