Is there any way to override a WordPress template with a plugin?

I’d like to make a landing page. If plugin detects some GET or POST requests it should override wordpress theme and show its own.

It would work somehow like that:

Read More
if (isset($_GET['action']) && $_GET['action'] == 'myPluginAction'){
    /* do something to maintain action */
    /* forbid template to display and show plugin's landing page*/
}

I’m familiar with WP Codex, but I don’t remember if there is any function to do that. Of course, I googled it with no results.

Thanks for any ideas in advance.

Related posts

Leave a Reply

2 comments

  1. You need the hook template_include. It doesn’t seem documented in the Codex, but you can find more examples here in SO or in WordPress StackExchange

    Plugin file

    <?php
    /**
     * Plugin Name: Landing Page Custom Template
     */
    add_filter( 'template_include', 'so_13997743_custom_template' );
    
    function so_13997743_custom_template( $template )
    {
        if( isset( $_GET['mod']) && 'yes' == $_GET['mod'] )
            $template = plugin_dir_path( __FILE__ ) . 'my-custom-page.php';
    
        return $template;
    }
    

    Custom Template in Plugin folder

    <?php
    /**
     * Custom Plugin Template
     * File: my-custom-page.php
     *
     */
    
    echo get_bloginfo('name');
    

    Result

    Visiting any url of the site with ?mod=yes will render the plugin template file, e.g.: http://example.com/hello-world/?mod=yes.

  2. you need to create a folder ‘/woocommerce/’ inside your plugin directory, inside woocommerce you need to add a folder say, for email ’emails’ and put the required template inside ‘/emails/’ to override. just copy paste this code in the main.php of your plugin.

    <?php
    /**
     * Plugin Name: Custom Plugin
     */
    
    function myplugin_plugin_path() {   
      // gets the absolute path to this plugin directory 
      return untrailingslashit( plugin_dir_path( __FILE__ ) ); 
    }
    
    add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 ); 
    function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
    
      global $woocommerce;  
      $_template = $template; 
      if ( ! $template_path ) $template_path = $woocommerce->template_url; 
      $plugin_path  = myplugin_plugin_path() . '/woocommerce/'; 
      // Look within passed path within the theme - this is priority 
      $template = locate_template( 
        array( 
          $template_path . $template_name, $template_name 
        ) 
      );
    
      // Modification: Get the template from this plugin, if it exists 
      if ( ! $template && file_exists( $plugin_path . $template_name ) ) 
        $template = $plugin_path . $template_name;  
    
      // Use default template 
      if ( ! $template ) 
        $template = $_template; 
    
      // Return what we found 
      return $template; 
     }
    ?>
    

    for reference template override using plugin