Set wp_title to change title tag from plugin? Set wp_title to change title tag from plugin? wordpress wordpress

Set wp_title to change title tag from plugin?


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;}


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


Wordpress complains about the 2nd parameter because I assume the function is used in a few places where the function is simply called with 1 paramter just like it is now.

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

I assume you add more logic to the function, but with this way the 2nd parameter is 'defined'. This is not really 'neat' code btw.

For your second question; yes you can obviously store something in an object. It just matters where you do this to see if its accessible by an other part of your code.