1

Suppose that in one plugin (let's call it plugin A) I register a country custom field for requests that query users via the Wordpress REST API:

register_rest_field( "user", "country", [
   "get_callback" => array( $this, "get_country" ),
   "update_callback" => array( $this, "set_country" ),
   "schema" => [
      "type" => "string",
      "description" => "The user's country",
      "arg_options" => [
         "sanitize_callback" => function( $value ) {
            return sanitize_text_field( $value );
         },
         "validate_callback" => function( $value ) {
            return is_string( $value );
         }
      ]
   ]
]);

I've discovered I can make the field required by adding "required" => true to the schema array, but what if I only want to make the field required if a certain plugin (let's call it plugin B) is active?

I could check in plugin A if plugin B is active, but I'd rather use some hook in plugin B because it's cleaner and more future-proof. Is there any way to hook into register_rest_field and modify the behavior of a REST API field, or something else to the same effect?

2
  • Can you modify plugin A (that has the register_rest_field() command in it)?
    – kero
    Commented Oct 18, 2021 at 8:17
  • @kero Yes I can.
    – grazdev
    Commented Oct 18, 2021 at 8:24

1 Answer 1

1

I think the most simple solution would be to use a custom filter for this. It could look something like:

$schema = apply_filters('pluginA/register/country', [
    "type" => "string",
    "description" => "The user's country",
    "arg_options" => [
        "sanitize_callback" => function( $value ) {
            return sanitize_text_field( $value );
        },
        "validate_callback" => function( $value ) {
            return is_string( $value );
        },
    ]
]);
register_rest_field( "user", "country", [
    "get_callback" => array( $this, "get_country" ),
    "update_callback" => array( $this, "set_country" ),
    "schema" => $schema,
]);

Now in your other plugin, you simply add a filter and add the required flag or not:

add_filter('pluginA/register/country', function(array $schema): array {
    $schema['required'] = true;
    return $schema;
});

Of course the name of the filter can be freely chosen, it probably makes sense to use some kind of vendor prefix, so it doesn't clash with filters defined by somebody else.

3
  • 1
    Oh I didn't know I could use apply_filters so granularly, never thought about it... Pretty powerful actually. Thank you for enlightening me!
    – grazdev
    Commented Oct 18, 2021 at 8:53
  • You're welcome! Usually I avoid writing custom filters, but it seems that register_rest_field doesn't easily allow to change stuff without more digging. Additionally, I think (you need to test it) that you can use 'sanitize_callback' => 'sanitize_text_field' instead of needing the anonymous function there.
    – kero
    Commented Oct 18, 2021 at 8:57
  • 1
    Yeah I think that's by design, in the api docs they clearly say it's strongly discouraged to modify default fields, but it seems that as a result it becomes difficult to modify a custom field too. And you're right about using named functions: I've used it somewhere else and it works (both for 'sanitize_callback' and 'validate_callback')
    – grazdev
    Commented Oct 18, 2021 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.