簡體   English   中英

通過插件將PHP頁面添加到Wordpress

[英]Adding a PHP page to Wordpress via Plugin

幾乎每一個將php頁面添加到Wordpress的指南都涉及在主題級別添加它。 例如, 如何在WordPress中添加PHP頁面? 我想通過插件向多個站點添加一個面向公眾的新頁面。 我不想將其添加到數十個主題中。 如果我可以在插件級別添加它,它將適用於所有站點。

我有這個工作,但我需要某種方式將其注入主題。 為了獲得側邊欄和東西,而不必為每個主題添加自定義css。

我已經開始添加重寫規則

RewriteRule ^mypage /wp-content/plugins/myplugin/mypage.php [QSA,L]

然后page.php包含

require_once('../../../wp-blog-header.php');
get_header();

//custom php content
//get_sidebar(); // how to get this working.
get_footer();

這也有效,但是側邊欄存在問題。 有些主題沒有主題,而其他主題則有。 並非所有的側邊欄都是30%等。我不知道如何在此處構建div結構以使其正常工作。 有些主題的頁面寬度為100%,但是在固定寬度的其他主題上看時,這看起來很難看。 我已經能夠提出一些妥協,但是id卻能夠做到這一點。

在夏季,我的主要問題是。 是否可以調用將html注入主題頁面的方法。 例如generate_page($ html);。 然后,該方法將轉到主題的page.php,並將$ html注入主題的內容區域。

為了將內容動態地注入未知主題,我進行了以下操作

global $post;

$post->post_content = "TEST PAGE content";
$post->post_title = "Page Title";
$post->post_name = "test-page";

include get_template_directory()."/page.php";

這適用於某些主題,不適用於其他主題。 一些主題可以很好地顯示此帖子,但是其他主題(默認的wordpres二十一十五主題)則顯示此帖子,然后顯示該數據庫之后的其他所有帖子。 林不知道在哪里或為什么拉所有這些職位,但如果我能阻止它,它看起來像是一個有效的解決方案。

然后,您可以嘗試在特定情況下加載特定的模板頁面。

function load_my_template( $template )
{
    if( is_page() )
        $template = plugin_dir_path(__FILE__) . "dir_to/my_template.php";

    return $template;
}

或更改加載頁面上使用的內容

function load_my_content( $content )
{
    global $post;

    $id = $post->ID;

    if( is_page() )
    {
        ob_start();

        include plugin_dir_path(__FILE__) . "dir_to/my_template.php";

        $content = ob_get_clean();
    }

    return $content;
}

在你的__construct()中

add_filter('template_include', array($this,'load_my_template') );
add_filter("the_content", array($this,"load_my_content" ) );
add_filter("get_the_content", array($this,"load_my_content" ) );

希望對你有幫助。 告訴我這是否與您的問題不符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM