0

I've installed a theme on my word press that uses Visual Composer. But after installing it I found out that the theme has disabled some element from Visual Composer. After quit some time i found the file that is being executed to remove these elements.

I created a child theme in order to override this function but with no luck.

the original function goes like this:

if ( ! function_exists('remove-elements-from-vc') ) {

function remove-elements-from-vc() {

    /*** Remove Grid Elements if grid elements disabled */
    vc_remove_element('vc_basic_grid');
    vc_remove_element('vc_media_grid');
    vc_remove_element('vc_masonry_grid');
    vc_remove_element('vc_masonry_media_grid');
    vc_remove_element('vc_icon');
    vc_remove_element('vc_button2');
    vc_remove_element("vc_custom_heading");

    /*** Remove unused parameters from grid elements */
    if (function_exists('vc_remove_param')) {
        vc_remove_param('vc_basic_grid', 'button_style');
        vc_remove_param('vc_basic_grid', 'button_color');
        vc_remove_param('vc_basic_grid', 'button_size');
        vc_remove_param('vc_basic_grid', 'filter_color');
        vc_remove_param('vc_basic_grid', 'filter_style');
        vc_remove_param('vc_media_grid', 'button_style');
        vc_remove_param('vc_media_grid', 'button_color');
        vc_remove_param('vc_media_grid', 'button_size');
        vc_remove_param('vc_media_grid', 'filter_color');
        vc_remove_param('vc_media_grid', 'filter_style');
        vc_remove_param('vc_masonry_grid', 'button_style');
        vc_remove_param('vc_masonry_grid', 'button_color');
        vc_remove_param('vc_masonry_grid', 'button_size');
        vc_remove_param('vc_masonry_grid', 'filter_color');
        vc_remove_param('vc_masonry_grid', 'filter_style');
        vc_remove_param('vc_masonry_media_grid', 'button_style');
        vc_remove_param('vc_masonry_media_grid', 'button_color');
        vc_remove_param('vc_masonry_media_grid', 'button_size');
        vc_remove_param('vc_masonry_media_grid', 'filter_color');
        vc_remove_param('vc_masonry_media_grid', 'filter_style');
        vc_remove_param('vc_basic_grid', 'paging_color');
        vc_remove_param('vc_basic_grid', 'arrows_color');
        vc_remove_param('vc_media_grid', 'paging_color');
        vc_remove_param('vc_media_grid', 'arrows_color');
        vc_remove_param('vc_masonry_grid', 'paging_color');
        vc_remove_param('vc_masonry_grid', 'arrows_color');
        vc_remove_param('vc_masonry_media_grid', 'paging_color');
        vc_remove_param('vc_masonry_media_grid', 'arrows_color');
    }
}

add_action('vc_after_init', 'remove-elements-from-vc');
}

And this is what I'm trying to do in my child theme in functions.php:

add_action( 'vc_after_init', 'undo-remove-elements-from-vc' );
function undo-remove-elements-from-vc(){
    remove_action( 'vc_after_init', 'remove-elements-from-vc'); 
}

the function that is causing this action is in a file called "visual-composer-config.php" and it's being loaded from a sibling file called "load.php"

if (visual_composer_installed()) {
include_once ELATED_FRAMEWORK_MODULES_ROOT_DIR.'/visualcomposer/visual-composer-functions.php';
include_once ELATED_FRAMEWORK_MODULES_ROOT_DIR.'/visualcomposer/visual-composer-config.php';
}

I don't want to edit the original theme codes in case of any theme update later on, the changes will be removed. So i need to find a solution from the Child Theme.

Thanks

Joey
  • 93
  • 2
  • 13
  • the function name in your `remove_action` call is your own function name, not the one from the parent theme... it should be `remove-elements-from-vc` – naththedeveloper Jun 05 '17 at 12:30
  • 1
    Wait... do dashes in function names even work? – naththedeveloper Jun 05 '17 at 12:32
  • My bad, i wrote the wrong function name here, in the original files i called the correct function. – Joey Jun 05 '17 at 12:33
  • the naming are just random naming, i replaced my original function names with "undo-remove-elements-from-vc" and etc ... The original functions doesn't contain "-" – Joey Jun 05 '17 at 12:34
  • Try changing the priority of your `add_action` call to earlier (the default is 10), so it looks like this: `add_action('vc_after_init', 'function_name', 5);` - 5 should be fine, it just needs to be before 10.. – naththedeveloper Jun 05 '17 at 12:38
  • No luck. The function is still being executed. – Joey Jun 05 '17 at 12:45

1 Answers1

1

Because the function in question is wrapped in if ( ! function_exists('') ) (read: "if this function does not exist") you should be able to declare it before the parent does. This will prevent the code inside the parent function from ever being run.

Note: I replaced your hyphens (-) because I haven't seen the hyphen syntax and went along assuming that is a typo. In my experience the proper syntax to use is underscores (_).

You would add something like this to your child theme's functions.php file:

function remove_elements_from_vc() {
    return;
}

However, your remove_action() should work as well. It's possible you're function is hooking in before the parent theme's function is hooked. You can't remove an action until after it is added. From the codex:

  1. You may need to prioritize the removal of the action to a hook that occurs after the action is added.
  2. You cannot successfully remove the action before it has been added.
  3. You also cannot remove an action after it has been run.
  4. To remove an action the priority must match the priority with with the function was originally added.

If that is the case you could try to add it with a lesser priority:

add_action( 'vc_after_init', 'undo_remove_elements_from_vc', 20 );

function undo_remove_elements_from_vc(){
remove_action( 'vc_after_init', 'remove_elements_from_vc'); 
}
DaveLak
  • 796
  • 9
  • 19
  • Thank you @DaveLak, I created the function in my child theme and gave it a priority 5, and it worked. – Joey Jun 05 '17 at 12:57
  • Great, you shouldn't need to add that empty function to an action at all though. All you are doing is declaring it before the parent. When `functions.php` is run that function is loaded into memory and "exists" to php. Because php is aware the function now "exists" the `if()` check in the parent fails and that function may as well just be a comment block. **TL;DR**: just add the function and you should be set. – DaveLak Jun 05 '17 at 13:01
  • @Joey- +1 - and Two comments: 1. The `-` would be illegal / not run, so you're right - that had to be a typo, and 2. When attempting to be sure that a hook hooks as early as possible, it can be run (and works) with a large negative number (such as `-9999`). That could be useful as an alternative method to "hijack" the parent theme's hook (in case the remove action was not working for some reason). – random_user_name Jun 05 '17 at 13:01