Support » Plugin: Opt-Out for Google Analytics » Add Class to Shortcode

  • Is it possible to add a class to the shortcode? I want to style the link created by the shortcode as a button, to match the other buttons on my website. Or is the only way of achieving this by editing the plugin files?

    Thanks in advance for any help.
    Dan

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Florian Simeth

    (@floriansimeth)

    Hey Dan,
    and thanks for your question.

    Well… you could use the do_shortcode_tag filter for that. Are you familiar with PHP?

    Greetings
    Flo

    Thread Starter dancappdesign

    (@dancappdesign)

    Thanks Flo. I only have a beginner understanding of PHP but I’ll do some research to see if I can piece this filter together.

    Thread Starter dancappdesign

    (@dancappdesign)

    I’m a little in over my head here. I’ve got this far:

    function append_class_to_gaoo( $output, $tag ) {
    	if ( 'google_analytics_optout' !== $tag ) {
    		return $output;
    	}
    	return $output . ' class="button-class"';
    }
    add_filter('do_shortcode_tag', 'append_class_to_gaoo', 10, 2);

    But it simply prints ‘class=”button-class”‘ after the <a> tag. I don’t suppose you could help me place it within the <a> tag?

    Plugin Contributor Florian Simeth

    (@floriansimeth)

    You’re welcome! 😉

    And you’re almost there. I would suggest to use str_replace. Depending on the code that you use inside the shortcode, this would be one way to do it:

    return str_replace( '<a', '<a class="button-class"', $output );

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.