Sam West
Sam West

Reputation: 41

Turning keywords into hyperlinks (problem with apostrophes)

I have a script that automatically turns keywords in my Wordpress site into hyperlinks. This works well, however one of the keywords I want hyperlinked has an apostrophe in it - and the code won't process the keyword.

I've tried:

"'Key\'word2' => '<a href="https://www.test.com/2.php" 
target="_blank">Key\'word2</a>'

but that won't work.

Can anyone suggest a work-around for this? Cheers

/***** KEYWORDS to links FUNCTION *****/

function link_words( $text ) {
$replace = array(
'Keyword1' => '<a href="https://www.test.com/1.php" 
target="_blank">Keyword1</a>',
'Key'word2' => '<a href="https://www.test.com/2.php" 
target="_blank">Key'word2</a>'
);
$text = str_replace( array_keys($replace), $replace, $text );
return $text;
}
add_filter( 'the_content', 'link_words' );
add_filter( 'the_excerpt', 'link_words' );

Upvotes: 1

Views: 43

Answers (1)

Stein Christensen
Stein Christensen

Reputation: 65

Try using a heredoc. It works just like a quote or double quote. You can use any letters, not just ABC.

<<<ABC 'Key\'word2' => '<a href="https://www.test.com/2.php" target="_blank">Key\'word2</a>' ABC>>>

Upvotes: 1

Related Questions