Skip to content
This repository has been archived by the owner on Apr 4, 2020. It is now read-only.

Meta _links do not appear post-beta13 #12

Open
duncanjbrown opened this issue Apr 18, 2016 · 1 comment
Open

Meta _links do not appear post-beta13 #12

duncanjbrown opened this issue Apr 18, 2016 · 1 comment

Comments

@duncanjbrown
Copy link
Contributor

This PR WP-API/WP-API@32e1940 removed meta links from Post controller responses in the WP-API plugin. This plugin doesn’t add them back in, though, so they're gone everywhere. I looked for a filter to remedy this, but couldn’t find one. Happy to implement any suggestions in a PR 😄

@duncanjbrown
Copy link
Contributor Author

duncanjbrown commented Jul 22, 2016

In case anyone else is having this problem, here's some code to restore the meta links for posts.

Note that the namespace is hardcoded because we're no longer in the context of REST_Posts_Controller, so can't read its properties.

add_action( 'rest_api_init', function() {
    $post_types = get_post_types( array( 'public' => true ), 'names' );

    foreach( $post_types as $post_type ) {
        if ( post_type_supports( $post_type, 'custom-fields' ) ) {
            add_filter( "rest_prepare_${post_type}", 'prepare_meta_link_for_post_type' , 10, 3);
        }
    }

    function prepare_meta_link_for_post_type( $response, $post, $request ) {
        $post_type = get_post_type_object( $post->post_type );
        $rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
        $base = sprintf( '/wp/v2/%s/', $rest_base );
        $response->add_link(
            'https://api.w.org/meta',
            rest_url( $base . $post->ID . '/meta' ),
            array( 'embeddable' => true )
        );
        return $response;
    }
});

duncanjbrown added a commit to duncanjbrown/wp-api-client that referenced this issue Jul 22, 2016
These were removed in beta13 and handed off to another plugin, wp-api-meta-endpoints.

This plugin does not restore the links, so you need to add them yourself:
WP-API/wp-api-meta-endpoints#12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant