A WP user with the role "Author" can post articles. On the blog in question I have the requirement, that these users' articles have to be live immediately but not publicly visible (i.e., for anonymous visitors or Subscribers). We use WP 3.0.5.
We already have a plugin running, that allows to hide categories from anonymous and Subscribers. So the most straight-forward method I came up with so far is: New blog posts by Authors should be automatically put in a category. Then I hide that category from anonymous users.
Does anyone know开发者_Go百科:
a) how to automatically put an article by "Author" users in a certain category, or
b) how the requirement "live but not public" could be achieved more elegantly for those posts?
(Plugin suggestions are welcome, too.)
What you probably want to do is write function to do this in your theme's functions.php
file, and then use add_action
to trigger that function when a post is saved.
For example:
function update_category_on_save($post_id) {
// Get post
$post = wp_get_single_post($post_id)
// Map author IDs to category IDs
$mapping = array(
1 => array(123),
2 => array(234),
);
// Update the post
$new_category = $mapping[$post->post_author];
$u_post = array();
$u_post['ID'] = $post_id;
$u_post['post_category'] = $new_category;
// Only update if category changed
if($post->post_category != $new_category[0]) {
wp_update_post($u_post);
}
}
add_action('category_save_pre', 'update_category_on_save');
Hope that makes sense, and gives you a hint as to how to do this — I'm afraid I haven't been able to test it.
The following code will change posts made by an author to private automatically.
function change_author_posts_to_private( $post_status ) {
// if the user is just saving a draft, we want to keep it a draft
if ( $post_status == 'draft' )
return $post_status;
$this_user = new WP_User( $_POST[ 'post_author' ] );
// this is assuming the user has just one role, which is standard
if ( $this_user->roles[0] == 'author' )
return 'private';
else
return $post_status;
}
add_filter( 'status_save_pre', 'change_author_posts_to_private' );
it filters the status on the post save, looks to see who the author is from the post variables, fetches their first role and sees if it's author, if it is, then it returns 'private', otherwise it returns the natural status. No need to use categories to do it when you can do it directly here.
精彩评论