apply_filters( ‘pre_post_link’, string $permalink, WP_Post $post, bool $leavename )

Filters the permalink structure for a post before token replacement occurs.

Description

Only applies to posts with post_type of ‘post’.

Parameters

$permalinkstring
The site’s permalink structure.
$postWP_Post
The post in question.
$leavenamebool
Whether to keep the post name.

Source

$permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );

Changelog

VersionDescription
3.0.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Modifying Pre-Post Links in WordPress
    we will learn how to modify the default permalink structure of your WordPress posts using the pre_post_link filter. We will write a function to customize the permalink and then add and remove this filter.

    Step 1: Add the Filter

    function wpdocs_modify_pre_post_link_defaults( $permalink, $post ) {
        // Check if the post type is 'post'
        if ( 'post' === $post->post_type ) {
            // Append '-custom' to the permalink
            $permalink = str_replace( '%postname%', '%postname%-custom', $permalink );
        }
    
        return $permalink;
    }
    
    add_filter( 'pre_post_link', 'wpdocs_modify_pre_post_link_defaults', 10, 2 );

    Step 2: Remove the Filter

    remove_filter( 'pre_post_link', 'wpdocs_modify_pre_post_link_defaults' );

You must log in before being able to contribute a note or feedback.