Add Snippet To Project
Are you looking for a way to clean up user profiles in WordPress by removing personal options? Personal options in WordPress allow users to choose preferences for visual editing, color schemes, keyboard shortcuts, and more. Although these settings can be helpful, in certain cases, you may want to streamline the user interface by removing them. In this enlightening article, we will guide you through the steps to successfully remove personal options from user profiles on your WordPress site.
function wpturbo_remove_personal_options( $subject ) {
$subject = preg_replace('#<h2>Personal Options</h2>.+?/table>#s', '', $subject, 1);
return $subject;
}
add_filter( 'show_user_profile', 'wpturbo_remove_personal_options' );
add_filter( 'edit_user_profile', 'wpturbo_remove_personal_options' );
The given code snippet defines a function wpturbo_remove_personal_options($subject)
, which aims at removing personal options present on the user profile page in WordPress.
Within this function, the PHP preg_replace()
function is employed. This function is used to perform a regular expression search and replace. This regular function utilizes three main parameters or arguments:
- The search pattern:
'#<h2>Personal Options</h2>.+?/table>#s'
- The replacement text:
''
(an empty string, which means the matched content will be removed) - Input string:
$subject
The regular expression within the preg_replace()
function #<h2>Personal Options</h2>.+?/table>#s
is designed to match a specific HTML pattern that WordPress uses for rendering the personal options section on the profile page. To be specific, it matches the string "Personal Options" enveloped by
HTML tags, and all characters (.+?)
up until the next closing HTML tag. The trailing ‘s’ after the regular expression is a modifier that indicates ‘single line’, allowing the dot .
metacharacter to match newline (n
) characters, ensuring the matching continues until the next tag, irrespective of line breaks within the page’s source code.
The regular expression consequently matches the entirety of the Personal Options section, and the preg_replace()
function replaces it with an empty string ''
, effectively removing it from $subject
, which stands for the user profile page’s source code.
Finally, the function wpturbo_remove_personal_options()
is associated with two hooks using add_filter()
. WordPress maintains these hooks to allow developers to modify certain functionalities. The two hooks in question are show_user_profile
and edit_user_profile
. These hooks get triggered when the current user’s profile page and another user’s profile page are being displayed for editing, respectively.
By attaching wpturbo_remove_personal_options()
to these hooks, you ensure that the Personal Options section is removed whenever a user attempts to edit their profile, thereby fulfilling the purpose of the initial function call.