Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. function link_words( $text ){
  2. //regular expression to verify if text contains tag a
  3.  
  4.  
  5. //array of words and links
  6. $words_to_replace = array(
  7. 'guanciale' => '<a href="#">guanciale</a>',
  8. 'lattuga' => '<a href="#">lattuga</a>'
  9. );
  10.  
  11.  
  12.  
  13. foreach ($words_to_replace as $old_word => $new_word)
  14. {
  15. $reg =sprintf('/<a [^>]+>([^<]+)+[%s]+<\/a>/',$old_word);
  16. if(!preg_match_all($reg, $text)){
  17. $text = preg_replace("/$old_word/", $new_word, $text, 1);
  18. echo '<h1>Ho messo il link</h1>';
  19. }
  20.  
  21. return $text;
  22. }
  23.  
  24.  
  25.  
  26. add_filter( 'the_content', 'link_words' );
  27. add_filter( 'the_excerpt', 'link_words' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement