apply_filters( ‘page_link’, string $link, int $post_id, bool $sample )

Filters the permalink for a page.

Parameters

$linkstring
The page’s permalink.
$post_idint
The ID of the page.
$samplebool
Is it a sample permalink.

Source

return apply_filters( 'page_link', $link, $post->ID, $sample );

Changelog

VersionDescription
1.5.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Customizing Permalinks for WordPress Pages Based on Categories
    Learn how to dynamically change WordPress page permalinks based on assigned categories. This tutorial will guide you through the process of creating a custom function that checks if a page belongs to a specific category (e.g., “example”) and modifies its permalink structure accordingly. You’ll understand the usage of WordPress hooks and functions like get_the_category() and add_filter() to achieve this customization.

    function wpdocs_tutorials_permalink( $permalink, $post ) {
        // Ensure we're dealing with a page
        if ( 'page' !== get_post_type( $post ) ) {
            return $permalink;
        }
    
        // Get the categories assigned to the page
        $categories = get_the_category( $post->ID );
    
        // Check if any of the categories is "example"
        foreach ( $categories as $category ) {
            if ( 'example' === $category->slug ) {
                return home_url( '/example/' . $post->post_name . '/' );
            }
        }
    
        // Return default permalink if no match
        return $permalink;
    }
    
    add_filter( 'page_link', 'wpdocs_tutorials_permalink', 10, 2 );

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