Search

Override Parent Theme Shortcode in WordPress Child Theme

    Akshaya Rane
Listen to this article

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.

[space]

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]
[space]

[space]

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.

 

Picture of Akshaya Rane

Akshaya Rane

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads