• Resolved billu18murali

    (@billu18murali)


    I am using table to display data. Is it possible to expose the table data as Rest – JSON directly.
    I am able to see the table data WITH <row> and <column> tags in a single json value.
    I even tried using table advanced custom field, using which I get the data as,

    “placetable”: [
    “{\”p\”:{\”o\”:{\”uh\”:0}},\”c\”:{\”0\”:{\”p\”:\”\”},\”1\”:{\”p\”:\”\”}},\”h\”:{\”0\”:{\”c\”:\”\”},\”1\”:{\”c\”:\”\”}},\”b\”:{\”0\”:{\”0\”:{\”c\”:\”test\”},\”1\”:{\”c\”:\”america\”}},\”1\”:{\”0\”:{\”c\”:\”india\”},\”1\”:{\”c\”:\”europe\”}}}}”
    ]
    Any help on this would be appreciated.

    https://www.ads-software.com/plugins/advanced-custom-fields-table-field/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Johann Heyne

    (@jonua)

    Hi, as long as WordPress has no build in REST-API you must enable the WordPress REST-API by the plugin https://www.ads-software.com/plugins/rest-api/.

    After installing the plugin, you can get for example data of the page with post_id 2 like this:
    yoursite.com/wp-json/wp/v2/pages/2/

    But the result does not include the meta/acf data of the page. To get the acf fields of a page, you have to register the fields in the API. Put the following code in your themes functions.php file

    function api_page_get_acf_fields( $object, $field_name, $request ) {
    
    	return get_fields( $object[ 'id' ] );
    }
    
    add_action( 'rest_api_init', function() {
    
    	register_api_field( 'page', // the post-type
    		'acf-fields', // the key in the JSON result
    		array(
    			'get_callback'    => 'api_page_get_acf_fields',
    		)
    	);
    });

    Now you can get the data of a page including the ACF fields via the REST-API. But notice, the WordPress REST-API is still in development.

    Plugin Author Johann Heyne

    (@jonua)

    To encode ACF table data as JSON you can do following:

    $table_data_array = get_field( 'your_table_field_name' );
    $table_data_json = json_encode( $table_data_array );

    Hello,

    My array after json_decode:

    Array (
    [p] => Array ( [o] => Array ( [uh] => 0 ) )
    [c] => Array ( [0] => Array ( [p] => ) )
    [h] => Array ( [0] => Array ( [c] => domain ) )
    [b] => Array ( [0] => Array ( [0] => Array ( [c] => domain ) ) )
    )

    How i can echo”domain” from array for because i don’t now about Array table

    Thanks for support

    Plugin Author Johann Heyne

    (@jonua)

    echo $table['b'][0][0]['c'];

    This code echo the first top, left table cell of the tables body. [‘b’] represents “body”. The first [‘0’] represents the first row. The second [‘0’] represents the first cell of the row. [‘c’] represents the content of a cell. And so on. [h] represents the table header. Because there is only one row as the header you can echo the first cell by:

    echo $table['h'][0]['c'];

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Exposing table data as Rest JSON’ is closed to new replies.