rest_get_route_for_post example

'raw'      => $post->post_excerpt,
                'rendered' => $this->prepare_excerpt_response( $post->post_excerpt, $post ),
            );
        }

        $context  = ! empty( $request['context'] ) ? $request['context'] : 'view';
        $data     = $this->add_additional_fields_to_object( $data$request );
        $data     = $this->filter_response_by_context( $data$context );
        $response = rest_ensure_response( $data );

        if ( ! empty( $data['parent'] ) ) {
            $response->add_link( 'parent', rest_url( rest_get_route_for_post( $data['parent'] ) ) );
        }

        /** * Filters a revision returned from the REST API. * * Allows modification of the revision right before it is returned. * * @since 4.7.0 * * @param WP_REST_Response $response The response object. * @param WP_Post $post The original revision object. * @param WP_REST_Request $request Request used to generate the response. */
$links     = parent::prepare_links( $post );
        $menu_item = $this->get_nav_menu_item( $post->ID );

        if ( empty( $menu_item->object_id ) ) {
            return $links;
        }

        $path = '';
        $type = '';
        $key  = $menu_item->type;
        if ( 'post_type' === $menu_item->type ) {
            $path = rest_get_route_for_post( $menu_item->object_id );
            $type = get_post_type( $menu_item->object_id );
        } elseif ( 'taxonomy' === $menu_item->type ) {
            $path = rest_get_route_for_term( $menu_item->object_id );
            $type = get_term_field( 'taxonomy', $menu_item->object_id );
        }

        if ( $path && $type ) {
            $links['https://api.w.org/menu-item-object'][] = array(
                'href'       => rest_url( $path ),
                $key         => $type,
                'embeddable' => true,
            );

    protected function add_image_to_index( WP_REST_Response $response$image_id$type ) {
        $response->data[ $type ] = (int) $image_id;
        if ( $image_id ) {
            $response->add_link(
                'https://api.w.org/featuredmedia',
                rest_url( rest_get_route_for_post( $image_id ) ),
                array(
                    'embeddable' => true,
                    'type'       => $type,
                )
            );
        }
    }

    /** * Retrieves the index for a namespace. * * @since 4.4.0 * * @param WP_REST_Request $request REST request instance. * @return WP_REST_Response|WP_Error WP_REST_Response instance if the index was found, * WP_Error if the namespace isn't set. */

    public function prepare_item_links( $id ) {
        $post = get_post( $id );

        $links = array();

        $item_route = rest_get_route_for_post( $post );
        if ( ! empty( $item_route ) ) {
            $links['self'] = array(
                'href'       => rest_url( $item_route ),
                'embeddable' => true,
            );
        }

        $links['about'] = array(
            'href' => rest_url( 'wp/v2/types/' . $post->post_type ),
        );

        


/** * Gets the REST route for the currently queried object. * * @since 5.5.0 * * @return string The REST route of the resource, or an empty string if no resource identified. */
function rest_get_queried_resource_route() {
    if ( is_singular() ) {
        $route = rest_get_route_for_post( get_queried_object() );
    } elseif ( is_category() || is_tag() || is_tax() ) {
        $route = rest_get_route_for_term( get_queried_object() );
    } elseif ( is_author() ) {
        $route = '/wp/v2/users/' . get_queried_object_id();
    } else {
        $route = '';
    }

    /** * Filters the REST route for the currently queried object. * * @since 5.5.0 * * @param string $link The route with a leading slash, or an empty string. */


        if ( 0 !== (int) $comment->user_id ) {
            $links['author'] = array(
                'href'       => rest_url( 'wp/v2/users/' . $comment->user_id ),
                'embeddable' => true,
            );
        }

        if ( 0 !== (int) $comment->comment_post_ID ) {
            $post       = get_post( $comment->comment_post_ID );
            $post_route = rest_get_route_for_post( $post );

            if ( ! empty( $post->ID ) && $post_route ) {
                $links['up'] = array(
                    'href'       => rest_url( $post_route ),
                    'embeddable' => true,
                    'post_type'  => $post->post_type,
                );
            }
        }

        if ( 0 !== (int) $comment->comment_parent ) {
            
/** * Prepares links for the request. * * @since 5.8.0 * * @param integer $id ID. * @return array Links for the given post. */
    protected function prepare_links( $id ) {
        $links = array(
            'self'       => array(
                'href' => rest_url( rest_get_route_for_post( $id ) ),
            ),
            'collection' => array(
                'href' => rest_url( rest_get_route_for_post_type_items( $this->post_type ) ),
            ),
            'about'      => array(
                'href' => rest_url( 'wp/v2/types/' . $this->post_type ),
            ),
        );

        return $links;
    }

    

        do_action( "rest_after_insert_{$this->post_type}", $post$request, true );

        wp_after_insert_post( $post, false, null );

        $response = $this->prepare_item_for_response( $post$request );
        $response = rest_ensure_response( $response );

        $response->set_status( 201 );
        $response->header( 'Location', rest_url( rest_get_route_for_post( $post ) ) );

        return $response;
    }

    /** * Checks if a given request has access to update a post. * * @since 4.7.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. */
/** * Prepares the links for the request. * * @since 6.3.0 * * @param WP_Post $post the Navigation Menu post object. * @return array Links for the given request. */
    private function prepare_links( $post ) {
        return array(
            'self' => array(
                'href'       => rest_url( rest_get_route_for_post( $post->ID ) ),
                'embeddable' => true,
            ),
        );
    }
}

remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );

/* * Block editor implements its own Options menu for toggling Document Panels. */
add_filter( 'screen_options_show_screen', '__return_false' );

wp_enqueue_script( 'heartbeat' );
wp_enqueue_script( 'wp-edit-post' );

$rest_path = rest_get_route_for_post( $post );

// Preload common data. $preload_paths = array(
    '/wp/v2/types?context=view',
    '/wp/v2/taxonomies?context=view',
    add_query_arg(
        array(
            'context'  => 'edit',
            'per_page' => -1,
        ),
        rest_get_route_for_post_type_items( 'wp_block' )
    ),
Home | Imprint | This part of the site doesn't use cookies.