Ich möchte das Website-Feld aus den Kontaktinformationen des Benutzers entfernen. Ich verwende das Folgende, um das AIM, Jabber und Yahoo IM zu entfernen. Ich kann dies jedoch nicht zum Entfernen der Website verwenden. Jemand bitte helfen.
function remove_contactmethods( $contactmethods ) {
unset($contactmethods['aim']);
unset($contactmethods['yim']);
unset($contactmethods['Jabber']);
return $contactmethods;
}
add_filter('user_contactmethods','remove_contactmethods',10,1);
Überarbeitete und aktualisierte Antwort:
Der user_contactmethods
-Filter kann nicht zum Entfernen des Website-Wrappers verwendet werden, da dieses Teil in der user-edit.php
-Datei fest codiert ist und nicht Teil der filterbaren Benutzerkontakte -Schleife, die generiert wird von:
wp_get_user_contact_methods( $profileuser )
Das website row Element hat jetzt eine eigene .user-url-wrap
Klasse:
<tr class="user-url-wrap">
<th><label for="url"><?php _e('Website') ?></label></th>
<td>
<input type="url" name="url" id="url"
value="<?php echo esc_attr( $profileuser->user_url ) ?>"
class="regular-text code" />
</td>
</tr>
Bisher mussten wir jQuery verwenden, um die übergeordnete Zeile des Felds #url
zum Entfernen auszuwählen.
Aber jetzt können wir einfach den website wrapper ansteuern und ihn mit CSS verstecken:
function remove_website_row_wpse_94963_css()
{
echo '<style>tr.user-url-wrap{ display: none; }</style>';
}
add_action( 'admin_head-user-edit.php', 'remove_website_row_wpse_94963_css' );
add_action( 'admin_head-profile.php', 'remove_website_row_wpse_94963_css' );
Es gibt ähnliche Zeilenklassen:
tr.user-{field}-wrap
verfügbar für die Felder:
admin-color,
comment-shortcuts,
admin-bar-front,
user-login,
role,
super-admin,
first-name,
last-name,
nickname,
display-name,
email,
description,
pass1,
pass2,
sessions,
capabilities,
...
einschließlich aller Felder aus den dynamischen Benutzerkontakten Methoden.
Hier ersetzen wir einfach den Teil {field}
durch den entsprechenden Feldnamen.
Vor dem Entfernen der Website-Zeile:
Nach dem Entfernen der Website-Zeile:
Ich habe das Problem mit ob_ functions und DOMDocument behoben. Es ist besser als jQuery oder CSS, um das Formular zu schützen.
Ich verwende diese Art von Lösung jedes Mal, wenn ich nicht über einen Hook auf einen Teil des HTML-Inhalts zugreifen kann.
function remove_extra_field_profile()
{
$current_file_url = preg_replace( "#\?.*#" , "" , basename( $_SERVER['REQUEST_URI'] ) );
if( $current_file_url == "profile.php" )
{
add_action( 'wp_loaded', function(){ ob_start("profile_callback"); } );
add_action( 'shutdown', function(){ ob_end_flush(); } );
}
}
add_action( 'init', 'remove_extra_field_profile' );
function profile_callback( $html )
{
$profile_dom = new DOMDocument;
$profile_dom->loadHTML( $html );
$all_lines = $profile_dom->getElementsByTagname( 'tr' );
$excludes = array(
'user-rich-editing-wrap',
'user-admin-color-wrap',
'user-comment-shortcuts-wrap',
'show-admin-bar user-admin-bar-front-wrap',
'user-url-wrap',
'user-description-wrap'
);
$deletes = array();
foreach ( $all_lines as $line )
{
$tr_calss = $line->getAttribute("class");
if( in_array( $tr_calss, $excludes ) )
{
$deletes[] = $line;
}
}
$deletes[] = $profile_dom->getElementsByTagname( 'h2' )->item(0);
foreach ($deletes as $delete)
{
$delete->parentNode->removeChild( $delete );
}
return $profile_dom->saveHTML();
}
Erweitern Sie @ birgires und begründen Sie @Patricia Waltons Antwort, wenn Sie nur hinzufügen
add_action('admin_head-user-edit.php','remove_website_row_wpse_94963');
es wird nur von der Seite entfernt, auf der der Administrator ein Profil bearbeitet. Damit es auch verschwindet, wenn ein Benutzer sein eigenes Profil bearbeitet, fügen Sie es hinzu
add_action('admin_head-profile.php','remove_website_row_wpse_94963');
, wie folgt:
function remove_website_row_wpse_94963() {
if(!current_user_can('manage_options')){
// hide only for non-admins
echo "<script>jQuery(document).ready(function(){jQuery('#url').parents('tr').remove();});</script>";
}
}
add_action('admin_head-user-edit.php','remove_website_row_wpse_94963');
add_action('admin_head-profile.php','remove_website_row_wpse_94963');
Ausgehend von der Antwort von @ birgire habe ich dies in ein Array geschrieben, damit es etwas einfacher zu lesen ist:
function awb_remove_user_profile_fields_with_css() {
//Hide unwanted fields in the user profile
$fieldsToHide = [
'rich-editing',
'admin-color',
'comment-shortcuts',
'admin-bar-front',
'user-login',
'role',
'super-admin',
//'first-name',
//'last-name',
'nickname',
'display-name',
//'email',
'description',
//'pass1',
//'pass2',
'sessions',
'capabilities',
'syntax-highlighting',
'url'
];
//add the CSS
foreach ($fieldsToHide as $fieldToHide) {
echo '<style>tr.user-'.$fieldToHide.'-wrap{ display: none; }</style>';
}
//fields that don't follow the wrapper naming convention
echo '<style>tr.user-profile-picture{ display: none; }</style>';
//all subheadings
echo '<style>#your-profile h2{ display: none; }</style>';
}
add_action( 'admin_head-user-edit.php', 'awb_remove_user_profile_fields_with_css' );
add_action( 'admin_head-profile.php', 'awb_remove_user_profile_fields_with_css' );
Der Code funktionierte auch nicht für mich, aber das Ändern von add_action, um auf profile.php zu zeigen, funktionierte.
function remove_website_row_wpse_94963() {
if(!current_user_can('manage_options')){
// hide only for non-admins
echo "<script>jQuery(document).ready(function()
{jQuery('#url').parents('tr').remove();});</script>";
}
}
add_action('admin_head-user-edit.php','remove_website_row_wpse_94963');