Technology WordPress Tips & Tricks

Override Parent Theme Shortcode in WordPress Child Theme

Overriding a parent theme's shortcode from a child theme keeps your customizations clean and update safe. Here is how to do it, so you can modify behavior without editing the parent theme that updates will overwrite.

Akshaya Rane Akshaya Rane 2 min read
Override Parent Theme Shortcode in WordPress Child Theme

Customising ThemesThe purpose of a creating a child theme is to modify the features of the parent theme. That said, I feel that the easiest feature to modify or extend, is the styling provided by the parent theme. Of course, if there are a sufficient number of actions and filters provided, we can comfortably tweak several functions of the parent theme.

I was working on a theme modification project, where I had to change the behaviour of a shortcode in the theme. I had already created a child theme, and all I had to do, was to add the following code in my child theme’s functions.php.

Overriding Parent Shortcode Functions in Child Theme

[pre]add_action(‘init’, ‘remove_parent_theme_shortcodes’);

function remove_parent_theme_shortcodes() {

// remove shortcode from parent theme
// shortcode_name should be the name of the shortcode you want to modify
remove_shortcode( ‘shortcode_name’ );

// add the same shortcode in child theme with our own function
// note to self: think of a better example function name
add_shortcode( ‘shortcode_name’, ‘wdm_child_theme_shortcode_name’ );
}

function wdm_child_theme_shortcode_name( $atts ) {
// write your shortcode function here.
}[/pre]

Well, in case your shortcode function is completely different than the parent theme’s function, this method should work perfectly. But if you were modifying the parent theme function and writing the code here, be sure to check any changes in the original function when the parent theme is updated.

 

Get a FREE Consultation

Let's build something that lasts.

Share what's on your mind — a clear brief, a half-formed idea, or just a sense that something needs to change. We'll listen first, ask the right questions, and point you toward what's actually worth building.

We take on a handful of projects each quarter,ones where we can truly make a difference.

  • Receive a human response within 24 hours
  • Get a detailed scope and quote upfront
  • We're happy to sign an NDA upon request

    Free 30-Min Strategy Call

    Your Name *

    Your Phone No *

    Work Email *

    Your Budget*

    Project Details *