Get outuput of wp_head or wp_footer in wordpress Rest API
Asked Answered
S

3

8

Is there a way to get what come with wp_head() or wp_footer() in rest api call?

I mean scripts and styles that can come from plugin or WordPress itself when I use wp_enqueue_script for example?

The reason is that I want to make everything with js framework, but I want to save the extendable by the plugins (not with all but if it's for styling for example that gives only special style).

If it's impossible so it would be good to know why...?

Also, if there is a way to circumvent this it also would be helpful in future.

Shilashilha answered 15/2, 2017 at 17:41 Comment(2)
Did you ever find a solution to this?Geyser
Unfortunately not...And just don't work with WP anymore.Shilashilha
G
0

You would have to create a custom API endpoint which contains the code for the header or footer. Then on whatever you're consuming the API with you'd have to parse and execute that code.

Gault answered 1/3, 2018 at 18:26 Comment(0)
E
0

The best way is to create a custom REST endpoint and place all scripts/styles output there. Also you can place a custom logic there if you have a different output depending on post type/taxonomy/etc.

You can also modify output of existing endpoints by using some filters, e.g.

apply_filters( "rest_prepare_{$this->post_type}", $response, $post, $request );
Engels answered 30/9, 2023 at 0:2 Comment(0)
G
0

It would require making your own API endpoint and depends one what actual information you specifically require.

For example if you want the HTML that you would normally get from the functions you can do this by getting the buffer contents after calling the functions. If however you want a list of script/style urls then a better way would be to look at the various global php variables to get that information and construct your own packet of json from that to interpret on the other end.

If the former is required, here is a simple class to add in a custom API endpoint - you can then hit this using your-wordpress-url/wp-json/my_route/get_header and your-wordpress-url/wp-json/my_route/get_footer which then returns a json packet like this

{
  "function": "getHeader",
  "html": "<link rel=\"stylesheet\" href=\"stylesheet.css\"><etc><etc>"
}

Note : An instance of the class would typically be created on the rest_api_init action - so Wordpress wouldn't of triggered all of its other normal actions and filters on a normal front resolve. It would be considered fairly bad practice to just get the HTML as it does below, but ultimately is possible.

Warning : The example below also has no authentication checks so would be open for all - you can add your own checks into the functions or add a permission call back

Example Class

/*-------------------*/
add_action('rest_api_init', function(){
  $apiInstance = new headerfooter_api();
});

/*-------------------*/
/*-------------------*/
//MARK: Class - Header/Footer API
class headerfooter_api {

  /*-------------------*/
  public function __construct() {
    register_rest_route('my_route', 'get_header', array(
      'methods' => 'GET',
      'callback' => array($this, 'getHeader'),
      'permission_callback' => '__return_true'
    ));

    register_rest_route('my_route', 'get_footer', array(
      'methods' => 'GET',
      'callback' => array($this, 'getFooter'),
      'permission_callback' => '__return_true'
    ));
  }

  /*-------------------*/
  public function getHeader($request) {
    $results = ['function' => 'getHeader', 'html' => ''];

    ob_start();
    wp_head();
    $results['html'] = ob_get_contents();
    ob_end_clean();

    $response = new WP_REST_Response($results);
    $response->set_status(200);
    return $response;
  }

  /*-------------------*/
  public function getFooter($request) {
    $results = ['function' => 'getFooter', 'html' => ''];

    ob_start();
    wp_footer();
    $results['html'] = ob_get_contents();
    ob_end_clean();

    $response = new WP_REST_Response($results);
    $response->set_status(200);

    return $response;
  }

  /*-------------------*/
}
Gangrene answered 4/7 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.