Cmb2: Rest API doesn't work

Created on 27 Oct 2016  路  4Comments  路  Source: CMB2/CMB2

Expected Behavior:

I wish to access the listing of all CMB2 boxes which have the 'show_in_rest' value set, GET request to: /wp-json/cmb2/v1/boxes.

Actual Behavior:

I was working with: "Postman", so I sent: "{{host}}/wp-json/cmb2/v1" (Where host = localhost) with a method: "GET", and the result is:

 {
  "code": "rest_no_route",
  "message": "No route was found matching the URL and request method",
  "data": {
    "status": 404
  }

Steps to reproduce:

  1. Download and active pluging: WP REST API (Last version).
  2. Download and active pluging: JSON Basic Authentication (Basic authentication - > user&password base 64).
  3. Download and active pluging: CMB2 (last version).
  4. Register a cmb2 fields (Look a code).
  5. Worked with postman (look Actual Behavior).

Observations:

  • I installed wordpress (Last version).
  • In Permalink Settings, its active a option: "Post name" (.htaccess it's updated)

CMB2 Field Registration Code:

add_action( 'cmb2_admin_init', 'usuario_register_metabox' ); 
function usuario_register_metabox() { // [name metabox]_register_metabox
    $prefix = 'pws';
    $cmb_demo = new_cmb2_box( array(
        'id'            => $prefix . 'metabox',
        'title'         => esc_html__( 'Test Metabox', 'cmb2' ),
        'object_types'  => array( 'page', ), // Post type
        'show_in_rest' => WP_REST_Server::READABLE
    ) );
    $cmb_demo->add_field( array(
        'name'       => esc_html__( 'Test Text', 'cmb2' ),
        'desc'       => esc_html__( 'field description (optional)', 'cmb2' ),
        'id'         => $prefix . 'text',
        'type'       => 'text',
        'show_on_cb' => 'yourprefix_hide_if_no_cats', // function should return a bool value
        'show_in_rest' => WP_REST_Server::READABLE
    ) );
 }

Actually its work with:

add_action( 'rest_api_init', 'usuario_custom_meta_cpost' );
function usuario_custom_meta_cpost() 
{
    register_rest_field(
        'page',
        'pws.text', // id field title
        array(
            'get_callback'    => 'prefix_get_custom_meta_cpost',
            'update_callback' => 'prefix_update_post_meta_cpost',
            'schema'          => null,
        )
    );
}

function prefix_update_post_meta_cpost( $value, $object, $field_name ) {
    return update_post_meta( $object->term_post_id, $field_name, $value );
}

function prefix_get_custom_meta_cpost( $object, $field_name, $request ) {
    return get_post_meta( $object[ 'id' ], $field_name, true );
}

All functions its work in Rest api, if I sent "{{host}}/wp-json/wp/v2/page" (Postman) with a method Get,
and the result is:

 {
    "id": 17,
    "date": "2016-10-24T22:05:12",
    "date_gmt": "2016-10-24T22:05:12",
    "guid": {
      "rendered": "{{host}}/?post_type=integrante&p=17"
    },
    "modified": "2016-10-24T22:05:12",
    "modified_gmt": "2016-10-24T22:05:12",
    "slug": "integrante3",
    "type": "integrante",
    "link": "{{host}}/integrante/integrante3/",
    "title": {
      "rendered": "integrante3"
    },
    "author": 1,
    "equipo": [],
    "invitacion": "no",
    "aceptado": "pendiente",
    "_links": {
      "self": [
        {
          "href": "{{host}}/wp-json/wp/v2/integrante/17"
        }
      ],
      "collection": [
        {
          "href": "{{host}}/wp-json/wp/v2/integrante"
        }
      ],
      "about": [
        {
          "href": "{{host}}wp-json/wp/v2/types/integrante"
        }
      ],
      "author": [
        {
          "embeddable": true,
          "href": "{{host}}/wp-json/wp/v2/users/1"
        }
      ],
      "wp:attachment": [
        {
          "href": "{{host}}/wp-json/wp/v2/media?parent=17"
        }
      ],
      "wp:term": [
        {
          "taxonomy": "equipo",
          "embeddable": true,
          "href": "{{host}}/wp-json/wp/v2/equipo?post=17"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }

where attributes: equipo, invitacion and aceptado are fields cmb2, but when I sent with a method post dont work, because is not written in the fields of cmb2 (but its write a basics fields wordpress).

All 4 comments

The rest_no_route error will be seen if no boxes are registered with the 'show_in_rest' param. This is the case for you as you are using the cmb2_admin_init hook to register your box/fields. To use the REST API with a box, it must be hooked to the cmb2_init hook instead.

I'll update the REST API docs to document this fact.

I've updated the docs, so please let me know if it is more clear.

And I've also updated the example-functions.php file to reflect this fact.

@jtsternberg Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

noquierouser picture noquierouser  路  8Comments

needtakehave picture needtakehave  路  7Comments

kidatart picture kidatart  路  7Comments

bomsn picture bomsn  路  5Comments

cehojac picture cehojac  路  3Comments