0

I am trying to replace tag keyword with link within post content. below is my code but it is making the whole content to disappear, I dont really know where i am getting it wrong.

function link_words( $text ) {
            $tags = get_tags();
            if ( is_single() && in_category( 'Tutorial' )) {
                foreach ( $tags as $tag ) {
                    $from = '!<h2>[^<>]*<\/h2>(*SKIP)(*F)|<b>[^<>]*<\/b>(*SKIP)(*F)|<a\b[^>]*>.*?</a>(*SKIP)(*F)|(\b'.$tag->name.'\b)(?=[^>]*(?:<|$))!';
                    $to = sprintf( ' <a href="%s">%s</a> ', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
                    $text = preg_replace($from, $to , $text, 5);
                }
            }
            return $text;
        }
        add_filter( 'the_content', 'link_words' );

Please Gurus in the house help me out, I am new to coding wp funtions

2
  • 1
    explode() the content, then work with the array instead of talking nonsense about huge regexes. – Max Yudin Jul 1 '18 at 11:27
  • Can You help me fix the code up there ? – Godwin Alex Ogbonda Jul 1 '18 at 18:20

Your Answer

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

Browse other questions tagged or ask your own question.