WordPress.org

Ready to get started?Download WordPress

Forums

change the title dynamically? (5 posts)

  1. gmdavis
    Member
    Posted 3 years ago #

    I am writing a plug-in and I want to change the title on a page "Search" to "Search: whatever I searched for" dynamically on page load.

    I am relatively new to wordpress and I can't find my way around the codex efficiently. I have found wp_title which seems to only be useful in a template, the_title which I still cannot figure out and others. I seem to be looking for a filter? I hope to change this in the html <title> attribute too.

    Any pointers would be helpful.

  2. Joey Kudish
    Member
    Posted 3 years ago #

    You're on the right path with wp_title http://adambrown.info/p/wp_hooks/hook/wp_title

    I suggest you read the source to understand how things are done as well as other plugins which have similar functionalities.

    The key when you want to build a plugin is to research and plan how you are going to achieve different things. Hope this points you in the right direction.

    Cheers

  3. gmdavis
    Member
    Posted 3 years ago #

    I found wp_title works on the <title> tag, and also (perhaps more specific?) post_title does the same.

    Filtering the_title will change the <h1 class="entry-title"> BUT it also changes all the tabs names equally (except the home page). I also tried post_name which does nothing I can notice. Any other suggestions?

    2 things I struggle with: finding the right hook (codex has poor documentation / organization and adambrown.info is not much better); also understanding add_filter (which I can get to work but still seems very mysterious).

  4. Joey Kudish
    Member
    Posted 3 years ago #

    Here's an example of the wp_title filter in action, taken from the WordPress SEO plugin by Yoast:

    add_filter('wp_title', array(&$this, 'wpseo_title'), 10, 3);

    First, he hooks into the wp_title filter with the wpseo_title function. Note that in this case, he's doing this inside a PHP object (class), which is why you see array(&$this, 'wpseo_title'). If this weren't inside a class, you would just use wpseo_title as the function name.

    Then he defines his wpseo_title function with all of his different paramaters. Chances are that what you are trying to achieve is a lot simpler than this.

    [Code moderated as per the Forum Rules. Please use the pastebin]

    More on hooks and filters here: http://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters

    The codex can be a bit disorganized at times, but if you search well enough, you'll almost always find what you are looking for. Better yet, is to look at the WordPress source directly. Of course, you'll need an understanding of PHP to do that.

    Good luck

  5. gmdavis
    Member
    Posted 3 years ago #

    Thanks! I will look at the WordPress SEO Plug-in code closer.

    I understand PHP fairly well, it's all the WordPress functions that are new to me. ;-D

Topic Closed

This topic has been closed to new replies.

About this Topic