Dodawanie pól do ekranu „Dodaj nowego użytkownika” na pulpicie nawigacyjnym


13

Chciałbym dodać pole „Nazwa firmy” do strony dodawania nowego użytkownika w panelu administracyjnym. Przeprowadziłem sporo poszukiwań i nie byłem w stanie znaleźć szczegółów, jak to zrobić. Mogę łatwo dodać informacje do strony profilu i zarejestrować się w ..

   function my_custom_userfields( $contactmethods ) {
    //Adds customer contact details
    $contactmethods['company_name'] = 'Company Name';
    return $contactmethods;
   }
   add_filter('user_contactmethods','my_custom_userfields',10,1);

Ale żadnych kości na niczym innym.


Do zaimplementowania tej funkcji możesz użyć wtyczki ACF (Advanced Custom Fields) .
Linish

Odpowiedzi:


17

user_new_form jest hakiem, który potrafi tutaj magię.

function custom_user_profile_fields($user){
  ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="company">Company Name</label></th>
            <td>
                <input type="text" class="regular-text" name="company" value="<?php echo esc_attr( get_the_author_meta( 'company', $user->ID ) ); ?>" id="company" /><br />
                <span class="description">Where are you?</span>
            </td>
        </tr>
    </table>
  <?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );

function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    if(!current_user_can('manage_options'))
        return false;

    # save my custom field
    update_usermeta($user_id, 'company', $_POST['company']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');

Aby uzyskać więcej informacji, odwiedź mój post na blogu: http://scriptbaker.com/adding-custom-fields-to-wordpress-user-profile-and-add-new-user-page/


13

Miałem tę samą potrzebę i stworzyłem następujący hack:

<?php
function hack_add_custom_user_profile_fields(){
    global $pagenow;

    # do this only in page user-new.php
    if($pagenow !== 'user-new.php')
        return;

    # do this only if you can
    if(!current_user_can('manage_options'))
        return false;

?>
<table id="table_my_custom_field" style="display:none;">
<!-- My Custom Code { -->
    <tr>
        <th><label for="my_custom_field">My Custom Field</label></th>
        <td><input type="text" name="my_custom_field" id="my_custom_field" /></td>
    </tr>
<!-- } -->
</table>
<script>
jQuery(function($){
    //Move my HTML code below user's role
    $('#table_my_custom_field tr').insertAfter($('#role').parentsUntil('tr').parent());
});
</script>
<?php
}
add_action('admin_footer_text', 'hack_add_custom_user_profile_fields');


function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    if(!current_user_can('manage_options'))
        return false;

    # save my custom field
    update_usermeta($user_id, 'my_custom_field', $_POST['my_custom_field']);
}
add_action('user_register', 'save_custom_user_profile_fields');

3
Teraz czekamy na twoje wyjaśnienie.
fuxia

Widziałem kod źródłowy w pliku user-new.php e nie mam haka akcji takiego jak „add_user_profile”, więc symuluję to za pomocą akcji „admin_footer_text” i filtruję według $ pagenow == „user-new.php”. Teraz skomentowałem hack, aby wyjaśnić kod.
NkR

3
Dlaczego nie używasz user_new_formakcji?
itsazzad

@SazzadTusharKhan tx za wskaźnik
alex

3

Musisz zrobić 2 rzeczy.

  1. Zarejestruj pola
  2. Zapisz pola

Uwaga: Poniższy przykład działa tylko dla administratorroli użytkownika.


1. Zarejestruj pola

Dla akcji Dodaj nowego użytkownika użyj akcjiuser_new_form

Na profil działania w ruchu show_user_profile,edit_user_profile

Zarejestruj pola Snippet:

/**
 * Add fields to user profile screen, add new user screen
 */
if( !function_exists('m_register_profile_fields') ) {

    //  This action for 'Add New User' screen
    add_action( 'user_new_form', 'm_register_profile_fields' );

    //  This actions for 'User Profile' screen
    add_action( 'show_user_profile', 'm_register_profile_fields' );
    add_action( 'edit_user_profile', 'm_register_profile_fields' );

    function m_register_profile_fields( $user ) {

        if ( !current_user_can( 'administrator', $user_id ) )
            return false;

        ?>

        <h3>Client Portal</h3>
        <table class="form-table">
            <tr>
                <th><label for="dropdown">Portal Category</label></th>
                <td>
                    <input type="text" class="regular-text" name="portal_cat" value="<?php echo esc_attr( get_the_author_meta( 'portal_cat', $user->ID ) ); ?>" id="portal_cat" /><br />
                </td>
            </tr>
        </table>
    <?php }
}

2. Zapisz pola

Dla akcji Dodaj nowego użytkownika użyj akcjiuser_register

Na profil działania w ruchu personal_options_update,edit_user_profile_update

Zapisz pola Snippet:

/**
 *  Save portal category field to user profile page, add new profile page etc
 */
if( !function_exists('m_register_profile_fields') ) {

    //  This action for 'Add New User' screen
    add_action( 'user_register', 'cp_save_profile_fields' );

    //  This actions for 'User Profile' screen
    add_action( 'personal_options_update', 'cp_save_profile_fields' );
    add_action( 'edit_user_profile_update', 'cp_save_profile_fields' );

    function cp_save_profile_fields( $user_id ) {

        if ( !current_user_can( 'administrator', $user_id ) )
            return false;

        update_usermeta( $user_id, 'portal_cat', $_POST['portal_cat'] );
    }
}

Kompletny fragment kodu:

/**
 * Add fields to user profile screen, add new user screen
 */
if( !function_exists('m_register_profile_fields') ) {

    //  This action for 'Add New User' screen
    add_action( 'user_new_form', 'm_register_profile_fields' );

    //  This actions for 'User Profile' screen
    add_action( 'show_user_profile', 'm_register_profile_fields' );
    add_action( 'edit_user_profile', 'm_register_profile_fields' );

    function m_register_profile_fields( $user ) {

        if ( !current_user_can( 'administrator', $user_id ) )
            return false;

        ?>

        <h3>Client Portal</h3>
        <table class="form-table">
            <tr>
                <th><label for="dropdown">Portal Category</label></th>
                <td>
                    <input type="text" class="regular-text" name="portal_cat" value="<?php echo esc_attr( get_the_author_meta( 'portal_cat', $user->ID ) ); ?>" id="portal_cat" /><br />
                </td>
            </tr>
        </table>
    <?php }
}


/**
 *  Save portal category field to user profile page, add new profile page etc
 */
if( !function_exists('m_register_profile_fields') ) {

    //  This action for 'Add New User' screen
    add_action( 'user_register', 'cp_save_profile_fields' );

    //  This actions for 'User Profile' screen
    add_action( 'personal_options_update', 'cp_save_profile_fields' );
    add_action( 'edit_user_profile_update', 'cp_save_profile_fields' );

    function cp_save_profile_fields( $user_id ) {

        if ( !current_user_can( 'administrator', $user_id ) )
            return false;

        update_usermeta( $user_id, 'portal_cat', $_POST['portal_cat'] );
    }
}

2

I obejście jest dostępne przy użyciu tego, user_new_form_tagktóry znajduje się w user-new.phptagu początkowym formularza strony. Jest na końcu, więc jeśli wypiszesz HTML, musisz po prostu rozpocząć >i usunąć ostatni wypisany >kod. Jak w:

function add_new_field_to_useradd()
{
    echo "><div>"; // Note the first '>' here. We wrap our own output to a 'div' element.

    // Your wanted output code should be here here.

    echo "</div"; // Note the missing '>' here.
}

add_action( "user_new_form_tag", "add_new_field_to_useradd" );

user_new_form_tagZnajduje się w user-new.phpokoło 303 linii (w WP3.5.1 przynajmniej):

...
<p><?php _e('Create a brand new user and add it to this site.'); ?></p>
<form action="" method="post" name="createuser" id="createuser" class="validate"<?php do_action('user_new_form_tag');?>>
<input name="action" type="hidden" value="createuser" />
...

Oczywiście wadą jest to, że wszystkie pola niestandardowe muszą pojawić się najpierw w formularzu, zanim pola zadeklarowane w WP core.


2

Haki są ważne, bez względu na to, jak posortowaliśmy pola w funkcji. Śledź moje wbudowane komentarze. Począwszy od WordPress 4.2.2 mamy teraz wiele haków:

<?php
/**
 * Declaring the form fields
 */
function show_my_fields( $user ) {
   $fetched_field = get_user_meta( $user->ID, 'my_field', true ); ?>
    <tr class="form-field">
       <th scope="row"><label for="my-field"><?php _e('Field Name') ?> </label></th>
       <td><input name="my_field" type="text" id="my-field" value="<?php echo esc_attr($fetched_field); ?>" /></td>
    </tr>
<?php
}
add_action( 'show_user_profile', 'show_my_fields' ); //show in my profile.php page
add_action( 'edit_user_profile', 'show_my_fields' ); //show in my profile.php page

//add_action( 'user_new_form_tag', 'show_my_fields' ); //to add the fields before the user-new.php form
add_action( 'user_new_form', 'show_my_fields' ); //to add the fields after the user-new.php form

/**
 * Saving my form fields
 */
function save_my_form_fields( $user_id ) {
    update_user_meta( $user_id, 'my_field', $_POST['my_field'] );
}
add_action( 'personal_options_update', 'save_my_form_fields' ); //for profile page update
add_action( 'edit_user_profile_update', 'save_my_form_fields' ); //for profile page update

add_action( 'user_register', 'save_my_form_fields' ); //for user-new.php page new user addition

1

user_contactmethodsFiltr hook nie jest wywoływany na user-new.phpstronie, więc nie będzie działać i niestety, jeśli spojrzysz na źródło , zobaczysz, że nie ma hooka, którego można użyć do dodania dodatkowych pól do formularza dodawania nowego użytkownika.

Można to zrobić tylko poprzez modyfikację plików podstawowych (DUŻY NIE NIE) lub dodanie pól za pomocą JavaScript lub jQuery i przechwycenie pól.

lub możesz stworzyć Bilet w Trac


Niestety, aby go uruchomić, byłem tymczasowo zmuszony zmodyfikować user-new.php. To jest nie nie. Ale mam nadzieję, że to tymczasowe.
Zach Shallbetter

1

Poniższy kod wyświetli „Informacje biograficzne” w formularzu „Dodaj użytkownika”


function display_bio_field() {
  echo "The field html";
}
add_action('user_new_form', 'display_bio_field');


Odpowiedź tylko na kod jest złą odpowiedzią. Spróbuj połączyć powiązany artykuł w Kodeksie i wyjaśnić kod tutaj.
Mayeenul Islam,

0

W tym celu musisz ręcznie zmienić stronę user-new.php. To nie jest właściwy sposób, aby sobie z tym poradzić, ale jeśli jesteś w rozpaczliwej potrzebie, tak to się robi.

dodałem

<tr class="form-field">
    <th scope="row"><label for="company_name"><?php _e('Company Name') ?> </label></th>
    <td><input name="company_name" type="text" id="company_name" value="<?php echo esc_attr($new_user_companyname); ?>" /></td>
</tr>

Dodałem również informacje do functions.php

   function my_custom_userfields( $contactmethods ) {
    $contactmethods['company_name']             = 'Company Name';
    return $contactmethods;
   }
   add_filter('user_contactmethods','my_custom_userfields',10,1);

0

Nie zrobi tego w przypadku strony dodawania nowego użytkownika, ale jeśli chcesz, aby stało się to na stronie „Twój profil” (gdzie użytkownicy mogą edytować swój profil), możesz to wypróbować w functions.php:

add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="companyname">Company Name</label></th>
            <td>
                <input type="text" name="companyname" id="companyname" value="<?php echo esc_attr( get_the_author_meta( 'companyname', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description">Where are you?</span>
            </td>
        </tr>
    </table>
<?php }
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.