Make WordPress Core

Opened 9 years ago

Last modified 2 years ago

#41616 new enhancement

REST API: Expose old slugs

Reported by: crosescu Owned by:
Priority: normal Milestone: Future Release
Component: REST API Version: 4.8.1
Severity: normal Keywords: good-first-bug has-patch
Cc: Focuses: rest-api

Description

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
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' );
}

Attachments (3)

41616.diff​ (13.4 KB ) - added by xipasduarte 2 years ago.
Revised code with feedback and test changes
41616.2.diff​ (14.8 KB ) - added by xipasduarte 2 years ago.
Add missing changes to generated fixtures
41616.expose-old-slugs-rest-api.patch​ (2.6 KB ) - added by sachinrajcp123 9 months ago.

Download all attachments as: .zip

Change History (10)

#1 @rmccue
9 years ago

  • Keywords needs-patch needs-unit-tests added
  • Milestone Awaiting Review → Future Release

I see no reason not to add this, so long as it's only available in the edit context.

#2 @TimothyBlynJacobs
6 years ago

  • Keywords good-first-bug added

This ticket was mentioned in ​PR #1164 on ​WordPress/wordpress-develop by ​engahmeds3ed.


6 years ago
#3

  • Keywords has-patch added; needs-patch removed

Add old_slug to list of fields returned with post details.

Trac ticket: https://core.trac.wordpress.org/ticket/41616

#4 @LeonidasMilossis
5 years ago

I see the PR considers and edits unit tests, should we remove the needs-unit-tests tag?

#5 @berubenic
2 years ago

  • Keywords needs-refresh added

This ticket was mentioned in ​PR #7067 on ​WordPress/wordpress-develop by ​xipasduarte.


2 years ago
#6

  • Keywords has-unit-tests added; needs-unit-tests needs-refresh removed

Add old_slug to REST API fields.

This is a review of the changes made in #1164. The code was made from those changes then resolving conflicts and incorporating feedback from the previous patch.

Trac ticket: core.trac.wordpress.org/ticket/41616

#7 @xipasduarte
2 years ago

  • Keywords has-unit-tests removed

Resolved conflict issues and incorporated the following feedback:

@xipasduarte
2 years ago

Revised code with feedback and test changes

@xipasduarte
2 years ago

Add missing changes to generated fixtures

Note: See TracTickets for help on using tickets.