0

I highly need a piece of code which will replace a simple word with a hypertext link, using str_replace method. And fortunately I found this code below, but the problem is that it repeats the link with all matching keywords, whereas I want to add the link only once and forget other matching keywords.

For example, if it sees "nursery rhymes", replace it with a hypertext link, but do not add the link again with "nursery rhymes", "rhymes" or "poems" keywords on same page. One URL only one time.

I also heard that it's probably not possible with str_replace, but possible with preg_replace, which could limit the occurrence of replacements.

Please help. I am not very familiar with PHP or coding, so please use complete code, so I can simply copy and paste in my functions.php. Here is the code I am using:

function wp_affiliate_links($text) {
    $replace = array(
        ' nursery rhymes ' => ' <a href="http://www.nurseryrhymes.me/">nursery rhymes</a> ',
        ' poems ' => ' <a href="http://www.nurseryrhymes.me/">poems</a> ',
        ' rhymes ' => ' <a href="http://www.nurseryrhymes.me/">rhymes</a> ',
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
}
add_filter('the_content', 'wp_affiliate_links');
add_filter('the_excerpt', 'wp_affiliate_links');
  • Check this thread – Nilambar Sep 3 '14 at 6:45
  • but I don't know how to use it Nilambar... Blue Clothes, I know meaning of your name, I also from Bharat.... – Tejinder Sep 3 '14 at 7:20
  • Yes, Nilambar means Blue clothes :-) I am from Nepal BTW. Ok, have a great day! – Nilambar Sep 3 '14 at 11:11
0

Can't you pass limit number of replacements in str_replace? Like that

$text = str_replace(array_keys($replace), $replace, $text, 1);

EDIT:

Sorry I was thinking about something else and missed this one. You can use preg_replace and set limit in it. Like this one.

function wp_affiliate_links($text){
    $replace = array(
        '/ nursery rhymes /' => ' <a href="http://www.nurseryrhymes.me/">nursery rhymes</a> ',
        '/ poems /' => ' <a href="http://www.nurseryrhymes.me/">poems</a> ',
        '/ rhymes /' => ' <a href="http://www.nurseryrhymes.me/">rhymes</a> ',
    );
    $text = preg_replace( array_keys($replace), $replace, $text, 1 );
    return $text;
}
add_filter('the_content', 'wp_affiliate_links');
add_filter('the_excerpt', 'wp_affiliate_links');

EDIT 2:

Try this one.

function wp_affiliate_links($text){
    $replace = array(
        '/ nursery rhymes /' => ' <a href="http://www.nurseryrhymes.me/">nursery rhymes</a> ',
        '/ poems /' => ' <a href="http://www.nurseryrhymes.me/">poems</a> ',
        '/ rhymes /' => ' <a href="http://www.nurseryrhymes.me/">rhymes</a> ',
    );
    foreach ( $replace as $key ) {
        $text = preg_replace( array_keys($replace), $replace, $text, 1 );
        return $text;
    }
}
add_filter('the_content', 'wp_affiliate_links');
add_filter('the_excerpt', 'wp_affiliate_links');
  • No it does not work, showing Fatal error: Only variables can be passed by reference in .. * .. function.php on line 537 – Tejinder Sep 3 '14 at 6:12
  • See edits. I used preg_replace for this. – Robert hue Sep 3 '14 at 6:38
  • Wow, but It is checking only keywords not url. I tell you how? It's Replacing all three keywords i.e. "nursery rhymes", "poem" and "rhymes" with hyperlink three times, whereas I want to add the link only once. If once the keyword replaced with one link, ignore other keywords. – Tejinder Sep 3 '14 at 6:57
  • Try edit 2 and let me know. – Robert hue Sep 3 '14 at 7:03
  • 1
    I will be highly thankful of you for that Robert, I have nothing else to say Thank you very much, not even enough reputations to like your answers. I must when I will got. Thank you again... waiting for solutions... – Tejinder Sep 3 '14 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.