Jump to content

Recommended Posts

Hallo PHP geeks

I need to override a parent-theme shortcode in Travelo from a child theme; I can edit the parent theme files just fine, but when I try to override the shortcode function from the child theme, nothing seems to happen, regardless of which hook I use to do so.

I can tell that my filter function (add_new_child_shortcodes) is called, because when I echo directly from the filter, I do get my output. This fact indicates that the remove_shortcode function is called at the wrong time, and the parent function is never removed.
From experimenting I know that the child-theme functions.php file is called before the parent, but according to users on stackoverflow, you should still be able to override the shortcode by using the right hook.

I already tried various proposed hooks from stackoverflow and other websites, but none of them worked in my case. This includes the following hooks:

  • after_setup_theme
  • init
  • wp_loaded

Also, I can not seem to figure out how to get a list of the loaded shortcodes, to see if the shortcode I am trying to override has actually been loaded. I tried print_r($shortcode_tags) within my filter, but this is empty when called from within a function, and I am also not sure that is the right variable. It does output a bunch of stuff when used outside though, but obviously not the relevant stuff - presumably because it has not yet been loaded.

This is my code so far:
 

add_action('after_setup_theme', 'add_new_child_shortcodes');
function add_new_child_shortcodes() {
  remove_shortcode('shortcode_search_group');
  add_shortcode('shortcode_search_group', 'child_shortcode_search_group');
}
function child_shortcode_search_group() {
    echo 'hallo';exit();
}

I specifically need to replace the shortcode_search_group shortcode, because I need to make modifications to the search HTML.

If it is of any help, when I examine the relevant file in the parent theme (inc/functions/shortcodes.php), I can tell that shortcodes themselves are added with the following code:
 

    function add_shortcodes() {
        foreach ( $this->shortcodes as $shortcode ) {
            $function_name = 'shortcode_' . $shortcode ;
            add_shortcode( $shortcode, array( $this, $function_name ) );
        }
        // to avoid nested shortcode issue for block
        for ( $i = 1; $i < 10; $i++ ) {
            add_shortcode( 'block' . $i, array( $this,'shortcode_block' ) );
        }
        add_shortcode( 'box', array( $this,'shortcode_block' ) );
    }

The $this->shortcodes variable is an array of shortcode names, corresponding with the method names in the class.

Help will be much appreciated!

Edited by JacobSeated

I am making some progress in debugging this. Of course I did not mean "filter", as I am trying to override an existing shortcode.

Anyway, I managed to verify that the shortcode has been added. I noticed the parent theme is using the init hook to add shortcodes. Then I tried print_r($shortcode_tags) again after using the init hook in my child theme, with a priority of 20, and I was finally able to verify that the shortcode is loaded. I.e.:
 

add_action('init', 'replace_parent_theme_features', 20);
function replace_parent_theme_features() {
  global $shortcode_tags;
  print_r($shortcode_tags);
  exit();
  remove_shortcode('shortcode_search_group');
  add_shortcode('shortcode_search_group', 'child_shortcode_search_group');
}

Apparently the priority is also important.

I have a feeling I am getting close, but I still can not get remove_shortcode to work for some reason.

Edited by JacobSeated

Hallo again, it turns out I was on a completely wrong track.

If the Theme is object orientated, then it may have something like this as the top of each PHP class:
 

if ( ! class_exists( 'TravShortcodes' ) ) :

If that is the case, then it will only apply the parent class if it has not already been included.

This means that you should just copy the file over to the child theme folder, and then include it from your functions.php:

include_once get_stylesheet_directory_uri() . 'shortcodes.php';

This way you can easily modify any aspect of the parent that you want.

It is important you remember to include the file, since simply copying it is not going to be enough.

Thanks. This is not solved.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.