get_sample_permalink

Get a sample permalink based off of the post name.

Description

(array) get_sample_permalink( (int) $id, (constant) $title = null, (constant) $name = null ); 

Returns (array)

Array containing the sample permalink with placeholder for the post name, and the post name.

Parameters (3)

0. $id (int)
Post ID or post object.
1. $title — Optional. (constant) => null
Title to override the post's current title when generating the post name. Default null.
2. $name — Optional. (constant) => null
Name to override the post name. Default null.

Usage

  1. if ( !function_exists( 'get_sample_permalink' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4. // Post ID or post object. 
  5. $id = -1; 
  6.  
  7. // Optional. Title to override the post's current title when generating the post name. Default null. 
  8. $title = null; 
  9.  
  10. // Optional. Name to override the post name. Default null. 
  11. $name = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = get_sample_permalink($id, $title, $name); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/post.php  
  1. function get_sample_permalink($id, $title = null, $name = null) { 
  2. $post = get_post( $id ); 
  3. if ( ! $post ) 
  4. return array( '', '' ); 
  5.  
  6. $ptype = get_post_type_object($post->post_type); 
  7.  
  8. $original_status = $post->post_status; 
  9. $original_date = $post->post_date; 
  10. $original_name = $post->post_name; 
  11.  
  12. // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published. 
  13. if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ) ) ) { 
  14. $post->post_status = 'publish'; 
  15. $post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID); 
  16.  
  17. // If the user wants to set a new name -- override the current one 
  18. // Note: if empty name is supplied -- use the title instead, see #6072 
  19. if ( !is_null($name) ) 
  20. $post->post_name = sanitize_title($name ? $name : $title, $post->ID); 
  21.  
  22. $post->post_name = wp_unique_post_slug($post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent); 
  23.  
  24. $post->filter = 'sample'; 
  25.  
  26. $permalink = get_permalink($post, true); 
  27.  
  28. // Replace custom post_type Token with generic pagename token for ease of use. 
  29. $permalink = str_replace("%$post->post_type%", '%pagename%', $permalink); 
  30.  
  31. // Handle page hierarchy 
  32. if ( $ptype->hierarchical ) { 
  33. $uri = get_page_uri($post); 
  34. if ( $uri ) { 
  35. $uri = untrailingslashit($uri); 
  36. $uri = strrev( stristr( strrev( $uri ), '/' ) ); 
  37. $uri = untrailingslashit($uri); 
  38.  
  39. /** This filter is documented in wp-admin/edit-tag-form.php */ 
  40. $uri = apply_filters( 'editable_slug', $uri, $post ); 
  41. if ( !empty($uri) ) 
  42. $uri .= '/'; 
  43. $permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink); 
  44.  
  45. /** This filter is documented in wp-admin/edit-tag-form.php */ 
  46. $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) ); 
  47. $post->post_status = $original_status; 
  48. $post->post_date = $original_date; 
  49. $post->post_name = $original_name; 
  50. unset($post->filter); 
  51.  
  52. /** 
  53. * Filters the sample permalink. 
  54. * @since 4.4.0 
  55. * @param array $permalink Array containing the sample permalink with placeholder for the post name, and the post name. 
  56. * @param int $post_id Post ID. 
  57. * @param string $title Post title. 
  58. * @param string $name Post name (slug). 
  59. * @param WP_Post $post Post object. 
  60. */ 
  61. return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post );