Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set wp_title to change title tag from plugin?

I have created a WP plugin which uses the query string to pull in page data based on what the visitor has selected. Obviously this 'simulates' additional pages but the page title does not change from the title set in WP Admin.

I have been trying to hook into wp_title to change the title tag on fly but can't get this one working.

The following function works:

public function custom_title($title) {
    return 'new title';
}
add_filter( 'wp_title', array($this, 'custom_title'), 20 );
// changes <title> to 'new title'

As soon as I try to pass a variable to it, it fails.

public function custom_title($title, $new_title) {
    return $new_title;
}

WordPress complains it's missing the 2nd argument, I guess this makes sense since the function is being called at page load... I was hoping I could do something like $this->custom_title($title, 'new title); within my plugin but it doesn't look like that is going to be possible?

I have posted this here because I think it's a general PHP class issue.

Can I globalise a returned variable, e.g. I want to return the 'title' column from a query in another function such as $query->title

When the function runs it returns data from the database

public function view_content()
{
  $query = $this->db->get_row('SELECT title FROM ...');
  $query->title; 
}

I now need $query->title to be set as the page title.

public function custom_title()
{
  if($query->title)
  {
    $new_title = $query->title;
  }
}
like image 305
Goodbytes Avatar asked Jul 15 '15 21:07

Goodbytes


2 Answers

It looks like you may have misunderstood how the filter mechanism works. A filter is a function WordPress calls with certain parameters at a certain time and retrieves the result. Here is a decent introduction to WordPress filters: http://dev.themeblvd.com/tutorial/filters/

You may also want to check out the documentation page for the wp_title filter in particular, so you would understand what arguments your function should expect: https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_title

The code that does what you want would look something like this:

public function __construct() {
    //...
    add_filter( 'wp_title', array($this, 'custom_title'), 20);
}

public function view_content() {

    $query = $this->db->get_row('SELECT title FROM ...');
    $this->page_title = $query->title; 
}

public function custom_title($title) {

    if ($this->page_title) {
        return $this->page_title;
    }

    return $title;
}
like image 61
emb Avatar answered Nov 20 '22 20:11

emb


Action and filter hooks allows you to change something generated by Wordpress at a certain point of program execution. These custom changes are made inside a function that is attached to a specific hook.

Parameters passed to a function attached are originally generated by Wordpress, the first parameter is a value to change and return, in case of the_title hook it is the title of the page.

Since the same filter can be used multiple times that value can be modified in other functions attached, when exactly your function will have its turn depends on the priority defined and the order in which they are added to the filter.

The difference between filters and actions is that in the first case you need to return a value ( modified or original ), while the actions are some sort of triggered events where you can, for example, print something. Of course, you can also define and trigger your own custom actions and filters.

Filter can be added at any time before it is applied, and function hooked can be in the form of anonymous function like in example below.

    public function view_content() 
    {
        $query = $this->db->get_row( 'SELECT title FROM ...' );

        add_filter( 'wp_title', function( $title ) use ( $query ) {
            return $query->title;
        }, 20 );
    }

Or you can save the value as an object property and use it later.

    public function view_content() 
    {
        $query = $this->db->get_row( 'SELECT title FROM ...' );     
        $this->title = $query->title;

        add_filter( 'wp_title', array( $this, 'custom_title' ), 20 );
    }

    public function custom_title( $title ) 
    {
        return $this->title;
    }

WP Plugin API
PHP Anonymous functions
PHP Class properties

like image 30
Danijel Avatar answered Nov 20 '22 19:11

Danijel