Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 136|回复: 0

WordPress: How to override shortcode from parent theme in your child theme or...

[复制链接]

2

主题

2

帖子

10

积分

新手上路

Rank: 1

积分
10
发表于 2024-4-24 01:33:41 | 显示全部楼层 |阅读模式
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.

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2026-3-25 15:33 , Processed in 0.053255 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表