12

我想做一个登陆页面。如果插件检测到一些 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 Codex,但我不记得是否有任何功能可以做到这一点。当然,我用谷歌搜索了它,没有结果。

提前感谢您的任何想法。

4

2 回答 2

26

你需要钩子template_include。Codex 中似乎没有记录,但您可以在 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');

结果

访问网站的任何 url?mod=yes都会呈现插件模板文件,例如:http://example.com/hello-world/?mod=yes.

于 2012-12-22T02:44:22.940 回答
-1

您需要在插件目录中创建一个文件夹“/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; 
 }
?>

使用插件参考模板覆盖

于 2015-04-28T07:36:44.143 回答