禁用单个帐户的个人资料编辑

发布于 2024-12-04 07:23:01 字数 184 浏览 1 评论 0原文

我已经在 WordPress 中创建了一个公共帐户,我将其发送给 100 个用户。
所以登录名是:

用户名:public
密码:123example

我唯一想要的就是隐藏这个特定用户帐户的个人资料页面,这样他们就无法更改密码、电子邮件地址等。

如何实现这一点?也许改变一些php?

I have made a public account in wordpress which I will send to 100 users.
So the login would be:

Username: public
Password: 123example

The only thing I want is to hide the profile page for this specific user account so they can't change password, emailadress, etc.

How to achieve this? Maybe change some php?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子的影子 2024-12-11 07:23:01

@aSeptik 答案的最后一部分可能对 WP 更友好一些。

function force_profile_redirect() {
    global $pagenow, $current_user;
    get_currentuserinfo();

    if ($pagenow == 'profile.php' && $current_user->user_login == 'public') {
        wp_redirect(home_url());
    }   
}
add_action('admin_init', 'force_profile_redirect');

The last portion in @aSeptik's answer could be a little more WP friendly.

function force_profile_redirect() {
    global $pagenow, $current_user;
    get_currentuserinfo();

    if ($pagenow == 'profile.php' && $current_user->user_login == 'public') {
        wp_redirect(home_url());
    }   
}
add_action('admin_init', 'force_profile_redirect');
香橙ぽ 2024-12-11 07:23:01

该脚本涵盖了问题的所有方面,请阅读代码注释以获取进一步的解释。

<?php
/**
 * this make sure the public user where redirected
 * to home instead of profile page
 */
function redirect_user_to($redirect_to, $request, $user)
{
  global $user;
  if ($user->user_login == 'public') {
    return home_url();
  }
  else {
    return home_url("/wp-admin/");
  }
}
add_filter('login_redirect', 'redirect_user_to', 10, 3);

/**
 * this remove the profile links from
 * the top nav menu
 */
function remove_edit_profile()
{
  global $wp_admin_bar, $current_user;
  get_currentuserinfo();
  if ($current_user->user_login == 'public') {
    $wp_admin_bar->remove_menu('edit-profile');
    $wp_admin_bar->remove_menu('my-account-with-avatar');
    $wp_admin_bar->remove_menu('my-account');
  }
}
add_action('wp_before_admin_bar_render', 'remove_edit_profile', 0);

/**
 * this remove the "Site Admin" link from
 * the WP meta widget, usually placed in
 * the side bar.
 */
function my_unregister_widgets()
{
  unregister_widget('WP_Widget_Meta');
  register_widget('MY_Widget_Meta');
}
add_action('widgets_init', 'my_unregister_widgets');

class MY_Widget_Meta extends WP_Widget
{

  function MY_Widget_Meta()
  {
    $widget_ops = array(
      'classname' => 'widget_meta',
      'description' => __("Log in/out, admin, feed and WordPress links"),
    );
    $this->WP_Widget('meta', __('Meta'), $widget_ops);
  }

  function widget($args, $instance)
  {
    extract($args);
    $title = apply_filters('widget_title', empty($instance['title']) ? __('Meta') : $instance['title']);
    echo $before_widget;
    if ($title) {
      echo $before_title.$title.$after_title;
    }
    ?>
                    <ul>
                    <?php
        global $current_user;
    get_currentuserinfo();
    if ($current_user->user_login == 'public') {
    }
    else {
      wp_register();
    }
    ?>
<li>
<?php wp_loginout();?>
</li>
<li>
<a href="<?php bloginfo('rss2_url');?>" title="<?php echo esc_attr(__('Syndicate this site using RSS 2.0'));?>">
    <?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>');?></a>
</li>
<li>
<a href="<?php bloginfo('comments_rss2_url');?>" title="<?php echo esc_attr(__('The latest comments to all posts in RSS'));?>">
    <?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>');?></a>
</li>
<li>
<a href="http://wordpress.org/" title="<?php echo esc_attr(__('Powered by WordPress, state-of-the-art semantic personal publishing platform.'));?>">WordPress.org</a>
</li>
<?php wp_meta();?>
</ul>
        <?php
                echo $after_widget;
  }
}

/**
 * this prevent from non authorized user ( public )
 * to pointing to the profile page by writing into
 * the address bar.
 */
function force_profile_redirect()
{
  global $pagenow, $current_user;
  if (strtolower($current_user->user_login) == 'public') {
    wp_redirect(home_url());
  }
}
add_action('admin_init', 'force_profile_redirect');
?>

this script cover all the aspects of the question, read the code comments for further explanation.

<?php
/**
 * this make sure the public user where redirected
 * to home instead of profile page
 */
function redirect_user_to($redirect_to, $request, $user)
{
  global $user;
  if ($user->user_login == 'public') {
    return home_url();
  }
  else {
    return home_url("/wp-admin/");
  }
}
add_filter('login_redirect', 'redirect_user_to', 10, 3);

/**
 * this remove the profile links from
 * the top nav menu
 */
function remove_edit_profile()
{
  global $wp_admin_bar, $current_user;
  get_currentuserinfo();
  if ($current_user->user_login == 'public') {
    $wp_admin_bar->remove_menu('edit-profile');
    $wp_admin_bar->remove_menu('my-account-with-avatar');
    $wp_admin_bar->remove_menu('my-account');
  }
}
add_action('wp_before_admin_bar_render', 'remove_edit_profile', 0);

/**
 * this remove the "Site Admin" link from
 * the WP meta widget, usually placed in
 * the side bar.
 */
function my_unregister_widgets()
{
  unregister_widget('WP_Widget_Meta');
  register_widget('MY_Widget_Meta');
}
add_action('widgets_init', 'my_unregister_widgets');

class MY_Widget_Meta extends WP_Widget
{

  function MY_Widget_Meta()
  {
    $widget_ops = array(
      'classname' => 'widget_meta',
      'description' => __("Log in/out, admin, feed and WordPress links"),
    );
    $this->WP_Widget('meta', __('Meta'), $widget_ops);
  }

  function widget($args, $instance)
  {
    extract($args);
    $title = apply_filters('widget_title', empty($instance['title']) ? __('Meta') : $instance['title']);
    echo $before_widget;
    if ($title) {
      echo $before_title.$title.$after_title;
    }
    ?>
                    <ul>
                    <?php
        global $current_user;
    get_currentuserinfo();
    if ($current_user->user_login == 'public') {
    }
    else {
      wp_register();
    }
    ?>
<li>
<?php wp_loginout();?>
</li>
<li>
<a href="<?php bloginfo('rss2_url');?>" title="<?php echo esc_attr(__('Syndicate this site using RSS 2.0'));?>">
    <?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>');?></a>
</li>
<li>
<a href="<?php bloginfo('comments_rss2_url');?>" title="<?php echo esc_attr(__('The latest comments to all posts in RSS'));?>">
    <?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>');?></a>
</li>
<li>
<a href="http://wordpress.org/" title="<?php echo esc_attr(__('Powered by WordPress, state-of-the-art semantic personal publishing platform.'));?>">WordPress.org</a>
</li>
<?php wp_meta();?>
</ul>
        <?php
                echo $after_widget;
  }
}

/**
 * this prevent from non authorized user ( public )
 * to pointing to the profile page by writing into
 * the address bar.
 */
function force_profile_redirect()
{
  global $pagenow, $current_user;
  if (strtolower($current_user->user_login) == 'public') {
    wp_redirect(home_url());
  }
}
add_action('admin_init', 'force_profile_redirect');
?>
み零 2024-12-11 07:23:01

如果用户 ID 为 [xyz],您需要修改您的个人资料页面代码,使其不显示可编辑区域,并且不运行“更新个人资料”操作。

对于实际更新个人资料的页面,您可以在顶部放置类似的内容

// Change this line to match however you identify your logged-in user
// And change the id number to the ID of the public user
global $current_user;
get_currentuserinfo();
if ($current_user->ID == 1)
{
    // Stop them seeing this page
    header('Location: index.php');
    // And for good measure
    die();
}

对于他们可以在提交表单之前更改个人资料字段的页面,您可以执行类似的操作

// Change this line to match however you identify your logged-in user
// And change the id number to the ID of the public user
global $current_user;
get_currentuserinfo();
if ($current_user->ID == 1)
{
    // Say no
    echo '<p>You cannot edit your profile on this account.</p>';
    // And for good measure
    die();
}

而无需查看代码,它是很难说得更具体,但这应该会在推动下起作用,即使它不完全是你想要的工作方式。

You'd need to modify your profile page code, to make it not show the editable areas, and not run the "update profile" action, if the user ID is [xyz].

For the page which actually does the updating of the profile, you can just put at the top something like

// Change this line to match however you identify your logged-in user
// And change the id number to the ID of the public user
global $current_user;
get_currentuserinfo();
if ($current_user->ID == 1)
{
    // Stop them seeing this page
    header('Location: index.php');
    // And for good measure
    die();
}

For the page on which they can change the profile fields before they submit the form, you can do something like this

// Change this line to match however you identify your logged-in user
// And change the id number to the ID of the public user
global $current_user;
get_currentuserinfo();
if ($current_user->ID == 1)
{
    // Say no
    echo '<p>You cannot edit your profile on this account.</p>';
    // And for good measure
    die();
}

Without seeing your code, it's hard to be more specific, but this should work at a push, even if it's not exactly how you want it to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文