Applying a filter in a wordpress website

You can apply a filter in a wordpress website easily

Suppose you want your name to be added to the end of site title you can use a filter

  1. Use add_filter function to register the filter function
  2. Define the filter function which takes the input parameter as value to be filtered
  3. Return the filtered value of the function
 PHP |  copy code | ?
1
<?php
2

3
add_filter('wp_title', 'title_filter',10,2);
4

5
function title_filter($title,$postattr){
6
$title=$title." | Myname";
7
return $title;
8
}
9

Here in the add_filter function parameter 3 signifies the weight of the plugin and 2 defines the number of parameters it takes

You can add a function in the class too.. using the array attribute

 PHP |  copy code | ?
01
<?php
02

03
add_filter('wp_title',array('MyClass','title_filter'),10,2);
04

05
class MyClass{
06
  function title_filter($title,$postattr){
07
    $titile=$title." | ". "Myname";
08
    return $title;
09
    }
10
}