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..