Override Shortcode in WordPress

The Shortcodes in WordPress is a small piece of code that allows you to various functions with less effort. We are going to discuss how to overwrite/rewrite predefined shortcodes.

Overwrite Shortcode

Let’s say we have a shortcode [service_list] that display various services on the frontend. But we need to add extra functionality or modify the existing functionality to match perfectly with the current UI (User Interface).

We are using a child theme, that override the parent theme shortcode functionality. No matter if the shortcode is defined in the parent theme or in the plugin. We need to use the wp_head action hook to override our shortcode.

add_action('wp_head','override_common_shortcodes');
function override_common_shortcodes() {

    remove_shortcode('service_list');
    add_shortcode('service_list', 'cpc_services_list_item_shortcode');
}

We have used the remove_shortcode() function to remove the shortcode and add_shortcode() to override it according to needs.

Now define your codes in the cpc_services_list_item_shortcode function.

function cpc_services_list_item_shortcode(atts) {
    extract(shortcode_atts(array(
        ....
        ....
    ), $atts));

    // Customize according to need
    $html = '';

    return $html;
}

Posted by Jogesh Sharma

Jogesh Sharma is a web developer and blogger who loves all the things design and the technology, He love all the things having to do with PHP, WordPress, Joomla, Magento, Durpal, Codeigniter, jQuery, HTML5 etc. He is the author of this blog.