• Home
  • Contact
No Result
View All Result
Blogs
  • Home
  • Contact
No Result
View All Result
Blogs
No Result
View All Result

WordPress: How To Overwrite A Shortcode From A Parent Theme In Your Child Theme Or Custom Plugin

admin by admin
January 26, 2024
in Marketing Automation
Share on FacebookShare on Twitter


Many of our clients have buttons implemented through parent theme shortcodes. One of our partners asked if we could do some event tracking on the buttons since they were great call-to-actions (CTA) throughout the sites. The button shortcode we use automatically outputs a nice button by adding classes to the output HTML.

Here’s the shortcode to produce a button to visit my site:

[button link="https://martech.zone/partner/dknewmedia/"]Visit DK New Media[/button]

That outputs:

Visit DK New Media

And here’s how it looks:

Visit DK New Media

Add GA4 Event Tracking to a Button Shortcode

We want to update the HTML output to automatically add Google Analytics 4 Event Tracking when the button is clicked:

Visit DK New Media

The button shortcode was created in our parent theme, so we do not want to modify our original theme to accommodate the change as those changes would be lost if we updated the theme. There is a solution, though! The WordPress API enables you to remove a shortcode using the remove_shortcode function!

This can be accomplished in your child theme functions.php file or by applying the code in a custom function. I advise deploying all of your content-related shortcodes in a custom plugin rather than in a theme file. Tools like AMP do not render shortcodes in themes.

Overwrite a Shortcode In a Child Theme

In a child theme, you can remove the shortcode and replace it with our new shortcode function. You will need to search your parent theme code to find the function (called button_function_in_parent_theme below) that creates the shortcode and then you can utilize it in this snippet in functions.php:

add_action( 'after_setup_theme', 'update_button_shortcode' );

function update_button_shortcode() {
    remove_shortcode( 'button_function_in_parent_theme' );
    add_shortcode( 'button', 'new_button_shortcode' );
}

Now, you can add your new and updated shortcode function with GA4 event tracking:

function new_button_shortcode($atts, $content = null) {
    // Extract shortcode attributes
    $attributes = shortcode_atts(
        array(
            'link' => "https://feed.martech.zone/link/8998/16547832/#", // Default value if 'link' is not provided
        ), 
        $atts
    );

    $url = esc_url($attributes['link']);
    $text = esc_html($content);

    // Generate the HTML output
    $html="' . $text . '';

    return $html;
}

// Register the shortcode
add_shortcode('button', 'new_button_shortcode');

Overwrite a Shortcode Using A Custom Plugin

I’d recommend building a custom plugin for your site that incorporates all your shortcodes, even those within your theme. To do this:

  1. Create a folder that’s uniquely named. For example, ours is named mtz-shortcodes.
  2. Within that file, add a shortcodes.php file. (You can name it anything you’d like)
  3. Within the shortcodes.php file, you can add the code above in addition to the plugin information that will display on your plugins page:

add_action( 'init', 'update_shortcodes' );

function new_button_shortcode($atts, $content = null) {
    // Extract shortcode attributes
    $attributes = shortcode_atts(
        array(
            'link' => '', // Default value if 'link' is not provided
        ), 
        $atts
    );

    $url = esc_url($attributes['link']);
    $text = esc_html($content);

    // Generate the HTML output
    $html="' . $text . '';

    return $html;
}
  1. You can remove and add multiple shortcodes in the function above if you have more than one shortcode you’d like to replace using your custom plugin.
  2. Zip up the folder, and you can now upload and activate the plugin via your WordPress plugins menu.



Source link

Related Posts

Organic Social Media Reach Is Dead: How To Grow Your Following In 2025
Marketing Automation

Organic Social Media Reach Is Dead: How To Grow Your Following In 2025

Social media platforms have evolved into highly competitive spaces where only a fraction of accounts receive meaningful visibility. Platforms...

by admin
May 13, 2025
The best marketing campaigns of the year (thus far), what to learn from them, and why they worked so darn well [new data]
Marketing Automation

The best marketing campaigns of the year (thus far), what to learn from them, and why they worked so darn well [new data]

Cookie-scented body wash flew off the shelves. Shaken espressos made a comeback (as long as they’re associated with short...

by admin
May 13, 2025
Marketing Automation

Best Sizes for Social Media Images in 2025: Your Essential Guide

Need the best sizes for social media images in 2025? This guide gives you the exact dimensions for each...

by admin
May 13, 2025
Optimove Engage: “Content Chaos” | Optimove
Marketing Automation

Optimove Engage: “Content Chaos” | Optimove

See the difference control groups make in this guide Why it Matters: With Optimove Engage, marketers can deliver hyper-personalized...

by admin
May 13, 2025

POPULAR POSTS

  • Scheduling Instagram Reels: The Complete Guide

    Scheduling Instagram Reels: The Complete Guide

    0 shares
    Share 0 Tweet 0
  • How to set up Discord Server Subscriptions + Shops

    0 shares
    Share 0 Tweet 0
  • How to Use Instagram Collab Posts (+ Ideas to Boost Engagement and Reach)

    0 shares
    Share 0 Tweet 0
  • The Best Time to Post on LinkedIn in 2024 (+Heatmap Graph)

    0 shares
    Share 0 Tweet 0
  • How to Add a Link to an Instagram Story (3 Steps + Examples)

    0 shares
    Share 0 Tweet 0

© MarTechs All rights reserved.

Use of these names, logos, and brands does not imply endorsement unless specified. By using this site, you agree to the Privacy Policy

Navigate Site

  • Home
  • About
  • Services
  • Contact

Newsletter Sign Up.

Loading
No Result
View All Result
  • Home
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.