• Hi! I’ve read a lot of the support requests and I just can’t quite figure out what I need to do to fix this.

    I have a custom shortcode and all it does is display the tag name and tag description for all tags on a post. I specifically need the tag descriptions to be shown on the page and searchable.

    This shortcode is incompatible with the Relevanssi shortcode indexing (I disabled it and found that everything else works, just this one is the issue), but I’m not sure how to fix it since I do need this data to be searchable.

    Here’s the shortcode which I have in the functions.php file. I’m fairly new to PHP so my apologies if I’m missing something obvious!

    function post_tags_test()
    	{
    	$tags = get_the_tags();
    	
    	echo 'Tags: <br>';
    		{
    			foreach ($tags as $tag) 
         		{
         		   	echo '<p>' . $tag->name . ' ' . $tag->description . '</p><br>';
    		     }
     		}
     	echo '<br>';
     	}
     
    add_shortcode ( 'tag_description', 'post_tags_test');
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Mikko Saari

    (@msaari)

    The problem here is that shortcodes should never ever echo anything out. A shortcode should always return whatever it outputs.

    So it should be:

    function post_tags_test() {
        $tags   = get_the_tags();
        $result = 'Tags: <br>';
        foreach ( $tags as $tag ) { 
            $result .= '<p>' . $tag->name . ' ' . $tag->description . '</p><br>';
        }
        $result .= '<br>';
        return $result;
    }
    Thread Starter digitaljenm

    (@digitaljenm)

    Thank you so much! I created a page with a standard search bar and I can now search within the tag descriptions.

    There’s one other compatibility issue but I think that’s within the search plugin (Search and Filter Pro) that we’re using. The search bar that they use is tied to the standard search bar and it looks like it is recognizing the content, but not actually displaying the results. I’ll see if I can get that sorted out, but if you happen to know a solution there, let me know.

    Plugin Author Mikko Saari

    (@msaari)

    Search & Filter Pro should work well with Relevanssi and use the Relevanssi compatibility mode automatically. So sorry, but I don’t know why it’s not working.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Fix shortcode to work with Relevanssi’ is closed to new replies.