How to Modify Title Tag in WordPress

I always missed a function to directly change the title tag in WordPress template. But here is a function which can do that for you.

function modify_title( $new_title ) {
    add_action( 'pre_get_document_title', function( $current_title ) use( $new_title ) {
        return $new_title;
    }, 9999 );
}

Please note that this function uses use keyword to bind variables into anonymous function’s scope. Learn more

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *