draney
draney

Reputation: 51

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?

Upvotes: 0

Views: 80

Answers (2)

jignesh886660
jignesh886660

Reputation: 31

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

Upvotes: 0

Bhautik
Bhautik

Reputation: 11282

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');

Upvotes: 1

Related Questions