Open stephenheron opened 4 years ago
There is no such option available at the moment. I'll mark this as a feature request.
Hello Yoast, This is really headache for my mobile app development. It is really taking a lot of time for fetching the posts from my wordpress website using REST API. So could you please suggest me any options to disable the yoast_head or please add it asap. Or else, we will forcefully change it to Rank Math or something else. I hope u r understanding and please help me out here.
Also having issues when listing posts, seems like yoast_head and yoast_head_json should be added for a post when requesting a single post by id or slug (as well as adding it to single author, etc) but seems like an overkill when listing posts by category for example, the response right now for a list of posts is massive.
I am sure there is a cleaner solution but this is a quick patch I came up with to only add yoast_head
and yoast_head_json
data to single post requests (applies to all public post types) if it helps anyone with the same problem.
I forked the plugin and changed the function register_routes
inside of wordpress-seo/src/routes/yoast-head-rest-field.php
/**
* Registers routes with WordPress.
*
* @return void
*/
public function register_routes() {
global $wp_post_types;
$public_post_types = $this->post_type_helper->get_public_post_types();
$parsed = wp_parse_url( $_SERVER['REQUEST_URI'] );
$path_prefix = '/wp-json/wp/v2/';
$paths = [];
$is_single_post_request = false;
// Creates an array of post type paths which check against later, using the rest_base or rewrite slug.
if ( ! empty( $public_post_types ) ) {
foreach ( $public_post_types as $public_post_type => $item ) {
if ( empty( $wp_post_types[ $public_post_type ] ) ) {
continue;
}
switch (true) {
case ! empty( $wp_post_types[ $public_post_type ]->rest_base ):
$slug = $wp_post_types[ $public_post_type ]->rest_base;
break;
case ! empty( $wp_post_types[ $public_post_type ]->rewrite ) &&
! empty( $wp_post_types[ $public_post_type ]->rewrite['slug'] ):
$slug = $wp_post_types[ $public_post_type ]->rewrite['slug'];
break;
default:
$slug = $public_post_type;
break;
}
$paths[ $slug ] = $path_prefix . $slug;
}
}
// Check if the request is by slug.
if ( in_array( rtrim( $parsed['path'], '/' ), $paths ) && ! empty( $parsed['query'] ) ) {
$is_single_post_request = false !== strpos( $parsed['query'], 'slug=' );
}
// Check if request is by ID.
if ( ! $is_single_post_request ) {
$regex_id = '/\/wp-json\/wp\/v2\/(' . join( '|', array_keys( $paths ) ) . ')\/(?P<ID>\d+)/';
preg_match( $regex_id, $parsed['path'], $matches, PREG_OFFSET_CAPTURE );
$is_single_post_request = ! empty( $matches['ID'] );
}
if ( $is_single_post_request ) {
foreach ( $public_post_types as $post_type ) {
$this->register_rest_fields( $post_type, 'for_post' );
}
}
$public_taxonomies = $this->taxonomy_helper->get_public_taxonomies();
foreach ( $public_taxonomies as $taxonomy ) {
if ( $taxonomy === 'post_tag' ) {
$taxonomy = 'tag';
}
$this->register_rest_fields( $taxonomy, 'for_term' );
}
$this->register_rest_fields( 'user', 'for_author' );
$this->register_rest_fields( 'type', 'for_post_type_archive' );
}
You can also just hardcode the list of post types in $paths instead of going through that loop.
$paths = [
'/wp-json/wp/v2/posts',
'/wp-json/wp/v2/pages',
'/wp-json/wp/v2/events',
......
];
If you do so then just replace the regex where join( '|', array_keys( $paths )
with:
$regex_id = '/\/wp-json\/wp\/v2\/(posts|pages|events)\/(?P<ID>\d+)/';
Hi,
We use the REST API to get a list of all our posts.
Without the “REST API: Head endpoint” feature enabled it takes us around 400ms - 600ms to get 500 posts via the API. Unfortunately when we enable the “REST API: Head endpoint” this jumps up to around 14 seconds which is quite an increase.
I understand that this time increase might be unavoidable and our use case is probably very rare however it would be good to have a way to disable the ‘yoast_head’ field without having to disable the entire Yoast REST API. An option may already exist but I could not find it.
Thanks, Stephen