Set wp_title to change the title tag from the plugin?

I created a WP plugin that uses a query string to pull page data based on what the visitor selected. Obviously, this "mimics" additional pages, but the page title does not change from the set of headers in WP Admin.

I am trying to connect to wp_title to change the title tag on the fly, but cannot make it work.

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 that it skips the second argument, I think it makes sense since the function is called when the page loads ... I was hoping I could do something like $this->custom_title($title, 'new title); inside your plugin, but it doesn't look like it will be possible?

I posted this here because I consider this a common PHP class problem.

Can I globalize the returned variable, for example. I want to return the "title" column from the query in another function like $query->title

When the function starts, it returns data from the database

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

Now I need to specify $ query-> title as the page title.

 public function custom_title() { if($query->title) { $new_title = $query->title; } } 
+7
php class wordpress title
source share
3 answers

It sounds like you misunderstood how the filter mechanism works. A filter is a WordPress function that calls certain parameters at a specific time and retrieves the result. Here's a decent introduction to WordPress filters: http://dev.themeblvd.com/tutorial/filters/

You can also check the documentation page for the wp_title filter in particular so that you understand what arguments your function should expect: https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_title

The code that does what you want will 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; } 
+6
source share

The action and filter keys allow you to modify something generated by Wordpress at a certain point in the execution of the program. These custom changes are made inside a function that is bound to a specific hook.

The parameters passed to the attached function are initially generated by Wordpress, the first parameter is the value to change and return, in the case of the_title hook this is the page title.

Since the same filter can be used several times, this value can be changed in other connected functions, when exactly your function will have a turn, it depends on the specific priority 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 (changed or original), while actions are a kind of triggered events where you can, for example, print something. Of course, you can also define and run your own custom actions and filters.

A filter can be added at any time before it is applied, and the hooked function can be in the form of an anonymous function, as in the 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 a property of the object 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

+3
source share

Wordpress complains about the second parameter, because I assume that the function is used in several places where the function is simply called with 1 parameter, as it is now.

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

I assume that you added more logic to the function, but in this way the second parameter is "defined". This is actually not a "clean" code.

For the second question; yes, you can obviously store something in the object. It is simply important where you do this to find out if it is accessible by another part of your code.

0
source share

All Articles