The get_sample_permalink WordPress PHP filter allows you to modify the sample permalink of a post.
Usage
add_filter('get_sample_permalink', 'your_custom_function', 10, 5); function your_custom_function($permalink, $title, $name, $post_id, $post) { // your custom code here return $permalink; }
Parameters
$permalink
(array) – Array containing the sample permalink with a placeholder for the post name, and the post name.$title
(string) – Post title.$name
(string) – Post name (slug).$post_id
(int) – Post ID.$post
(WP_Post) – Post object.
More information
See WordPress Developer Resources: get_sample_permalink
Examples
Modify the permalink structure for a custom post type
This code changes the permalink structure for a custom post type called ‘product’ by adding a custom prefix ‘shop’.
add_filter('get_sample_permalink', 'modify_product_permalink', 10, 5); function modify_product_permalink($permalink, $title, $name, $post_id, $post) { if ($post->post_type == 'product') { $permalink[0] = str_replace('%postname%', 'shop/%postname%', $permalink[0]); } return $permalink; }
Add the post date to the permalink
This code adds the post date (in ‘Y/m/d’ format) before the post name in the permalink.
add_filter('get_sample_permalink', 'add_post_date_to_permalink', 10, 5); function add_post_date_to_permalink($permalink, $title, $name, $post_id, $post) { $date = get_the_date('Y/m/d', $post_id); $permalink[0] = str_replace('%postname%', $date . '/%postname%', $permalink[0]); return $permalink; }
Change the post name in the permalink
This code replaces the post name with the post ID in the permalink.
add_filter('get_sample_permalink', 'change_post_name_in_permalink', 10, 5); function change_post_name_in_permalink($permalink, $title, $name, $post_id, $post) { $permalink[0] = str_replace('%postname%', $post_id, $permalink[0]); return $permalink; }
Add a custom prefix to post permalinks
This code adds a custom prefix ‘blog’ to the permalinks of ‘post’ post type.
add_filter('get_sample_permalink', 'add_custom_prefix_to_post_permalink', 10, 5); function add_custom_prefix_to_post_permalink($permalink, $title, $name, $post_id, $post) { if ($post->post_type == 'post') { $permalink[0] = str_replace('%postname%', 'blog/%postname%', $permalink[0]); } return $permalink; }
Modify permalink for a specific category
This code adds a custom prefix ‘featured’ to the permalinks of posts in the ‘featured’ category.
add_filter('get_sample_permalink', 'modify_permalink_for_featured_category', 10, 5); function modify_permalink_for_featured_category($permalink, $title, $name, $post_id, $post) { if (has_category('featured', $post_id)) { $permalink[0] = str_replace('%postname%', 'featured/%postname%', $permalink[0]); } return $permalink; }
Remove parent slug from child page permalink
This code removes the parent page slug from the permalink of child pages, resulting in a flat URL structure.
add_filter('get_sample_permalink', 'remove_parent_slug_from_child_page_permalink', 10, 5); function remove_parent_slug_from_child_page_permalink($permalink, $title, $name, $post_id, $post) { if ($post->post_type == 'page' && $post->post_parent) { $parent_slug = get_post_field('post_name', $post->post_parent); $permalink[0] = str_replace($parent_slug . '/', '', $permalink[0]); } return $permalink; }