由 wordpress 插件创建的自定义页面
Custom page created by a wordpress plugin
我想知道如何创建一个 Wordpress 插件,将页面添加到安装它的 Wordpress 网站。
我想出了以下可行的方法并添加了页面。
然而,这与我想要实现的目标相去甚远:
如何改变整个页面?不仅仅是内容?现在,它具有所有页眉和页脚以及导航栏。
如何在页面中包含 PHP 代码,而不仅仅是静态内容?
是否可以将 url (https://some-url.com/my-plugin/) 下的所有内容都路由到同一页面?
例如:
- https://some-url.com/my-plugin/ -> 运行 我的页面
- https://some-url.com/my-plugin/foo/ -> 运行 我的页面
- https://some-url.com/my-plugin/foo2/abc/ -> 运行 我的页面
- 等等
<?php
/**
* Plugin Name: MyPlugin
* Plugin URI: myplugin.com
* Description: MyPlugin
* Version: 1.0
* Author: Mike
* Author URI: myplugin.com
*/
define( 'MYPLUGIN__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
function create_page() {
$post_data = array(
'post_title' => 'Test of my plugin',
# How to change the entire page? Not just the content?
# Also, how to have PHP code in the page, not just static content?
'post_content' => 'Place all your body content for the post in this line.',
'post_status' => 'publish', // Automatically publish the post.
'post_type' => 'page', // defaults to "post".
'post_name' => 'my-plugin', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugin-page?/
);
// Lets insert the page now.
wp_insert_post( $post_data );
}
function plugin_activation() {
create__page();
}
function plugin_deactivation() {
}
register_activation_hook( __FILE__, 'plugin_activation' );
register_deactivation_hook( __FILE__, 'plugin_deactivation' );
你想要wp_insert_post()
用法:
$postarr = [
'post_title' => 'My Page Title', // <title> tag
'post_content' => '<p>page content</p>', // html for page content
'post_type' => 'page', // blog post is default
'post_name' => 'my-plugion-page', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugion-page?/
];
$post_id = wp_insert_post($postarr);
例如,在您的 $postarr
中,如果您希望它是一个页面(而不是博客 post),请使用 'post_type' => 'page'
。对于页面标题'post_title' => 'My New Page'
等等。
参见:https://developer.wordpress.org/reference/functions/wp_insert_post/
已更新以补充用户问题
如果您不希望它遵循主题模板,那么您需要连接到 'page_template' 过滤器并使用您在插件中创建的模板文件。参见:https://wordpress.stackexchange.com/questions/3396/create-custom-page-templates-with-plugins
视情况而定,您可以在您的自定义模板中添加代码,在 Admin UI 中使用 shortcodes or custom Gutenberg blocks. You can use Advanced Custom Fields 并在您的代码中使用 get_field()
,等等。您在 WordPress 中有很多选择。有了自定义模板后,您只需在其中添加常规 PHP。
是的,但您需要使用正则表达式并使用 add_rewrite_rule()
创建重写规则。以下是有助于在 WordPress 中创建重写规则的链接
我想知道如何创建一个 Wordpress 插件,将页面添加到安装它的 Wordpress 网站。 我想出了以下可行的方法并添加了页面。
然而,这与我想要实现的目标相去甚远:
如何改变整个页面?不仅仅是内容?现在,它具有所有页眉和页脚以及导航栏。
如何在页面中包含 PHP 代码,而不仅仅是静态内容?
是否可以将 url (https://some-url.com/my-plugin/) 下的所有内容都路由到同一页面?
例如:
- https://some-url.com/my-plugin/ -> 运行 我的页面
- https://some-url.com/my-plugin/foo/ -> 运行 我的页面
- https://some-url.com/my-plugin/foo2/abc/ -> 运行 我的页面
- 等等
<?php
/**
* Plugin Name: MyPlugin
* Plugin URI: myplugin.com
* Description: MyPlugin
* Version: 1.0
* Author: Mike
* Author URI: myplugin.com
*/
define( 'MYPLUGIN__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
function create_page() {
$post_data = array(
'post_title' => 'Test of my plugin',
# How to change the entire page? Not just the content?
# Also, how to have PHP code in the page, not just static content?
'post_content' => 'Place all your body content for the post in this line.',
'post_status' => 'publish', // Automatically publish the post.
'post_type' => 'page', // defaults to "post".
'post_name' => 'my-plugin', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugin-page?/
);
// Lets insert the page now.
wp_insert_post( $post_data );
}
function plugin_activation() {
create__page();
}
function plugin_deactivation() {
}
register_activation_hook( __FILE__, 'plugin_activation' );
register_deactivation_hook( __FILE__, 'plugin_deactivation' );
你想要wp_insert_post()
用法:
$postarr = [
'post_title' => 'My Page Title', // <title> tag
'post_content' => '<p>page content</p>', // html for page content
'post_type' => 'page', // blog post is default
'post_name' => 'my-plugion-page', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugion-page?/
];
$post_id = wp_insert_post($postarr);
例如,在您的 $postarr
中,如果您希望它是一个页面(而不是博客 post),请使用 'post_type' => 'page'
。对于页面标题'post_title' => 'My New Page'
等等。
参见:https://developer.wordpress.org/reference/functions/wp_insert_post/
已更新以补充用户问题
如果您不希望它遵循主题模板,那么您需要连接到 'page_template' 过滤器并使用您在插件中创建的模板文件。参见:https://wordpress.stackexchange.com/questions/3396/create-custom-page-templates-with-plugins
视情况而定,您可以在您的自定义模板中添加代码,在 Admin UI 中使用 shortcodes or custom Gutenberg blocks. You can use Advanced Custom Fields 并在您的代码中使用
get_field()
,等等。您在 WordPress 中有很多选择。有了自定义模板后,您只需在其中添加常规 PHP。是的,但您需要使用正则表达式并使用
add_rewrite_rule()
创建重写规则。以下是有助于在 WordPress 中创建重写规则的链接