Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 250 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Set wp_title to change title tag from plugin?

#1
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;
}
}
Reply

#2
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.
Reply

#3
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:

[To see links please register here]


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:

[To see links please register here]


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

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

<hr>

[**WP Plugin API**][1]<br>
[**PHP Anonymous functions**][2]<br>
[**PHP Class properties**][3]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through