我正在研究某种基于 wordpress 的 wiki/百科全书系统。
有两种自定义帖子类型。 我将展示其中之一的配置:
$labels = array(
'name' => _x('Objects', 'post type general name'),
'singular_name' => _x('Object', 'post type singular name'),
'add_new' => _x('Add New', 'object'),
'add_new_item' => __('Add New Object'),
'edit_item' => __('Edit Object'),
'new_item' => __('New Object'),
'view_item' => __('View Object'),
'search_items' => __('Search Objects'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'has_archive' => 'objects', // this is the archive-slug (could be "objects/index"!!)
'rewrite' => array( 'slug' => 'objects' ), // this is the prepended slug (like "objects/my-object")
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','thumbnail')
);
register_post_type( 'dr_object' , $args );
一切都按预期工作:mysite.de/objects 给我一个概述(archive-dr_object.php 模板),mysite.de/objects/my-object 显示对象的详细信息。
为了为前端编辑功能提供一些不错的 url,我添加了重写端点。
add_rewrite_endpoint('do', EP_PERMALINK | EP_PAGES ); // for do/edit, do/remove, etc
这对 mysite.de/objects/my-object/do/edit 这样的 url 非常有用
我的问题来了:
档案也应该有一些编辑能力。 但是 mysite.de/objects/do/edit 给了我一个 404:“找不到页面”。
我 print_r’ed $wp_query 看起来 wp 查询一个名为“edit”的附件
希望有人能帮助我——到现在为止,我会坚持使用丑陋的 ?do=edit 档案的 url。
提前致谢,汉斯
//更新 09/03/2012
剥离代码 我的插件.php
add_action('init', 'object_register');
function object_register() {
$labels = array(
'name' => _x('Objects', 'post type general name'),
'singular_name' => _x('Object', 'post type singular name'),
'add_new' => _x('Add New', 'object'),
'add_new_item' => __('Add New Object'),
'edit_item' => __('Edit Object'),
'new_item' => __('New Object'),
'view_item' => __('View Object'),
'search_items' => __('Search Objects'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'has_archive' => 'objects', // this is the archive-slug (could be "objects/index"!!)
'rewrite' => array( 'slug' => 'objects' ), // this is the prepended slug (like "objects/my-object")
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','thumbnail')
);
register_post_type( 'dr_object' , $args );
}
add_action('init', 'cluster_register');
function cluster_register() {
$labels = array(
'name' => _x('Cluster', 'post type general name'),
'singular_name' => _x('Cluster', 'post type singular name'),
'add_new' => _x('Add New', 'cluster'),
'add_new_item' => __('Add New Cluster'),
'edit_item' => __('Edit Cluster'),
'new_item' => __('New Cluster'),
'view_item' => __('View Cluster'),
'search_items' => __('Search Cluster'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'cluster' ),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title')
);
register_post_type( 'dr_cluster' , $args );
}
add_action( 'init', 'build_taxonomies', 0 );
function build_taxonomies() {
register_taxonomy( 'dr_designers', array("dr_object"), array("hierarchical" => false, "label" => "Designers", "singular_label" => "Designer", "rewrite" => array('slug' => 'designer')));
register_taxonomy( 'dr_tags', array("dr_object", "dr_cluster"), array("hierarchical" => false, "label" => "Tags", "singular_label" => "Tag", "rewrite" => array('slug' => 'tags')));
register_taxonomy( 'dr_manufactors', array("dr_object"), array("hierarchical" => false, "label" => "Manufactors", "singular_label" => "Manufactor", "rewrite" => array('slug' => 'manufactors')));
}
function dr_rewrite_settings(){
// this enables to get action form objects, cluster etc by adding sth like "objects/my-object/do/edit" -- YEAH!
add_rewrite_endpoint('do', EP_PERMALINK | EP_PAGES );
// ATTENTION: This is *only* done during plugin activation hook in this example!
// You should *NEVER EVER* do this on every page load!!
flush_rewrite_rules();
}
//register_activation_hook( __FILE__, 'dr_rewrite_settings' ); this should happen
add_action('init', dr_rewrite_settings);
如您所见——我在以正确的方式刷新重写时遇到了问题(register_activation_hook)。
除了我应该提到的形式之外,我正在使用另一个插件来创建一个假页面:mysite.de/objects/new,它是这样的:
<?php
/**
* Plugin Name: Fake Page Plugin 2
* Plugin URI: http://scott.sherrillmix.com/blog/blogger/creating-a-better-fake-post-with-a-wordpress-plugin/
* Description: Creates a fake page without a 404 error (based on <a href="http://headzoo.com/tutorials/wordpress-creating-a-fake-post-with-a-plugin">Sean Hickey's Fake Plugin Page</a>)
* Author: Scott Sherrill-Mix
* Author URI: http://scott.sherrillmix.com/blog/
* Version: 1.1
*/
class FakePage
{
/**
* The slug for the fake post. This is the URL for your plugin, like:
* http://site.com/about-me or http://site.com/?page_id=about-me
* @var string
*/
var $page_slug = 'objects/new';
/**
* The title for your fake post.
* @var string
*/
var $page_title="New Object";
/**
* Allow pings?
* @var string
*/
var $ping_status="open";
/**
* Class constructor
*/
function FakePage()
{
/**
* We'll wait til WordPress has looked for posts, and then
* check to see if the requested url matches our target.
*/
add_filter('the_posts',array(&$this,'detectPost'));
}
/**
* Called by the 'detectPost' action
*/
function createPost()
{
/**
* What we are going to do here, is create a fake post. A post
* that doesn't actually exist. We're gonna fill it up with
* whatever values you want. The content of the post will be
* the output from your plugin.
*/
/**
* Create a fake post.
*/
$post = new stdClass;
/**
* The author ID for the post. Usually 1 is the sys admin. Your
* plugin can find out the real author ID without any trouble.
*/
$post->post_author = 1;
/**
* The safe name for the post. This is the post slug.
*/
$post->post_name = $this->page_slug;
/**
* Not sure if this is even important. But gonna fill it up anyway.
*/
$post->guid = get_bloginfo('wpurl') . "https://wordpress.stackexchange.com/" . $this->page_slug;
/**
* The title of the page.
*/
$post->post_title = $this->page_title;
/**
* This is the content of the post. This is where the output of
* your plugin should go. Just store the output from all your
* plugin function calls, and put the output into this var.
*/
$post->post_content = $this->getContent();
/**
* Fake post ID to prevent WP from trying to show comments for
* a post that doesn't really exist.
*/
$post->ID = -1;
/**
* Static means a page, not a post.
*/
$post->post_status="static";
/**
* Turning off comments for the post.
*/
$post->comment_status="closed";
/**
* Let people ping the post? Probably doesn't matter since
* comments are turned off, so not sure if WP would even
* show the pings.
*/
$post->ping_status = $this->ping_status;
$post->comment_count = 0;
/**
* You can pretty much fill these up with anything you want. The
* current date is fine. It's a fake post right? Maybe the date
* the plugin was activated?
*/
$post->post_date = current_time('mysql');
$post->post_date_gmt = current_time('mysql', 1);
add_post_meta($post->ID, '_wp_page_template', 'objects-new.php', true);
return($post);
}
function getContent()
{
return '<p>Hi there! You are viewing my fake post!</p>';
}
function detectPost($posts){
global $wp;
global $wp_query;
/**
* Check if the requested page matches our target
*/
if (strtolower($wp->request) == strtolower($this->page_slug)){ //09.03 removed: || $wp->query_vars['page_id'] == $this->page_slug
//Add the fake post
$posts=NULL;
$posts[]=$this->createPost();
/**
* Trick wp_query into thinking this is a page (necessary for wp_title() at least)
* Not sure if it's cheating or not to modify global variables in a filter
* but it appears to work and the codex doesn't directly say not to.
...
