MFirenzimax
MFirenzimax

Reputation: 39

Simple wp action & filter hook

I know this is basic stuff, but I am just starting to learn action and filter hooks by building a simple plugin (everyone starts somewhere!) with a simple action hook to add content and a filter hook to change it.

Here is my action hook in the plugin file:

function sushi_add_a_title(){

    $title = 'hello world!';
    echo $title;
}
add_action( 'wp_head', 'sushi_add_a_title' );

and the filter hook in functions.php:

function sushi_change_the_title( $title ){

    $title = 'hi world!';
    return $title;
}
add_filter( 'wp_head', ‘sushi_change_the_title' );

I was expecting the output to change from ‘hello world’ to ‘hi world!’... but no change.

What am I doing wrong?

Upvotes: 1

Views: 172

Answers (2)

MFirenzimax
MFirenzimax

Reputation: 39

Well I think I have come up with a solution, and using str_replace.

function sushi_add_a_title(){
  $title = 'hello world!';
  echo apply_filters('a_nice_title' , $title );
}
add_action( 'wp_head', 'sushi_add_a_title');

add_filter('a_nice_title', 'replace_string');
function replace_string($title){
  $title = str_replace("hello world!", "Hi world!", $title);
  return $title;
}

Upvotes: 1

Hollings
Hollings

Reputation: 532

You have a "curly quote" () in your add_filter line that might be breaking your PHP.

add_filter( 'wp_head', ‘sushi_change_the_title' );

Upvotes: 0

Related Questions