問題描述

過去幾個小時,我一直在尋找一種從外掛中建立自定義頁面模板的方法,但還沒有執行。

我特別想做的是在編輯頁面時向可用頁面模板列表新增一個選項,而不是使用其他類似 if( is_page( 'page-slug' ) ) { /* etc */ }的方法

是否有全域性變數我可以修改這樣做?

編輯:

我正在使用這個程式碼,基於連結 @ m0r7if3r 給了我一個評論,問題是它會在檢視頁面時執行此功能,但在編輯頁面時不會 (使用頁面模板填充下拉式清單) :

class JW_SiteGrader {

    private static $instance;

    private function __construct() {


        add_action( 'template_redirect', array( &$this, 'sitegrader_template_redirect' ), 20 );

    }

    public static function getInstance() {

        // Return the class data in a Singleton fashion
        if (self::$instance == null)
            self::$instance = new JW_SiteGrader();
        return self::$instance;

    }

    public function sitegrader_template_redirect() {

        add_filter( 'page_template', array( &$this, 'sitegrader_page_template' ), 10, 1 );

    }

    public function locate_plugin_template( $template_names, $load = false, $require_once = true ) {

        if ( !is_array( $template_names ) )
            return '';

        $located = '';

        $this_plugin_dir = WP_PLUGIN_DIR . '/' . str_replace( basename( __FILE__ ), '', plugin_basename( __FILE__ ) );

        foreach ( $template_names as $template_name ) {

            if ( !$template_name )
                continue;

            if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {

                $located = STYLESHEETPATH . '/' . $template_name;
                break;

            } else if ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {

                $located = TEMPLATEPATH . '/' . $template_name;
                break;

            } else if ( file_exists( $this_plugin_dir .  $template_name ) ) {

                $located =  $this_plugin_dir . $template_name;
                break;

            }

        }

        if ( $load && '' != $located )
            load_template( $located, $require_once );

        return $located;
    }

    public function sitegrader_page_template( $template ) {

        $object = get_queried_object();

        if ( 'page' == $object->post_type ) {

            // New
            $templates[] = "page-sitegrader.php";
            // Like in core
            $templates[] = "page-{$object->post_type}.php";
            $templates[] = "page.php";

            return locate_template( $templates );

        }

        // return apply_filters('page_template', $template);
        return $template;
    }

}

編輯 2:

看來這個功能將在未來的更新中釋出,我讀了很多關於這個的 trac 門票,並且有一些討論,但沒有真正的答案 (希望 3.4) 。將列出 trac 門票網址在這裡。

編輯 3:

上面的程式碼工作,但是,我在這一點上唯一的問題是編輯/新增新頁面時沒有模板新增到下拉式清單中。我正在嘗試幾件事情,並會盡快更新我的問題。

最佳解決方案

過濾器?任何人?

沒有幫助的過濾器:page_template_dropdown($template); 用於構建下拉式清單,它不可過濾。

潛入模板陣列?

要構建下拉式清單的內容,核心元框使用 get_page_templates()。從裡面看,功能如下所示:

$themes = get_themes();
$theme = get_current_theme();
$templates = $themes[$theme]['Template Files'];

但是當檢視 get_themes(); 時,那麼接縫不可能攔截模板列表。更多的我們有問題,沒有機會從主題目錄外部獲取模板…

偽造主題!

理論和它的缺點…

您可以使用 register_theme_directory()新增一個附加的主題目錄,您可以在其中放置模板。所以最簡單的事情就是將你的外掛註冊為主題資料夾:

// Register during init hook:
register_theme_directory( plugin_dir_path( __FILE__ ).'templates' );

注意:這是我不確定它是否可以工作的地方。

外掛啟用期間:您應該將 style.css.php 檔案放在 templates 資料夾中。這將允許您新增變數到您的檔案。這個變數將是父主題。父主題應該是目前活躍的主題。然後將您的活動主題更新為您的外掛。

缺點#2:關於» 外觀 «UI … 也許新增一個註釋,這個”Theme” 不是用作實際的主題。我離開其餘的» 避免啟用這個主題 «填充你的想象力。無論如何:它應該工作。

缺點#2:這個技巧將成功避免兒童主題。您可以擁有一個父主題。而已。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。