在插件激活 (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' 索引。我想知道是否有办法,我可以简单地选择:
- 'post_content' => link 到我插件目录下的文件
- 'post_content' => 调用一个函数,它将 return html 内容作为字符串 :( [最坏情况]
- 或者,一些更简单的方法来实现objective。
请帮帮我
您使用和指导并阅读此 link 如何创建插件和页面 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()] 和 return 值的简码。
函数 foo1() 调用另一个函数 [say foo2()],它 return 是一个干净的 html 形式以响应调用(当调用短代码时)。
第 2 节:在本节中,我定义了函数 foo2(),其中定义了 html 表单 [登录表单] 并 returned 到 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 );
}
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 );
我正在创建我的第一个 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' 索引。我想知道是否有办法,我可以简单地选择:
- 'post_content' => link 到我插件目录下的文件
- 'post_content' => 调用一个函数,它将 return html 内容作为字符串 :( [最坏情况]
- 或者,一些更简单的方法来实现objective。
请帮帮我
您使用和指导并阅读此 link 如何创建插件和页面 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()] 和 return 值的简码。 函数 foo1() 调用另一个函数 [say foo2()],它 return 是一个干净的 html 形式以响应调用(当调用短代码时)。
第 2 节:在本节中,我定义了函数 foo2(),其中定义了 html 表单 [登录表单] 并 returned 到 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 );
}
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 );