• Guys, how can I create a field in the WordPress registration form?
    I would like to create a “Company name” field in this field that is of the text type, but it should work in the standard wordpress user search. You must enter this company information in the Site field that already exists and works in standard search, but when you fill in the field, it changes to http: //.com.

    ——–
    I tried to do it that way, however, it doesn’t work in standard search.

    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

    can anybody help me?

Viewing 11 replies - 1 through 11 (of 11 total)
  • If you want to make a customer user profile field searchable, you’ll need to adjust the SQL query directly to include the user meta key for the custom field. WP_User_Query class is what you’ll want to work with, likely the pre_user_query action, and adjust the meta_query property (a WP_Meta_Query object).

    • This reply was modified 4 years, 11 months ago by crstauf.
    Moderator t-p

    (@t-p)

    Thread Starter tiicaa

    (@tiicaa)

    @t-p It does not help me already tried but it does not work.

    @crstauf I don’t understand what you mean

    @tiicaa The code you provided does add the field to the user profile’s form, but you’ll need to do more to include the custom field in the user search, which uses the WP_User_Query class.

    You created the action callbacks, so it seems you do know some PHP: find and review the WP_User_Query class, and create a callback for the pre_user_query action, review the parameter passed to the callback, especially the meta_query property (which is a WP_Meta_Query object).

    If you are not a PHP developer and need some help, post here, and myself or someone may provide that to you.

    Thread Starter tiicaa

    (@tiicaa)

    @crstauf I know very little php, I managed to assemble this code because it was a combination of some tutorials I saw. If someone can help seriously, it will be of great help. I insert this code in my child theme functions

    Thread Starter tiicaa

    (@tiicaa)

    Para mim também serviria quebrar o campo SITE que já existe por padr?o no wordpress, eu poderia utilizar aquele campo e só mudaria o rótulo da label que é mais fácil. Porém, todas as vezes que inserimos um texto no campo ele automaticamente converte para padr?o de url (https://www&#8230;). Como posso quebrar isso no meu functions do tema? Isso já me serviria

    Thread Starter tiicaa

    (@tiicaa)

    For me it would also be useful to break the SITE field that already exists by default in wordpress, I could use that field and only change the label of the label, which is easier. However, every time we enter text in the field it automatically converts to a url pattern (http: // www & # 8230;). How can I break this in my theme functions? That would already serve me

    @tiicaa Okay, I’ve spent way longer than I expected I would working on a solution: WordPress does not make adjusting the user search query easy, especially when concerning meta data.

    I’m inclined to think that searching meta data of users is not possible with the built-in search field (introducing your own search functionality would be pretty easy), but I’m certainly not the best WP developer out there.

    A solution for this (besides writing a custom search) is above my skill-level.

    Moderator bcworkz

    (@bcworkz)

    You can use the “pre_user_query” action like crstauf suggests. There you would need to manipulate the SQL clauses that WP came up with. I personally would rather use “pre_get_users” action where we can instead set query vars that modify the query that WP will come up with. Most find query vars more accessible than SQL clauses.

    Lets say your action callback collects the passed WP_User_Query object as $query. You’ll want to use $query->set() to set the “meta_key” query var to “my_field” and set the “meta_value” query var to the search term, which can be taken from $query->get('s')

    Your callback should verify that 'users' == get_current_screen()->id and that the ‘s’ query var is set before modifying the query so that you do not modify queries that you shouldn’t. The logic between other search criteria and the meta values will be AND. If you need OR logic, then you need to also use the “pre_user_query” action to modify the WHERE clause to use OR instead of AND in relation to the meta data portion of the clause.

    https://developer.www.ads-software.com/reference/classes/wp_user_query/
    The noted actions occur in WP_User_Query::prepare_query() which is linked to from the above page under “Methods”

    Thread Starter tiicaa

    (@tiicaa)

    @bcworkz

    I appreciate your help, but I really don’t know how to implement this into the code. I tried in some ways I didn’t succeed.

    Moderator bcworkz

    (@bcworkz)

    Maybe the examples for “pre_get_posts” might help you with the basics. It’s very similar to “pre_get_users” except it’s for posts instead of users, but better documented. Start with minimal code and get that working, then incrementally embellish, testing frequently all the way.

    For example, don’t verify the current screen yet, let your code modify all user queries for the time being. Hardcode the values used to set query vars for now, you can work on getting the value from search terms later.

    Custom code like this can go in a simple plugin, or a child or custom theme’s functions.php. If you still have trouble, post your best effort at minimal code here and maybe someone can help you find what’s wrong.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How can I create a field in the WordPress registration form?’ is closed to new replies.