简体   繁体   中英

Add html/PHP pages on plugin activation (Wordpress)

Well am creating my first wordpress plugin, which should create a page (relatively large) on activation.

Currently i can create a file using :

$_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 );

The problem is, I cant put all the content of the file(which is large) as string to 'post_content' index. I want to know if there is a way, where i can simply either:

  1. 'post_content' => link to the file in my plugin directory
  2. 'post_content' => call a function which will return html content as string :( [worst case]
  3. OR, Some more simpler way to achieve the objective.

Please help me.

Create Page On Plugin activation with content

$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

Well, what i did to solve the problem is:

//**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');

SECTION 1: registered a shortcode that will call a function [say,foo1()] and return the value. The function foo1() calls another a function [say foo2()] which returns a clean html form in response to the call (when the shortcode is called).

SECTION 2: In this section I defined the function foo2(), within which the html form [login form] is defined and returned to foo1() [where it is displayed]. Then i created an action [ add_action('init', 'validate_login_user'); ] which will call the function validate_login_user() on initialization, inside this function i checked for isset(METHOD[username]) and isset(METHOD[password]) and then do respective logic.

Like this i created multiple [shortcodes] for each of the pages I wanted to create at the time of activation, Then :

 **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 );
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM