Home > Software design >  How to register custom field that is based on existing value in REST route
How to register custom field that is based on existing value in REST route

Time:02-11

Iam registering a rest route (see Code 1) whereby the content of the route will be equal to results from the wpso_messages database.

The resultant JSON of the get request to the custom route is below. (see #Resultant Json)

I would like to register 2 additional custom fields in the rest route which are: <username_from> and <username_to> that are based on the login names of the userids currently defined in the <user_from> and <user_to> (see Resultant JSON).

I understand that I may have to use register rest field function (see sample code 2), however it doesnt seem to be getting hooked to the registered custom rest route, also, I would need to access the user_from column in the existing api callback.

Any advise?

#Code 1: Register Rest Route

<?php

 function get_wp_query() {
    global $wpdb;
      $row = $wpdb->get_results("SELECT * FROM wpso_messages");
      return $row;
      };
add_action( 'rest_api_init', function () {
    register_rest_route( 'wp/v2', 'messages', array(
        'methods' => 'GET',
        'callback' => 'get_wp_query'
        ) );
    } );

#Resultant JSON

{ "id": "1", "user_from": "82", "user_to": "1", "created_at": "2022-01-07 10:25:56", "message": "Iam interest to bid", "listing_id": "22775", "seen": "1" }

Code 2:

function get_custom_fields() {
    return 'our awesome endpoint';
}
add_action( 'rest_api_init', 'add_custom_fields' );

function add_custom_fields() {
    register_rest_field(
        'messages', 
        'custom_fields', //New Field Name in JSON RESPONSEs
        array(
            'get_callback'    => 'get_custom_fields', // custom function name 
            'update_callback' => null,
            'schema'          => null,
            )
        );
    }

CodePudding user response:

In your get_wp_query() function, don't return the results immediately. First, loop through them and add required fields from there.

In your case:

function get_wp_query() {
   global $wpdb;
   $rows = $wpdb->get_results("SELECT * FROM wpso_messages");

   foreach( $rows as $index => $row ) {
        $rows[$index]->username_from = get_user_by('id', $row.user_from)->display_name;
        $rows[$index]->username_to = get_user_by('id', $row.user_to)->display_name;
   }
   return $rows;
};

CodePudding user response:

Solution based Nawaz proposal:

function get_wp_query() {
    global $wpdb;
    $rows = $wpdb->get_results("SELECT * FROM wpso_messages");
    foreach( $rows as $index => $row ) {
    
           $user_from_id = $rows[$index]->user_from; 
           $user_to_id = $rows[$index]->user_to; 
           $rows[$index]->username_from = get_user_by('id', $user_from_id)->display_name;
           $rows[$index]->username_to = get_user_by('id', $user_to_id)->display_name;
    }
    return $rows;
};


add_action( 'rest_api_init', function () {
    register_rest_route( 'wp/v2', 'messages', array(
        'methods' => 'GET',
        'callback' => 'get_wp_query'
        ) );
    } );
  • Related