Reputation: 5479
function replace_text_wps($text){
$replace = array(
// 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
'wordpress' => '<a href="#">wordpress</a>',
'excerpt' => '<a href="#">excerpt</a>',
'function' => '<a href="#">function</a>'
);
$text = str_replace(array_keys($replace),
$replace, $text);
return $text; }
add_filter('the_content','replace_text_wps');
add_filter('the_excerpt','replace_text_wps');
This code is used to replace some words, why is he using add_filter() function twice, is he wrong?
Also, what does the line $text = str_replace(array_keys($replace), $replace, $text)
mean?
Upvotes: 0
Views: 1502
Reputation: 50019
add_filter('the_content','replace_text_wps');
add_filter('the_excerpt','replace_text_wps');
He's applying the filter to the content of the post as well as the excerpt (Usually separate from the post body. Separately filled in). Generally you only use one of these in a blog listing so he's covering all his bases by applying it to both.
$text = str_replace(array_keys($replace), $replace, $text);
// 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
Then, he's just doing a string replace : http://php.net/manual/en/function.str-replace.php
Basically, if your post content has any of the following words wordpress, excerpt, excerpt
it will replace that word with a link that is wrapped arounf the word.
Upvotes: 1
Reputation: 318518
$text = str_replace(array_keys($replace), $replace, $text);
This line searches for all array keys from $replace
and replaces them with their respective value.
It's basically a shorter/nicer way for foreach($replace as $s => $r) $text = str_replace($s, $r, $text);
Upvotes: 1
Reputation: 21449
$text = str_replace(array_keys($replace), $replace, $text);
Replaces all given keys with $replace in $text string
this code is just filtering two different strings.
Upvotes: 1