Friday, August 26, 2022
HomeWordPress Developmentphp - Retailer customized put up sort with JSON content material

php – Retailer customized put up sort with JSON content material


The core WordPress knowledge constructions and the REST API’s core controllers aren’t actually designed to ever deal with put up content material as something apart from one large string. As Tom mentions within the feedback, which means that storing JSON in that subject will end in it being introduced as an escaped string in REST responses reasonably than structurally a part of JSON response itself – you would want to carry out a second go of JSON-parsing on the sector within the shopper.

Nothing will actually “break” – it can simply be a bit uncomfortable to see a serialized JSON string included in a JSON REST response as a substitute of as a part of the response, along with the particular parsing case required on the client-side. One other downside is that if the shopper is able to setting the info on this JSON string, then you’ll probably want to write down a good bit of customized server-side logic to deal with validation and sanitization of submitted values (or in any other case discover a strategy to direct that worth via the mechanisms which WordPress provides).

A greater choice is likely to be to retailer the info in post-meta, as WordPress is already able to dealing with advanced knowledge saved in meta-values. This additionally signifies that inside server-side PHP you’ll be able to work together with the structured knowledge (if crucial) by way of conventional means with out manually unserializing/serializing the info:

update_post_meta(
  $profile_post_id,
  'user_profile_data',
  [
    'status'   => 'Eating a kabob',
    'bio'      => 'Lorem Ipsum dolor sit amet...',
    'bg_color' => '#efefef',
  ]
);

A put up meta-field might be uncovered on the core REST routes by way of the register_post_meta() perform, which additionally provides lots of configurability to help in validating and sanitizing the info, customized authorization checks, and extra. See the Modifying Responses web page within the REST API Handbook for extra data.

perform wpse408959_register_profile_meta() {
  register_post_meta(
    'user_profile',
    'user_profile_data',
    [
      'type'          => 'object',
      'single'        => true,
      'auth_callback' => function( $allowed, $meta_key, $object_id, $user_id, $cap, $caps ) {
        return current_user_can( 'edit_post', $object_id );
      },
      'show_in_rest'  => [
        'schema' => [
          'type'       => 'object',
          'properties' => [
            'status'   => [
              'type'      => 'string',
              'maxLength' => 120,
            ],
            'bio'      => [
              'type'      => 'string',
              'maxLength' => 1024,
            ],
            'bg_color' => [
              'type'      => 'string',
              'format'    => 'hex-color',
            ],
          ],
        ],
      ],
    ]
  );
}

add_action( 'rest_api_init', 'wpse408959_register_profile_meta' );

The Schema web page will assist in figuring out finest write and leverage the schema.

The meta-data will then be out there for REST interactions via a property within the meta object:

GET /wp-json/wp/v2/user_profile/1234
{
  // ...
  "meta": {
    "user_profile_data": {
      "standing": "Consuming a kabob",
      "bio": "Lorem Ipsum dolor sit amet...",
      "bg_color": "#efefef"
    }
  }
  // ...
}

As a last notice, in case you intend to make use of any of the advanced knowledge inside core WordPress techniques – say, to filter or kind profiles, or carry out a search primarily based on particular fields – then the info can be higher organized into WordPress’s core knowledge constructions – taxonomy phrases or particular person post-meta with primitive values, for instance. WordPress has services for storing and retrieving advanced meta-data, however it does not actually supply the means to work together with it past that, owed to the complexity and overhead concerned in working on serialized values in SQL.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments