Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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

<?php
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.

share|improve this answer
    
@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 '12 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 '12 at 21:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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