Override a WordPress plugin function or constant via a child theme's functions.php

376 Views Asked by At

I am wondering if it's possible to override either a function or a constant of a plugin via a child theme's functions.php.

The issue I'm having is I have a plugin which refers to some custom template files for said plugin in a folder in the current theme i.e. the parent theme, but as I am working on a child theme I want the plugin to reference the templates from a folder within the child theme. Obviously I don't want to modify any of the plugin files or have to put the custom template folder in the parent theme as they will get overwritten on updates.

In the plugin there is the below function:

/**
 * Load view
 *
 * A simple hook of wp_plugin_hook to accept custom view in
 * the current theme folder
 *
 * @return    string
 */
public function load_view($file, array $view_datas = array(), $return = true) {
    
    $custom_view_path = WP_CURRENT_THEME_PATH . 'custom-template-folder/' . $file . '.php';
    
    $view_datas = array_merge($view_datas, $GLOBALS['wp_views_datas']);
    
    if(file_exists($custom_view_path)) {
        
        $view = wp_plugin_hook($file, $view_datas, $return, WP_CURRENT_THEME_PATH . 'custom-template-folder');
        
    }
    else {
        
        $view = wp_plugin_hook($file, $view_datas, $return, WP_PLUGIN_PATH . 'views/template/');
        
    }
    
    return $view;
    
}

Then the constant in the plugin is as follows:

// Path directory current theme
define('WP_CURRENT_THEME_PATH', get_template_directory() . '/');

I basically either need to change the WP_CURRENT_THEME_PATH constant so it uses get_stylesheet_directory() as opposed to get_template_directory().

OR

overwrite the whole load_view function and replace within it the WP_CURRENT_THEME_PATH with a hard coded URL i.e. so it points to the folder in the child theme directory.

Is any of the above possible via overrides in the child theme's functions.php?

Thanks in advance for anyone who can help!

Chris

0

There are 0 best solutions below