does WP offer any functions which can be used to set the page title and/or keywords from the PHP/Controller level?

I'm basically looking for something like the wp_enqueue_style() function, which results in changes to the HTML page header but for the title and keywords fields. I have some PHP code which dynamically retrieves/builds the page title and would like to avoid having to alter my theme/templates to display this.

Greetings/Thanks Robert

1 Answer 1


You can use the wp_title filter to modify the HTML document title, and you can use the wp_head action to hook in a custom HTML meta link. e.g.:

function wpse46249_filter_wp_title( $title ) {
    // $title contains the default document title
    // output by WordPress. Modify it here however
    // you want.
    $some_custom_title_content = 'CUSTOM TITLE CONTENT HERE';
    // Now append it to the default
    $custom_title = $title . $some_custom_title_content;
    // Then return the result
    return $custom_title;
add_filter( 'wp_title', 'wpse46249_filter_wp_title' );

function wpse46249_add_meta_link() {
    echo '<meta name="keywords" content="INSERT KEYWORDS HERE" />';
add_action( 'wp_head', 'wpse46249_add_meta_link' );

These callbacks can be added to a Plugin, to keep them safe from Theme switching.

For further reference, I would recommend taking a look at how the popular SEO Plugins implement these extensions.

  • @user13955 Here's links to the filter and action hooks references from WordPress for more of a foundation on Chip's answer.
    – Brent
    Mar 20, 2012 at 20:16
  • Thank you very much, I'll give this a try tomorrow morning. This seems to be exactly what I was looking for!
    – user13955
    Mar 20, 2012 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.