skip to Main Content

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?

2

Answers


  1. 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');
    
    Login or Signup to reply.
  2. 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');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search