[wp-trac] [WordPress Trac] #41616: REST API: Expose old slugs
WordPress Trac
noreply at wordpress.org
Sat Aug 12 05:32:47 UTC 2017
#41616: REST API: Expose old slugs
-------------------------+-----------------------------
Reporter: crosescu | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: REST API | Version: 4.8.1
Severity: normal | Keywords:
Focuses: rest-api |
-------------------------+-----------------------------
Expose the old slugs for all posts including custom post types in the REST
API.
For example, when building a React-powered app using the WordPress REST
API the old slugs are required so that users can be automatically
redirected if they clicked on an old link.
To expose the old slugs I created a small plugin (see below):
{{{#!php
<?php
add_action( 'rest_api_init', 'expose_old_slugs_in_api' );
function expose_old_slugs_in_api() {
$post_types = get_post_types( array( 'show_in_rest' => true,
'show_in_nav_menus' => true ) ) ;
$args = array(
'get_callback' => 'get_old_slugs_for_api',
'schema' => null
);
foreach ($post_types as $type) {
register_rest_field( $type, 'old_slugs', $args );
}
}
function get_old_slugs_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
return get_post_meta( $post_id, '_wp_old_slug' );
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/41616>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list