Using WordPress ‘get_sample_permalink_html’ PHP filter

The get_sample_permalink_html WordPress PHP filter allows you to modify the sample permalink HTML markup.

Usage

add_filter( 'get_sample_permalink_html', 'your_function_name', 10, 5 );
function your_function_name( $return, $post_id, $new_title, $new_slug, $post ) {
    // your custom code here
    return $return;
}

Parameters

  • $return (string): Sample permalink HTML markup.
  • $post_id (int): Post ID.
  • $new_title (string): New sample permalink title.
  • $new_slug (string): New sample permalink slug.
  • $post (WP_Post): Post object.

More information

See WordPress Developer Resources: get_sample_permalink_html

Examples

Modify the sample permalink text color to red.

add_filter( 'get_sample_permalink_html', 'change_sample_permalink_text_color', 10, 5 );
function change_sample_permalink_text_color( $return, $post_id, $new_title, $new_slug, $post ) {
    $return = str_replace( '<strong>', '<strong style="color: red;">', $return );
    return $return;
}

Add a custom CSS class

Add a custom CSS class to the sample permalink.

add_filter( 'get_sample_permalink_html', 'add_custom_css_class', 10, 5 );
function add_custom_css_class( $return, $post_id, $new_title, $new_slug, $post ) {
    $return = str_replace( '<strong>', '<strong class="your-custom-class">', $return );
    return $return;
}

Change the sample permalink structure to include the category.

add_filter( 'get_sample_permalink_html', 'modify_sample_permalink_structure', 10, 5 );
function modify_sample_permalink_structure( $return, $post_id, $new_title, $new_slug, $post ) {
    $category = get_the_category( $post_id );
    if ( ! empty( $category ) ) {
        $return = str_replace( '%postname%', $category[0]->slug . '/%postname%', $return );
    }
    return $return;
}

Display a custom message

Display a custom message below the sample permalink.

add_filter( 'get_sample_permalink_html', 'display_custom_message', 10, 5 );
function display_custom_message( $return, $post_id, $new_title, $new_slug, $post ) {
    $custom_message = '<p>Remember to check the permalink before publishing.</p>';
    $return .= $custom_message;
    return $return;
}

Remove the sample permalink from the Edit Post screen.

add_filter( 'get_sample_permalink_html', '__return_empty_string' );