3 Code formatting
source | link
$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;
        }
    }
$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;
        }
    }
$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;
    }
}
2 Code formatting
source | link

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

$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;
        }
    }

$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;
    }
}
$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;
        }
    }
1
source | link

Building on the previous answers, here is what I'm using to remove the parts of the User page I don't want:

$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;
    }
}

It's still reliant on the structure of the HTML, but it works for me.