经过几个小时的WordPress资源搜索,我找不到任何合理的方法从插件创建前端页面。我的目标很简单,我想要http://mysite.com/friends 显示由我的插件生成的页面。这是一个多站点安装,我不希望博客所有者能够删除此页面。我的最终实现不涉及用户博客,但可以。
wp包括/主题。php:我添加了这个函数
function get_extension_page_template() {
$page = get_queried_object();
$templates = array($page->ID.\'.php\');
return get_query_template($page->ID,$templates);
}
wp包括/模板加载器。php:我刚刚在if(is\\u 404()行(靠近第8行)中添加了这个
elseif ( is_extension_page() && $template = get_extension_page_template()) :
wp包括/查询。php:我添加了两个函数
function set_extension_page($page,$object) {
global $wp_query;
return $wp_query->set_extension_page($page,$object);
}
function is_extension_page() {
global $wp_query;
return $wp_query->is_extension_page();
}
wp包括/查询。php:我向WP\\u查询类添加了两个属性
var $extension_page = null;
var $extension_page_object = null;
wp包括/查询。php:我向WP\\u查询类添加了两个方法
function set_extension_page($page,$object) {
$this->extension_page_object = $object;
$this->extension_page = $page;
}
function is_extension_page() {
return isset($this->extension_page);
}
wp包括/查询。php:我更改了WP\\U查询类中的get\\u queryed\\u object方法
function get_queried_object() {
if ( isset($this->queried_object) )
return $this->queried_object;
if (isset($this->extension_page_object)) {
return $this->extension_page_object;
}
$this->queried_object = NULL;
$this->queried_object_id = 0;
if ( $this->is_category || $this->is_tag || $this->is_tax ) {
$tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( \'operator\' => \'NOT IN\' ), \'NOT\' );
$query = reset( $tax_query_in_and );
if ( \'term_id\' == $query[\'field\'] )
$term = get_term( reset( $query[\'terms\'] ), $query[\'taxonomy\'] );
else
$term = get_term_by( $query[\'field\'], reset( $query[\'terms\'] ), $query[\'taxonomy\'] );
if ( $term && ! is_wp_error($term) ) {
$this->queried_object = $term;
$this->queried_object_id = (int) $term->term_id;
if ( $this->is_category )
_make_cat_compat( $this->queried_object );
}
} elseif ( $this->is_post_type_archive ) {
$this->queried_object = get_post_type_object( $this->get(\'post_type\') );
} elseif ( $this->is_posts_page ) {
$page_for_posts = get_option(\'page_for_posts\');
$this->queried_object = & get_page( $page_for_posts );
$this->queried_object_id = (int) $this->queried_object->ID;
} elseif ( $this->is_singular && !is_null($this->post) ) {
$this->queried_object = $this->post;
$this->queried_object_id = (int) $this->post->ID;
} elseif ( $this->is_author ) {
$this->queried_object_id = (int) $this->get(\'author\');
$this->queried_object = get_userdata( $this->queried_object_id );
}
return $this->queried_object;
}
wp包括/类wp。php:我将handle\\u 404方法更改为
function handle_404() {
global $wp_query;
if ( !is_admin() && ( 0 == count( $wp_query->posts ) ) && !is_404() && !is_robots() && !is_search() && !is_home() ) {
// Don\'t 404 for these queries if they matched an object.
if ( ( is_extension_page() || is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object() && !is_paged() ) {
if ( !is_404() )
status_header( 200 );
return;
}
$wp_query->set_404();
status_header( 404 );
nocache_headers();
} elseif ( !is_404() ) {
status_header( 200 );
}
}
最终的结果是,我可以在补丁站点的插件中创建一个对象,并测试某些条件,例如$blogpage=$this->ID,然后调用set\\u extension\\u page($this->ID,$this);注册要显示的页面。主题还必须为名为(set\\u extension\\u page的第一个参数)的插件设置模板。php,但总比什么都没有好。
我承认这只是一个丑陋的黑客,但它是有效的。很高兴将来能在wordpress中添加这样的功能。