• 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

    • This topic was modified 2 years, 8 months ago by cpthomas1990.
Viewing 5 replies - 1 through 5 (of 5 total)
  • I would rather recommend that you contact the developer of the plugin you are interested in. You will not be able to overload the load_view() function because it is obviously in a class. And you can’t adjust a constant if it is not set to defined() together with a check.

    Moderator bcworkz

    (@bcworkz)

    Try finding the source code for wp_plugin_hook(). Within it there could be a filter hook you could use to alter paths or what is returned for assignment to $view.

    Thread Starter cpthomas1990

    (@lechuck)

    @bcworkz @threadi Thank you for your responses. I have had a look and the only source code I can find for wp_plugin_hook() is as follows:

    **
     * Load a view, the cool way
     *
     * @param string $view the view name (e.g. 'admin/dashboard')
     * @return string
     */
    function wp_plugin_hook($view, $datas = array(), $return = false, $start_path) {
    	
    	// We want return, so we must stock the buffer
    	if($return)
    		ob_start();
    	
    	/*
    	 * We get our global variable and extract it to simulate a view variable system
    	 * For example, $wp_plugin_name_views_datas['var'] will become $var within this function and our view
    	 *
    	 */
    	$datas = (array)$datas;
    	
    	if(!empty($datas)) {
    		
    		extract($datas);
    		
    	}
    	
    	require(rtrim($start_path, '/') . '/' . ltrim($view, '/') . '.php');
    	
    	// Get content of the buffer
    	if($return) {
    		
    		$buffer = ob_get_contents();
    		
    		// Bye bye buffer
    		ob_get_clean();
    		
    		return $buffer;
    		
    	}
    	
    }

    I have tried the below two hooks in my child theme’s functions.php file.

    1) Where I try and override $view:

    function ct_test( $view ) { 
    	$view = wp_plugin_hook($file, $view_datas, $return, get_stylesheet_directory() . 'custom-template-folder');
    	return $view;
    }
    add_filter( 'wp_plugin_hook', 'ct_test' );

    2: Where I try and override the $custom_view_path:

    function ct_test2( $custom_view_path ) { 
    	$custom_view_path = get_stylesheet_directory() . '/custom-template-folder/' . $file . '.php';
    	return $custom_view_path;
    }
    add_filter( 'wp_plugin_hook', 'ct_test2' );

    Something is going wrong in the above as neither are working. Is there anything you can see which I may be doing wrong? Of course if it’s not possible I will admit defeat and have to get in touch with the plugin developers ??

    Thanks,
    Chris

    No hook is defined in the source code shown. Therefore, your code does not work at all. You cannot influence the behaviour in this way.

    Thread Starter cpthomas1990

    (@lechuck)

    @threadi Thank for you taking a look. I will get in touch with the plugin developer as you originally mentioned.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Override a plugin function or constant via a child theme’s functions.php’ is closed to new replies.