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 to filter the permalink for a post. It applies only to posts with the default post_type of ‘post’, and can’t be applied to custom posts.

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

This filter only applies to posts with the post_type of ‘post’. For a filter that applies to custom post types, look at 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 Examples

Example 1: Running the Hook

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... 
} 

Example 2: Adding a Hook Callback

The following example shows how to add a hook callback.

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

Example 3: Removing a Hook Callback

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'post_link', 'weplugins_filter_post_link', 10, 3 ); 

Access Premium WordPress Plugins

Contact Us

If you need any customization, feel free to contact us.

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.