Search code examples
wordpresswordpress-shortcode

What is wrong with this shortcode function?


I am trying to create a shortcode that will add a CTA button to any page with onClick event tracking code. This is what I have so far, but it doesn't work.

<?php
function my_cta() {
    return `<div class="mycalltoaction"><a class="button" href="/contact-us/" onClick="_gaq.push(['_trackEvent', 'QuoteRequest', 'Click', '`.get_the_title().`']);">Request a Quote <strong>TODAY</strong></a></div>`;
}
add_shortcode('cta', 'my_cta');

If I add the shortcode [cta] to a page before adding this custom function, I see the shortcode on the front end as text. When I add the function, the page no longer displays the shortcode on front end. It doesn't display anything. When I view the source HTML, there is nothing there where the shortcode tag was inserted.

I got the code from another contributor and see it contains backticks. Are those the problem, or what?


Solution

  • I revised your code.

    function my_cta() {
        return '<div class="mycalltoaction"><a class="button" href="/contact-us/" onClick="_gaq.push([\'_trackEvent\', \'QuoteRequest\', \'Click\', '.get_the_title().'] );">Request a Quote <strong>TODAY</strong></a></div>';
    }
    add_shortcode('cta', 'my_cta');