get_sample_permalink_html

Returns the HTML of the sample permalink slug editor.

Description

(string) get_sample_permalink_html( (int) $id, (constant) $new_title = null, (null) $new_slug = null ); 

Returns (string)

The HTML of the sample permalink slug editor.

Parameters (3)

0. $id (int)
Post ID or post object.
1. $new_title — Optional. (constant) => null
New title. Default null.
2. $new_slug — Optional. (null) => null
New slug. Default null.

Usage

  1. if ( !function_exists( 'get_sample_permalink_html' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4. // Post ID or post object. 
  5. $id = -1; 
  6.  
  7. // Optional. New title. Default null. 
  8. $new_title = null; 
  9.  
  10. // Optional. New slug. Default null. 
  11. $new_slug = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = get_sample_permalink_html($id, $new_title, $new_slug); 
  15.  

Defined (1)

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

/wp-admin/includes/post.php  
  1. function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { 
  2. $post = get_post( $id ); 
  3. if ( ! $post ) 
  4. return ''; 
  5.  
  6. list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug); 
  7.  
  8. $view_link = false; 
  9. $preview_target = ''; 
  10.  
  11. if ( current_user_can( 'read_post', $post->ID ) ) { 
  12. if ( 'draft' === $post->post_status || empty( $post->post_name ) ) { 
  13. $view_link = get_preview_post_link( $post ); 
  14. $preview_target = " target='wp-preview-{$post->ID}'"; 
  15. } else { 
  16. if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) { 
  17. $view_link = get_permalink( $post ); 
  18. } else { 
  19. // Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set 
  20. $view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink ); 
  21.  
  22. // Permalinks without a post/page name placeholder don't have anything to edit 
  23. if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) { 
  24. $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n"; 
  25.  
  26. if ( false !== $view_link ) { 
  27. $display_link = urldecode( $view_link ); 
  28. $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n"; 
  29. } else { 
  30. $return .= '<span id="sample-permalink">' . $permalink . "</span>\n"; 
  31.  
  32. // Encourage a pretty permalink setting 
  33. if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) { 
  34. $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n"; 
  35. } else { 
  36. if ( mb_strlen( $post_name ) > 34 ) { 
  37. $post_name_abridged = mb_substr( $post_name, 0, 16 ) . '…' . mb_substr( $post_name, -16 ); 
  38. } else { 
  39. $post_name_abridged = $post_name; 
  40.  
  41. $post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>'; 
  42. $display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) ); 
  43.  
  44. $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n"; 
  45. $return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n"; 
  46. $return .= '‎'; // Fix bi-directional text display defect in RTL languages. 
  47. $return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>\n"; 
  48. $return .= '<span id="editable-post-name-full">' . esc_html( $post_name ) . "</span>\n"; 
  49.  
  50. /** 
  51. * Filters the sample permalink HTML markup. 
  52. * @since 2.9.0 
  53. * @since 4.4.0 Added `$post` parameter. 
  54. * @param string $return Sample permalink HTML markup. 
  55. * @param int $post_id Post ID. 
  56. * @param string $new_title New sample permalink title. 
  57. * @param string $new_slug New sample permalink slug. 
  58. * @param WP_Post $post Post object. 
  59. */ 
  60. $return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post ); 
  61.  
  62. return $return;