好的,我设法做到了,我花了 2 天时间才弄明白。这是我设法做到的:
- 制作插件文件夹。
- 在该插件文件夹中制作 1x php 文件。所以 index.php
好的,首先我们需要注册插件,我是这样做的,在你的 index.php 中粘贴
这段代码。
function activate_profile_plugin() {
add_option( 'Activated_Plugin', 'Plugin-Slug' );
/* activation code here */
}
register_activation_hook( __FILE__, 'activate_profile_plugin' );
那么我们需要一个函数,当你只注册一个插件时,注册个人资料页面。
function create_profile_page( $title, $slug, $post_type, $shortcode, $template = null ) {
//Check if the page with this name exists.
if(!get_page_by_title($title)) {
// if not :
$page_id = -1;
$page_id = wp_insert_post(
array(
'comment_status' => 'open',
'ping_status' => 'open',
'post_content' => $shortcode,
'post_author' => 1, // Administrator is creating the page
'post_title' => $title,
'post_name' => strtolower( $slug ),
'post_status' => 'publish',
'post_type' => strtolower( $post_type )
)
);
// If a template is specified in the function arguments, let's apply it
if( null != $template ) {
update_post_meta( get_the_ID(), '_wp_page_template', $template );
} // end if
return $page_id;
}
}
好的,所以我们创建了以编程方式注册页面的函数。它有 5 个参数。
- 是标题
- 蛞蝓
- 帖子类型
- 简码。
- 模板
对于简码模板,您需要使用完整的页面输出制作简码
并将其作为参数添加到此函数中,因此对于注册页面,它将是带有注册表单等的简码。
例如:
function registration_shortcode(){
echo 'Wellcome to Registration page';
}
add_shortcode('registration_output', 'registration_shortcode');
接下来我们只需要在插件加载时调用它一次。
所以我们这样做:
function load_plugin() {
if ( is_admin() && get_option( 'Activated_Plugin' ) == 'Plugin-Slug' ) {
delete_option( 'Activated_Plugin' );
/* do stuff once right after activation */
// example: add_action( 'init', 'my_init_function' );
create_profile_page('Registration', 'registration', 'page', '[registration_output]');
create_profile_page('Profile', 'profile', 'page', '[profile_shortcode]');
create_profile_page('Profil Edit', 'profile-edit', 'page', '[edit_shortcode]');
}
}
add_action( 'admin_init', 'load_plugin' );
好的,所以这只会在插件加载时执行一次,它会创建 3 个页面,分别是 Profile、Registration 和 Profile Edit。
就是这样,您的前端用户个人资料空白页,您可以用简码编写页面输出,创建更多页面,放置您喜欢的任何表单或元素并创建体面的个人资料(没有任何东西你不需要像插件一样在里面。)
希望这会有所帮助,解决这个问题对我来说很痛苦。干杯!