有什么办法来超越插件WordPress模板?(Is there any way to overrid

2019-07-17 11:45发布

我想提出一个目标网页。 如果插件检测到一些GET或POST请求它应该重写WordPress主题和展现自己。

这会在一定程度工作这样的:

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

我熟悉WP食品,但我不记得是否有任何功能做到这一点。 当然,我没有结果GOOGLE了它。

感谢提前任何想法。

Answer 1:

您需要挂钩template_include 。 它似乎并不记录在食品,但你可以在SO或在这里找到更多的例子WordPress的StackExchange

插件文件

<?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;
}

在插件文件夹自定义模板

<?php
/**
 * Custom Plugin Template
 * File: my-custom-page.php
 *
 */

echo get_bloginfo('name');

结果

访问该网站的任何网址?mod=yes将使插件模板文件,如: http://example.com/hello-world/?mod=yes



Answer 2:

你需要创建一个文件夹/ woocommerce /'你的插件目录里面,里面woocommerce您需要添加一个文件夹中说,对电子邮件的电子邮件时“,把里面的所需模板“/电子邮件/”来覆盖。 只是复制你的插件的main.php粘贴此代码。

<?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; 
 }
?>

参考模板覆盖使用插件



文章来源: Is there any way to override a WordPress template with a plugin?
标签: wordpress