如何从用户配置文件管理页面中删除传记

时间:2012-01-13 作者:Marc

我想从个人资料页面中删除或隐藏传记输入字段。你是怎么做到的?我已经从这个页面删除了一些联系方式,但我不知道如何删除传记。

5 个回复
最合适的回答,由SO网友:fuxia 整理而成

没有专门的挂钩——WordPress中用户管理的优先级较低。您必须使用输出缓冲(是的,不是很好)。

下面是一个简单的演示如何做到这一点:

add_action( \'personal_options\', array ( \'T5_Hide_Profile_Bio_Box\', \'start\' ) );

/**
 * Captures the part with the biobox in an output buffer and removes it.
 */
class T5_Hide_Profile_Bio_Box
{
    /**
     * Called on \'personal_options\'.
     *
     * @return void
     */
    public static function start()
    {
        $action = ( IS_PROFILE_PAGE ? \'show\' : \'edit\' ) . \'_user_profile\';
        add_action( $action, array ( __CLASS__, \'stop\' ) );
        ob_start();
    }

    /**
     * Strips the bio box from the buffered content.
     *
     * @return void
     */
    public static function stop()
    {
        $html = ob_get_contents();
        ob_end_clean();

        // remove the headline
        $headline = __( IS_PROFILE_PAGE ? \'About Yourself\' : \'About the user\' );
        $html = str_replace( \'<h2>\' . $headline . \'</h2>\', \'\', $html );

        // remove the table row
        $html = preg_replace( \'~<tr>\\s*<th><label for="description".*</tr>~imsUu\', \'\', $html );
        print $html;
    }
}
您可以将代码作为独立插件下载:Plugin Remove Bio Box.

密码字段现在位于联系人信息下……如果您不喜欢,请在中添加标题stop() – 并注意I18n.)

SO网友:Rich

自最近的课程更改以来,这一做法行之有效:

add_action( \'personal_options\', array ( \'T5_Hide_Profile_Bio_Box\', \'start\' ) );

/**
 * Captures the part with the biobox in an output buffer and removes it.
 *
 * @author Thomas Scholz, <[email protected]>
 *
 */
class T5_Hide_Profile_Bio_Box
{
    /**
     * Called on \'personal_options\'.
     *
     * @return void
     */
    public static function start()
    {
        $action = ( IS_PROFILE_PAGE ? \'show\' : \'edit\' ) . \'_user_profile\';
        add_action( $action, array ( __CLASS__, \'stop\' ) );
        ob_start();
    }

    /**
     * Strips the bio box from the buffered content.
     *
     * @return void
     */
    public static function stop()
    {
        $html = ob_get_contents();
        ob_end_clean();

        // remove the headline
        $headline = __( IS_PROFILE_PAGE ? \'About Yourself\' : \'About the user\' );
        $html = str_replace( \'<h3>\' . $headline . \'</h3>\', \'\', $html );

        // remove the table row
        $html = preg_replace( \'~<tr class="user-description-wrap">\\s*<th><label for="description".*</tr>~imsUu\', \'\', $html );
        print $html;
    }
}

SO网友:Phill Healey

最简单、最轻量级的解决方案是使用CSS将其从视图中隐藏。

.user-description-wrap {
   display: none;
}

SO网友:jxmallett

在前面的答案的基础上,以下是我用来删除我不想要的用户页面部分的内容:

$pagesToAffect = [
    \'/wp-admin/user-edit.php\',
    \'/wp-admin/profile.php\'
];

if (isset($PHP_SELF) && in_array($PHP_SELF, $pagesToAffect)) {
    add_action(\'admin_head\', [UserProfileCleaner::class, \'start\']);
    add_action(\'admin_footer\', [UserProfileCleaner::class, \'finish\']);
    add_filter(\'user_contactmethods\',[UserProfileCleaner::class, \'hideInstantMessaging\'],10000,1);
}

class UserProfileCleaner {
    public static function start() {
        ob_start(function($buffer) {
            // Personal Options
            if (!IS_PROFILE_PAGE) {
                $startHeading = \'Personal Options\';
                $pattern = "@<(h[0-9]) ?[^>]*?>".preg_quote(_x($startHeading))."</\\\\1 ?>@is";
                preg_match($pattern, $buffer, $start, PREG_OFFSET_CAPTURE);

                $endHeading = \'Name\';
                $pattern = "@<(h[0-9]) ?[^>]*?>".preg_quote(_x($endHeading))."</\\\\1 ?>@is";
                preg_match($pattern, $buffer, $end, PREG_OFFSET_CAPTURE);

                if (isset($start[0][1]) && isset($end[0][1]) && $start[0][1]<$end[0][1]) {
                    $buffer = substr($buffer, 0, $start[0][1]).substr($buffer,$end[0][1]);
                }
            }

            $buffer = self::removeSectionHeading($buffer, \'Name\');
            $buffer = self::removeSectionHeading($buffer, \'Contact Info\');

            $buffer = self::removeSectionHeading($buffer, \'Additional Capabilities\');
            $buffer = self::removeSectionRow($buffer, \'Capabilities\');

            $buffer = self::removeSectionHeading($buffer, \'Forums\');

            // About / Bio
            $heading = IS_PROFILE_PAGE ? \'About Yourself\' : \'About the user\';
            $buffer = self::removeStandardSection($buffer, $heading);

            // Yoast
            $heading = \'Yoast SEO Settings\';
            $buffer = self::removeStandardSection($buffer, $heading);

            $heading = \'Memberships\';
            $pattern = "@<(h[0-9]) ?[^>]*?>".preg_quote(_x($heading))."</\\\\1 ?>.*?</p>@is";
            $buffer = preg_replace($pattern, "", $buffer, 1);

            return $buffer;
        });
    }

    private static function removeStandardSection($buffer, $heading) {
        $pattern = "@<(h[0-9]) ?[^>]*?>".preg_quote(_x($heading))."</\\\\1 ?>.*?</table>@is";
        return preg_replace($pattern, "", $buffer, 1);
    }

    private static function removeSectionHeading($buffer, $heading) {
        $pattern = "@<(h[0-9]) ?[^>]*?>".preg_quote(_x($heading))."</\\\\1 ?>@is";
        return preg_replace($pattern, "", $buffer, 1);
    }

    function removeSectionRow($buffer, $heading) {
        $pattern = "@<tr ?[^>]*?>[^<]*?<th ?[^>]*?>[^<]*?".preg_quote(_x($heading))."[^<]*?</th ?[^>]*?>.*?</tr ?>@is";
        return preg_replace($pattern, "", $buffer, 1);
    }

    public static function finish() {
        ob_end_flush();
    }

    public static function hideInstantMessaging( $contactmethods ) {
        unset($contactmethods[\'googleplus\']);
        unset($contactmethods[\'twitter\']);
        unset($contactmethods[\'facebook\']);
        return $contactmethods;
    }
}
它仍然依赖于HTML的结构,但对我来说是可行的。

SO网友:SwitzerBaden

如果将以下代码添加到函数中。php文件,它将删除多语言网站中所有语言的bio部分:

//remove the bio
function remove_plain_bio($buffer) {
    $titles = array(\'#<h3>\'._x(\'About Yourself\').\'</h3>#\',\'#<h3>\'._x(\'About the user\').\'</h3>#\');
    $buffer=preg_replace($titles,\'<h3>\'._x(\'Password\').\'</h3>\',$buffer,1);
    $biotable=\'#<h3>\'._x(\'Password\').\'</h3>.+?<table.+?/tr>#s\';
    $buffer=preg_replace($biotable,\'<h3>\'._x(\'Password\').\'</h3> <table class="form-table">\',$buffer,1);
    return $buffer;
}
function profile_admin_buffer_start() { ob_start("remove_plain_bio"); }
function profile_admin_buffer_end() { ob_end_flush(); }
add_action(\'admin_head\', \'profile_admin_buffer_start\');
add_action(\'admin_footer\', \'profile_admin_buffer_end\');

结束

相关推荐