Support

Account

Home Forums General Issues How can I retrieve fields from an options page using the REST API (v5.11)? Reply To: How can I retrieve fields from an options page using the REST API (v5.11)?

  • Thanks to your suggestion I wound up learning how to create a custom endpoint, so thanks for that!

    I wound up creating a custom endpoint that executes and returns the result of a get_field(); call against my options field, which worked a charm.

    Here’s what I wound up with, in case it’s helpful for anyone else trying to do something similar:

    Set up the endpoint and callback:

    
    function cp_colspec_endpoint() {
      $field = get_field('col_spec','options');
      return $field;
    }
    
    //expose acf options page field(s) through custom endpoint
    function cp_register_api_endpoints() {
      register_rest_route( 'canopy/v2', '/acf--colspec', array(
        'methods' => 'GET',
        'callback' => 'cp_colspec_endpoint',
      ) );
    }
    
    add_action( 'rest_api_init', 'cp_register_api_endpoints' );