Change page title from plugin

Is possible to change page title on the fly from plugin?

I’ve try global $post, but seem like plugin runs after.

Any Ideas?

Edit:
Im writting some pages on the fly, based on same page / post, so every page show the same title.
Looking a way to do via shortcode or writting my own plugin/function

2 Answers
2

There’s a filter for that:

function wpse_alter_title( $title, $id )
{
    // $id = $post->ID;
    // alter the title here
    return $title;
}

If you want to alter the “Protected” and “Private” titles, then you need other filters:

// Preserve the "%s" - else the title will be removed.
function wpse_alter_protected_title_format( $title )
{
    return __( 'Protected: %s' );
}

function wpse_alter_private_title_format( $title )
{
    return __( 'Private: %s' );
}

Last but not least, you must add your filter callbacks early enough.

function wpse_load_alter_title()
{
    add_filter( 'the_title', 'wpse_alter_title', 20, 2 );
    add_filter( 'protected_title_format', 'wpse_alter_protected_title_format' );
    add_filter( 'private_title_format', 'wpse_alter_private_title_format' );
}
add_action( 'init', 'wpse_load_alter_title' );

Leave a Comment