簡體   English   中英

在插件激活(Wordpress)上添加html / PHP頁面

[英]Add html/PHP pages on plugin activation (Wordpress)

好吧,我正在創建我的第一個wordpress插件,該插件應在激活時創建一個頁面(相對較大)。

目前,我可以使用創建文件:

$_p['post_title'] = $the_page_title;
        $_p['post_content'] = '<h1>PAGE CONTENT</h1>';
        $_p['post_status'] = 'publish';
        $_p['post_type'] = 'page';
        $_p['comment_status'] = 'closed';
        $_p['ping_status'] = 'closed';
        $_p['post_category'] = array(1); // the default 'Uncatrgorised'

        // Insert the post into the database
        $the_page_id = wp_insert_post( $_p );

問題是,我不能將文件的所有內容(大)作為字符串放入“ post_content”索引中。 我想知道是否有辦法,我可以在其中任意一個:

  1. 'post_content'=>鏈接到我的插件目錄中的文件
  2. 'post_content'=>調用一個函數,該函數將以字符串形式返回html內容:( [最壞的情況]
  3. 或者,一些更簡單的方法可以實現目標。

請幫我。

使用內容創建插件頁面激活

$page_content= 'Content of page';
$demo_page = array(
      'comment_status' => 'closed',
      'ping_status' =>  'closed' ,
      'post_author' => 1,
      'post_content' => $page_content,
      'post_date' => date('Y-m-d H:i:s'),
      'post_name' => 'page_name',//display on address bar
      'post_status' => 'publish' ,
      'post_title' => 'Page Display Name',
      'post_type' => 'page',
);  
//insert page and save the id
$demo_page_value = wp_insert_post( $demo_page, false );
//save the id in the database
update_option( 'testpage', $$demo_page_value );

您使用並引導並閱讀此鏈接如何創建插件和頁面https://codex.wordpress.org/Creating_Options_Pages

好吧,我為解決該問題所做的是:

//**SECTION : 1** 
   function user_login_foo() {

    return get_login_form(); // get_login_form() function will return the html template i want to display.
}
add_shortcode('user_login', 'user_login_foo'); // created a shortcode


**// SECTION: 2**
function get_login_form()
    {
        ob_start(); ?>
        <h3><?php __('Login'); ?></h3> 
        <form action="" method="post">
            <fieldset>
                 // the login form comes here
            </fieldset>
        </form>
    <?php
    return ob_get_clean();
    }

    function validate_login_user() {
             // the login validation logic comes here
    }
    add_action('init', 'validate_login_user');

第1節:注冊了一個短代碼,該短代碼將調用一個函數[say,foo1()]並返回該值。 函數foo1()調用另一個函數[say foo2()],該函數返回干凈的html形式以響應該調用(調用短代碼時)。

第2節:在本節中,我定義了函數foo2(),在其中定義了html格式[login form]並返回到foo1()[顯示它的位置]。 然后,我創建了一個動作[add_action('init','validate_login_user'); ],它將在初始化時調用函數validate_login_user(),在該函數內部,我檢查了isset(METHOD [username])和isset(METHOD [password]),然后執行各自的邏輯。

像這樣,我在激活時要為每個要創建的頁面創建多個[簡碼],然后:

 **step 1:** register_activation_hook(__FILE__,'activation_plugin'); 
 **step 2:** activation_plugin(){
     '390' => [ // '390' is page id
           'post_title' => 'Page title say login',
           'post_content' => "[user_login]",
           'post_status' => 'publish',
           'post_type' => 'page',
           'comment_status' => 'closed',
           'ping_status' => 'closed',
           'post_category' => array(1)
        ],
        '391' => [
           'post_title' => 'page title 2',
           'post_content' => "[short_code2]",
           'post_status' => 'publish',
           'post_type' => 'page',
           'comment_status' => 'closed',
           'ping_status' => 'closed',
           'post_category' => array(1)
        ],
         // like this add multiple shortcodes 
   }
      // this foreach will create all the pages
      foreach ($_ as $key => $value) {
        $the_page_title = $value['post_title'];
        $the_page_name = $value['post_title'];

        // the menu entry...
        delete_option($value['post_title']);
        add_option($value['post_title'], $the_page_title, '', 'yes');
        // the slug...
        delete_option($value['post_title']);
        add_option($value['post_title'], $the_page_name, '', 'yes');
        // the id...
        delete_option($key);
        add_option($key, '0', '', 'yes');

        $the_page = get_page_by_title( $the_page_title );

        if ( ! $the_page ) {
            $the_page_id = wp_insert_post( $value );
        }
        else {
            $the_page_id = $the_page->ID;
            $the_page->post_status = 'publish';
            $the_page_id = wp_update_post( $the_page );
        }

        delete_option( $key );
        add_option( $key, $the_page_id );
    }

暫無
暫無

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

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