Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress pods io - Rest API for fetching fields information for custom post type

Hi I have created a custom post type using pods io. Whenever I hit the rest API for a custom post type, I get data for all the posts created using that post type. But what I need from rest API is the configuration of that post type.

That is I need to fetch the label and field type (ex plain text, file/video/image, wysiwyg) for that post type. enter image description here

As shown in the figure above, I need to fetch all the fields information for a particular custom post type.

Is that possible in wordpress using Rest API. Or is there any plugin which does that.

My requirement is that I want to configure fields of different types and I want to fetch fields information for that page. Any plugin which does that would be helpful.

like image 752
Aniket Avatar asked Jun 28 '17 08:06

Aniket


1 Answers

You can use Pods::fields() to get the details (e.g. label and type) of your pod's fields. For example:

$pod = pods( 'apc_information' );

// Get all fields.
$fields = $pod->fields();
foreach ( $fields as $field ) {
    echo $field['label'] . ' (' . $field['type'] . ')<br>';
}

// Get a specific field.
$field = $pod->fields( 'apc_title' );
echo $field['label'];

Now, if you would like to be able to using the WordPress REST API to fetch the fields details, you can create a custom REST API route.

Below is a working example, which is based on the example here.

  • The custom route I used is /my-plugin/v1/fields/<pod> and it has one GET endpoint: prefix_get_pods_fields() which is the callback for getting the fields details of a pod.

  • To get the details of all fields in a pod, make a request to /wp-json/my-plugin/v1/fields/<pod> where <pod> is the pod name/slug (e.g. apc_information in your case).

  • To get the details of a specific field only, make a request to /wp-json/my-plugin/v1/fields/<pod>?field=<name> where <name> is the field name/slug (e.g. the apc_title for your "Title" field).

    // The endpoint callback.
    function prefix_get_pods_fields( WP_REST_Request $request ) {
        // Get the pod.
        $pod = pods( $request['pod'], null, true );
    
        // Check if the pod exists and valid.
        if ( empty( $pod ) || ! $pod->valid() ) {
            return new WP_Error( 'rest_pods_invalid', 'The pod does not exist.', array( 'status' => 404 ) );
        }
    
        // Now return the fields array/data.
        $field = isset( $request['field'] ) ? $request['field'] : null;
        return rest_ensure_response( $pod->fields( $field ) );
    }
    
    // Callback for validating a parameter value.
    function prefix_data_arg_validate_callback( $value, WP_REST_Request $request, $param ) {
        if ( 'field' === $param && ! preg_match( '/^[a-z0-9\-_]+$/', $value ) ) {
            return new WP_Error( 'rest_invalid_param', 'Invalid field name.', array( 'status' => 400 ) );
        }
        return true;
    }
    
    // Callback for sanitizing a parameter value.
    function prefix_data_arg_sanitize_callback( $value, WP_REST_Request $request, $param ) {
        if ( 'field' === $param ) {
            return sanitize_text_field( $value );
        }
        return $value; // .. please make your own logic for sanitizing other fields, if any.
    }
    
    // Parameters for the /fields endpoint.
    function prefix_get_data_arguments() {
        $args = array();
    
        $args['field'] = array(
            'description'       => 'Field name.',
            'type'              => 'string',
            'validate_callback' => 'prefix_data_arg_validate_callback',
            'sanitize_callback' => 'prefix_data_arg_sanitize_callback',
        );
    
        return $args;
    }
    
    // Register our routes.
    function prefix_register_routes() {
        register_rest_route( 'my-plugin/v1', '/fields/(?P<pod>[a-z0-9\-_]+)', array(
            'method'   => WP_REST_Server::READABLE,
            'callback' => 'prefix_get_pods_fields',
            'args'     => prefix_get_data_arguments(),
        ) );
    }
    add_action( 'rest_api_init', 'prefix_register_routes' );
    

Let me know if you need clarification on any parts of the above code. But basically, you'd want to change the prefix (prefix_) (or the entire function name) and the route namespace (my-plugin/v1) to something more meaningful..

like image 162
Sally CJ Avatar answered Dec 12 '22 21:12

Sally CJ