WP - 使用插件目录中的文件作为自定义页面模板? [英] WP - Use file in plugin directory as custom Page Template?

查看:27
本文介绍了WP - 使用插件目录中的文件作为自定义页面模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将插件目录中的文件用作自定义页面模板?

Is it possible for a file in the plugin directory to be used as a custom Page Template?

另外,你如何让插件创建一个页面?

Also, how do you make a plugin create a page?

我正在为一个基于主题的客户开发一个插件,他希望这个插件能够制作销售页面,同时能够在主页上使用他的主题.这是我为他制作的产品,因此需要通过插件实现自动化.

I'm developing a plugin for a client based on a theme, he wants this plugin to make sales pages while being able to use his theme on the homepage. This is a product that I'm making for him to market so it needs to be automated all through the plugin.

这可能吗?

编辑

我的插件主文件中有激活/停用挂钩,但它不起作用.代码如下:

I have the activation/deactivation hooks in my plugins main file, and it's not working. Here's the code:

$filename = __FILE__;

register_activation_hook($filename, 'superActivation');
register_deactivation_hook($filename, 'superDeactivation');

global $myFile; global $fh; global $stringData; global $filename;

$myFile = "testFile.txt";
$stringData = "Testing\n";
$fh = fopen($myFile, 'w') or die("can't open file");

function superActivation() {
    global $myFile; global $fh; global $stringData; global $filename;
    fwrite($fh, $stringData);
    fclose($fh);
}

function superDeactivation() {
    $myFile = "testFile.txt";
    unlink($myFile);
}

推荐答案

您可以使用 template_redirect 钩子来做到这一点.这是我的代码,如果模板文件夹中没有模板,则将自定义帖子类型的模板手动替换为主题中的模板.将其放入您的插件文件中,然后在您的插件下放置一个名为 themefiles 的文件夹,其中包含您的默认主题文件.

You can do this with the template_redirect hook. Here's my code to manually replace the template for a custom post type with one in the theme if there isn't one in the template folder. Put this in your plugin file and then put a folder underneath your plugin called themefiles with your default theme files.

//Template fallback
add_action("template_redirect", 'my_theme_redirect');

function my_theme_redirect() {
    global $wp;
    $plugindir = dirname( __FILE__ );

    //A Specific Custom Post Type
    if ($wp->query_vars["post_type"] == 'product') {
        $templatefilename = 'single-product.php';
        if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
            $return_template = TEMPLATEPATH . '/' . $templatefilename;
        } else {
            $return_template = $plugindir . '/themefiles/' . $templatefilename;
        }
        do_theme_redirect($return_template);

    //A Custom Taxonomy Page
    } elseif ($wp->query_vars["taxonomy"] == 'product_categories') {
        $templatefilename = 'taxonomy-product_categories.php';
        if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
            $return_template = TEMPLATEPATH . '/' . $templatefilename;
        } else {
            $return_template = $plugindir . '/themefiles/' . $templatefilename;
        }
        do_theme_redirect($return_template);

    //A Simple Page
    } elseif ($wp->query_vars["pagename"] == 'somepagename') {
        $templatefilename = 'page-somepagename.php';
        if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
            $return_template = TEMPLATEPATH . '/' . $templatefilename;
        } else {
            $return_template = $plugindir . '/themefiles/' . $templatefilename;
        }
        do_theme_redirect($return_template);
    }
}

function do_theme_redirect($url) {
    global $post, $wp_query;
    if (have_posts()) {
        include($url);
        die();
    } else {
        $wp_query->is_404 = true;
    }
}

这篇关于WP - 使用插件目录中的文件作为自定义页面模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆