Overwrite wordpress theme page by plugin

跟風遠走 提交于 2020-01-06 06:07:13

问题


I want to overwrite a theme page using my custom wordpress plugin , i tried to add filter but without result , i don't want to change directly on the theme because it's bad idea for every developer. this is my plugin code i added a filter to check pages

public function __construct(){
    add_filter('theme_file_path','customize_theme_pages', 99, 2 );
}

and i tried to replace theme page with my custom page like that

public function customize_theme_pages($path, $file = ''){
        if( 'woocommerce/checkout--/form-checkout.php' === $file ) {
            return plugin_dir_url( __FILE__ ).'inc/form-checkout.php';
        }
        return $path;
}

it seems logical solution for me , but there is no changes in my site


回答1:


You can use the woocommerce_locate_template filter to override a woocommerce template file from your plugin.

A quick example, assuming you want to override checkout/form-checkout.php with your plugin's inc/checkout/form-checkout.php file:

function customize_theme_pages($template, $template_name, $template_path) {
    if ($template_name == 'checkout/form-checkout.php') {
        $template = plugin_dir_path( __FILE__ ) . 'inc/checkout/form-checkout.php';
    }
    return $template;
}

add_filter('woocommerce_locate_template', 'customize_theme_pages', 99, 3);

**I have tested above code, and it's working on WooCommerce Version 3.5.0 running on WordPress 4.9.8.

If you want you replace a standard page template, you can use page_template filter.



来源:https://stackoverflow.com/questions/53035844/overwrite-wordpress-theme-page-by-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!