Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use post_link filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
November 8, 2022
5 minutes read

post_link filter

Filters the permalink for a post.

apply_filters( 'post_link', string $permalink, WP_Post $post, bool $leavename )

Description

This hook is used for filter the permalink for a post. Its applies only to posts with default post_type of ‘post’, and can’t be applies to custom posts.

post_link is a filter applied to the permalink URL for a post prior to returning the processed url by the function get_permalink().

This filter only applies to posts with post_type of ‘post’. For that filter which applies to custom post type look post_type_link.

Parameters

  • $permalink : (string) The post’s permalink.
  • $post : (WP_Post) The post in question.
  • $leavename : (bool) Whether to keep the post name.

Live Example

To run the hook, copy the example below.

 $permalink = apply_filters( 'post_link', $permalink, $post, $leavename ); 
                         
if ( !empty( $permalink ) ) { 
                         
   // everything has led up to this point... 
                         
} 
  

The following example is for adding a hook callback.

// define the post_link callback 
function filter_post_link( $permalink, $post, $leavename ) { 
    // make filter magic happen here... 
    return $permalink; 
}; 
         
// add the filter 
add_filter( 'post_link', 'filter_post_link', 10, 3 ); 

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'post_link', 'filter_post_link', 10, 3 ); 
Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.