|
|
Many of our clients have buttons implemented via parent theme shortcodes. One of our partners asked if we could do some event tracking on the buttons because they were great calls to action (CTAs) on all pages. The button shortcode we use will automatically create a nice button by adding classes to the output HTML.
Here is the shortcode to create a button to visit my site:
This has the outputs:
Visit DK New Media
Click : UK mobile number database
Add GA4 event measurement to button shortcode
We want to update the HTML output to automatically add Google Analytics 4 event tracking when the button is clicked:
The button shortcode was created in our parent theme, so we don't want to modify our original theme to accommodate the change, as those changes would be lost if we update the theme. However, there is a solution! The WordPress API allows you to remove a shortcode using the remove_shortcode function!
Get More Info click here : 
This can be achieved in a child theme file or by using code in a custom function. I recommend deploying all your content-related shortcodes in your own plugin, not in your theme file. Tools like AMP don't render shortcodes in themes.
I would recommend creating a custom plugin for your website that will include all your shortcodes, even the ones included in your theme. Do this:
Create a folder with a unique name. For example, ours is called
Add a file to this file. (You can name it whatever you want)
You can add the code above to your shortcodes file in addition to the plugin information that will be displayed on your plugins page:
If you have more than one shortcode that you want to replace with a custom plugin, you can remove and add more shortcodes in the above function.
Zip the folder and you can now upload and activate the plugin via the WordPress plugins menu.
|
|