当前位置: 首页 > 知识库问答 >
问题:

从插件添加自定义页面模板

卓宏达
2023-03-14

我正在为wordpress构建我的第一个插件,我需要它为登录屏幕动态添加一个自定义页面。

我能找到的唯一一件接近我需要的东西是:WP-使用插件目录中的文件作为自定义页面模板?

这是我目前在我的插件中运行的代码...

// Add callback to admin menu
add_action( 'template_redirect', 'uploadr_redirect' );

// Callback to add menu items
function uploadr_redirect() {

global $wp;
$plugindir = dirname( __FILE__ );

// A Specific Custom Post Type
if ( $wp->query_vars["post_type"] == 'uploadr' ) {

    $templatefilename = 'custom-uploadr.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;

    }

}

使用这将需要我的客户端创建新页面。。。我需要的是插件使用插件文件夹中的模板文件自动创建一个自定义页面(具有自定义路径,意思是.com/custompathhere),该模板文件将包含插件执行的所有操作。

注意:此插件被设计为在一个页面上运行,因此减少了加载时间等。

提前谢谢!

共有3个答案

茅高卓
2023-03-14

我为那些想从插件中向帖子添加模板的人提供了一个通用的解决方案。使用单_模板过滤器。

<?php
  add_filter( 'single_template', 'add_custom_single_template', 99 );

  function add_custom_single_template( $template ) {
    return plugin_dir_path( __FILE__ ) . 'path-to-page-template-inside-plugin.php';
  }
?>

此外,如果要在特定的帖子类型中使用模板,则:

<?php
  add_filter( 'single_template', 'add_custom_single_template', 99 );

  function add_custom_single_template( $template ) {
    if ( get_post_type() == 'post-type-name'; ) {
      return plugin_dir_path( __FILE__ ) . 'path-to-page-template-inside-plugin.php';
    }

    return $template;
  }
?>
郑胡媚
2023-03-14

在修改了相当多的代码后,我实际上能够和我的一个开发者朋友交谈。

这是。。。

<?php

    register_activation_hook( __FILE__, 'create_uploadr_page' );

    function create_uploadr_page() {

        $post_id = -1;

        // Setup custom vars
        $author_id = 1;
        $slug = 'event-photo-uploader';
        $title = 'Event Photo Uploader';

        // Check if page exists, if not create it
        if ( null == get_page_by_title( $title )) {

            $uploader_page = array(
                    'comment_status'        => 'closed',
                    'ping_status'           => 'closed',
                    'post_author'           => $author_id,
                    'post_name'                     => $slug,
                    'post_title'            => $title,
                    'post_status'           => 'publish',
                    'post_type'                     => 'page'
            );

            $post_id = wp_insert_post( $uploader_page );


            if ( !$post_id ) {

                    wp_die( 'Error creating template page' );

            } else {

                    update_post_meta( $post_id, '_wp_page_template', 'custom-uploadr.php' );

            }
        } // end check if

    }

    add_action( 'template_include', 'uploadr_redirect' );
    function uploadr_redirect( $template ) {

        $plugindir = dirname( __FILE__ );

        if ( is_page_template( 'custom-uploadr.php' )) {

            $template = $plugindir . '/templates/custom-uploadr.php';
        }

        return $template;

    }

?>
裴存
2023-03-14

下面是我从Wordpress插件(受Tom McFarlin启发)添加页面模板的代码解决方案。

这是为插件设计的(在插件的根目录中搜索模板文件)。这些文件的格式与直接包含在主题中的格式完全相同。这可以改变,如果需要-查看我的完整教程http://www.wpexplorer.com/wordpress-page-templates-plugin/ 有关此解决方案的更多详细信息。

要定制,只需在_构造方法中编辑以下代码块;

   $this->templates = array(
       'goodtobebad-template.php'     => 'It\'s Good to Be Bad',
   );

完整代码;

class PageTemplater {

    /**
     * A Unique Identifier
     */
     protected $plugin_slug;

    /**
     * A reference to an instance of this class.
     */
    private static $instance;

    /**
     * The array of templates that this plugin tracks.
     */
    protected $templates;


    /**
     * Returns an instance of this class. 
     */
    public static function get_instance() {

            if( null == self::$instance ) {
                    self::$instance = new PageTemplater();
            } 

            return self::$instance;

    } 

    /**
     * Initializes the plugin by setting filters and administration functions.
     */
    private function __construct() {

            $this->templates = array();


            // Add a filter to the attributes metabox to inject template into the cache.
            add_filter(
                'page_attributes_dropdown_pages_args',
                 array( $this, 'register_project_templates' ) 
            );


            // Add a filter to the save post to inject out template into the page cache
            add_filter(
                'wp_insert_post_data', 
                array( $this, 'register_project_templates' ) 
            );


            // Add a filter to the template include to determine if the page has our 
            // template assigned and return it's path
            add_filter(
                'template_include', 
                array( $this, 'view_project_template') 
            );


            // Add your templates to this array.
            $this->templates = array(
                    'goodtobebad-template.php'     => 'It\'s Good to Be Bad',
            );

    } 


    /**
     * Adds our template to the pages cache in order to trick WordPress
     * into thinking the template file exists where it doens't really exist.
     *
     */

    public function register_project_templates( $atts ) {

            // Create the key used for the themes cache
            $cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );

            // Retrieve the cache list. 
            // If it doesn't exist, or it's empty prepare an array
            $templates = wp_get_theme()->get_page_templates();
            if ( empty( $templates ) ) {
                    $templates = array();
            } 

            // New cache, therefore remove the old one
            wp_cache_delete( $cache_key , 'themes');

            // Now add our template to the list of templates by merging our templates
            // with the existing templates array from the cache.
            $templates = array_merge( $templates, $this->templates );

            // Add the modified cache to allow WordPress to pick it up for listing
            // available templates
            wp_cache_add( $cache_key, $templates, 'themes', 1800 );

            return $atts;

    } 

    /**
     * Checks if the template is assigned to the page
     */
    public function view_project_template( $template ) {

            global $post;

            if (!isset($this->templates[get_post_meta( 
                $post->ID, '_wp_page_template', true 
            )] ) ) {

                    return $template;

            } 

            $file = plugin_dir_path(__FILE__). get_post_meta( 
                $post->ID, '_wp_page_template', true 
            );

            // Just to be safe, we check if the file exist first
            if( file_exists( $file ) ) {
                    return $file;
            } 
            else { echo $file; }

            return $template;

    } 


} 

add_action( 'plugins_loaded', array( 'PageTemplater', 'get_instance' ) );

查看我的教程了解更多信息。

http://www.wpexplorer.com/wordpress-page-templates-plugin/

我希望这能帮助你做你想做的事:)

 类似资料:
  • 我正在编写一个自定义Gradle插件,它将向Android项目添加一个或多个任务。其中一项任务需要将“ad hoc”构建风格添加到Android项目已经定义的现有构建风格列表中。 我尝试通过更改名称来更改现有构建风格之一,如下代码所示: 这里的问题是,所有构建风格都在目标中。AndroidproductFlavors此时为只读,并引发以下错误:

  • 我正在为wordpress开发一个插件。这个插件必须有一个插件设置管理部分,但也必须有一个自定义的前端与表单页面。 我是wordpress插件开发领域的新手,但我还没有找到这项任务的具体信息。 有没有办法从插件向前端添加页面,或者需要手动编辑当前模板并添加页面?

  • 问题内容: 我的公司最近写了gradle插件来进行原始配置(存储库,项目间的通用依赖关系等)。总体而言,这大大简化了我们的构建过程,并发现了项目之间的一些不一致之处。我们最近尝试向插件添加任务,但该任务无法正常工作。 这是坏掉的插件: 除了之外,此插件都很好用。当我将其添加到混合中(并编译/部署到我们的本地仓库)时,当我尝试构建使用插件的项目时出现此错误: 问题答案: 在脚本中定义任务时,将调用方

  • YDoc 的页面可支持 .md、.jsx、.html 三种类型。我们推荐大部分的文档内容 使用 markdown 编写,少数个性化页面使用 html 或 jsx 实现。 Markdown 规则 YDoc 会根据 markdown 内容获取网站标题和描述信息,如下所示,YDoc 会将当前页面标题设置为 “示例”, 页面描述信息设置为 “这是一个示例。”。 # 示例 这是一个示例。 ## 章节1

  • 自定义页面 您可以将网页添加到您的网站,而不是作为标准文档或博客 markdown 文件的一部分。 你可以通过在 website/pages 目录中添加 .js 文件来实现。 这些文件是 React 组件,并调用 render() 来创建它们,由CSS类等支持。 自定义主页 开始自定义主页的最简单方法是使用运行 Docusaurus 初始化脚本 时 创建 的示例网站。 你可以 启动 你的本地服务器

  • 问题内容: 我已经构建了自己的自定义react-bootstrap Popover组件: 该组件的呈现方式如下: 现在,我想向组件中添加自定义道具,例如:我的文字,并使用新道具在弹出框中设置一些内容,例如- 但随后我在浏览器中收到此警告: 警告:标签上的未知道具。从元素中删除这些道具。 现在,我想我可以删除零件并逐个插入所有原始道具,而无需自定义道具,但是这样我就失去了“淡入淡出”效果,这也是处理