我正在尝试根据用户发布新帖子时发布的帖子数更新用户角色。我已经尝试过这个代码,但它不起作用。
add_action(\'publish_post\', \'update_roles\');
function update_roles()
{
global $wpdb;
// Get the author
$author = wp_get_current_user();
$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );
$numPost = count($posts);
// Do the checks to see if they have the roles and if not update them.
if($numPost > 0 && $numPosts <= 2 && current_user_can(\'subscriber\'))
{
$user_id_role = new WP_User($user_id);
$user_id_role->set_role(\'contributor\');
} elseif ($numPost > 3 && $numPosts <= 5 && current_user_can(\'contributor\'))
{
$user_id_role = new WP_User($user_id);
$user_id_role->set_role(\'author\');
} elseif ($numPost > 6 && $numPosts <= 9 && current_user_can(\'author\'))
{
$user_id_role = new WP_User($user_id);
$user_id_role->set_role(\'author\');
}
}
?>
我也尝试过这个,但它也不起作用。<?php
add_action(\'publish_post\', \'update_roles\');
function update_roles()
{
global $wpdb;
// Get the author
$author = wp_get_current_user();
// Get post by author
$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );
$numPost = count($posts);
// Do the checks to see if they have the roles and if not update them.
if($numPost > 0 && $numPosts <= 2 && current_user_can(\'subscriber\'))
{
// Remove role
$author->remove_role( \'subscriber\' );
// Add role
$author->add_role( \'contributor\' );
} elseif ($numPost > 3 && $numPosts <= 5 && current_user_can(\'contributor\'))
{
// Remove role
$author->remove_role( \'contributor\' );
// Add role
$author->add_role( \'author\' );
} elseif ($numPost > 6 && $numPosts <= 9 && current_user_can(\'author\'))
{
// Remove role
$author->remove_role( \'author\' );
// Add role
$author->add_role( \'editor\' );
}
}
?>
EDIT NEW: 你能告诉我我做得对不对吗?<?php
add_action(\'publish_post\', \'update_roles\');
function update_roles()
{
global $wpdb;
// Get the author
$author = wp_get_current_user();
// Get post by author
$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );
$numPost = count($posts);
// Do the checks to see if they have the roles and if not update them.
if ($numPost > 3 && $numPosts <= 5 && array_key_exists( \'contributor\', $old_role ) )
{
$user_id_role = new WP_User($user_id);
$user_id_role->set_role(\'author\');
} elseif ($numPost > 6 && $numPosts <= 9 && array_key_exists( \'author\', $old_role ) )
{
$user_id_role = new WP_User($user_id);
$user_id_role->set_role(\'editor\');
}
}
?>