Support » Plugin: Theme My Login » How to use the function wp_get_user_contact_methods() has a filter.

  • Resolved kamarr.cos

    (@kamarrcos)


    @jfarthing84
    The function wp_get_user_contact_methods() has a filter called user_contactmethods.` You would filter them using that:

    
    unction filter_user_contact_methods( $methods ) {
        $wanted_methods = array( 'city', 'state', 'zip' );
        foreach ( $methods as $method => $label ) {
            if ( ! isset( $wanted_methods[ $method ] ) ) {
                unset( $methods[ $method ] );
            }
        }
        return $methods;
    }
    add_filter( 'user_contactmethods', 'filter_user_contact_methods' );
    

    How would I implement that into this block of code?

    <?php
    			foreach ( wp_get_user_contact_methods() as $name => $desc ) {
    			    
    		?>
    		
    		<tr class="tml-user-contact-method-<?php echo $name; ?>-wrap">
    			<th><label for="<?php echo $name; ?>"><?php echo apply_filters( 'user_'.$name.'_label', $desc ); ?></label></th>
    			<td><input type="text" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="<?php echo esc_attr( $profileuser->$name ); ?>" class="regular-text" /></td>
    		</tr>
    		<?php
    			}
    		?>
Viewing 15 replies - 1 through 15 (of 17 total)
  • Isn’t that same as https://wordpress.org/support/topic/profile-page-edit-and-save-contact-info/ ?

    Just place the code he provided in your functions.php

    Thread Starter kamarr.cos

    (@kamarrcos)

    Thank you @tacov.

    I added the code to my function.php file however it removes all contact methods including the methods in the $wanted_methods array. How would I fix that?

    Change this
    if ( ! isset( $wanted_methods[ $method ] ) ) {

    into this

    if ( ! in_array( $method, $wanted_methods ) ) {

    If that didn’t help, add this to the top of the function and post the result
    var_dump($methods);die();

    • This reply was modified 5 years, 8 months ago by TacoV. Reason: code formatting
    Plugin Author Jeff Farthing

    (@jfarthing84)

    @tacov is right. It should be the in_array variant.

    Thread Starter kamarr.cos

    (@kamarrcos)

    Thank you @tacov and @jfarthing84.

    I’ve replace the code for the in_array variant however the code still removes all contact methods.

    The code now looks like this

    
    function filter_user_contact_methods( $methods ) {
        $wanted_methods = array( 'city', 'state', 'zip' );
        foreach ( $methods as $method => $label ) {
            if ( ! in_array( $method, $wanted_methods ) ) {
                unset( $methods[ $method ] );
            }
        }
        return $methods;
    }
    add_filter( 'user_contactmethods', 'filter_user_contact_methods' );
    

    If that didn’t help, add this to the top of the function and post the result
    var_dump($methods);die();

    Thread Starter kamarr.cos

    (@kamarrcos)

    @taco
    After inserting var_dump($methods);die(); to the top of the function, the page returns as null.

    That’s weird. That would mean that if you remove this snippet alltogether, still no methods would show up. Could you test that?

    Is there some other piece of code hooking into user_contactmethods? Are there typos somewhere in this code?

    Thread Starter kamarr.cos

    (@kamarrcos)

    I’ve removed the snippet of code and all the contact methods reappear.

    I’ve also searched the code to confirm there where no other pieces of code hooking into user_contactmethods.

    Thread Starter kamarr.cos

    (@kamarrcos)

    With exceptions to the the TML original code.

    <?php
    			foreach ( wp_get_user_contact_methods() as $name => $desc ) {
    		?>
    		<tr class="tml-user-contact-method-<?php echo $name; ?>-wrap">
    			<th><label for="<?php echo $name; ?>"><?php echo apply_filters( 'user_'.$name.'_label', $desc ); ?></label></th>
    			<td><input type="text" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="<?php echo esc_attr( $profileuser->$name ); ?>" class="regular-text" /></td>
    		</tr>
    		<?php
    			}
    		?>

    Very strange…

    Let’s build it step by step and see where it breaks.

    First, try an empty function. Do the default methods still appear?

    function test_but_do_nothing( $the_methods ) {
        return $the_methods;
    }
    add_filter( 'user_contactmethods', 'test_but_do_nothing' );

    Then, try the example from the docs:
    https://codex.wordpress.org/Plugin_API/Filter_Reference/user_contactmethods

    function modify_user_contact_methods( $user_contact ) {
    
    	// Add user contact methods
    	$user_contact['skype']   = __( 'Skype Username'   );
    	$user_contact['twitter'] = __( 'Twitter Username' );
    
    	// Remove user contact methods
    	unset( $user_contact['aim']    );
    	unset( $user_contact['jabber'] );
    
    	return $user_contact;
    }
    add_filter( 'user_contactmethods', 'modify_user_contact_methods' );
    Thread Starter kamarr.cos

    (@kamarrcos)

    After inserting the empty function.

    function test_but_do_nothing( $the_methods ) {
        return $the_methods;
    }
    add_filter( 'user_contactmethods', 'test_but_do_nothing' );
    

    The default methods still appear.

    After inserting the example from the codex.

    
    function modify_user_contact_methods( $user_contact ) {
    
    	// Add user contact methods
    	$user_contact['skype']   = __( 'Skype Username'   );
    	$user_contact['twitter'] = __( 'Twitter Username' );
    
    	// Remove user contact methods
    	unset( $user_contact['aim']    );
    	unset( $user_contact['jabber'] );
    
    	return $user_contact;
    }
    add_filter( 'user_contactmethods', 'modify_user_contact_methods' );
    

    The original fields remain in addition to the two new fields Twitter and Skype.

    P.S. I’ve tried changing the unset line of code to point to facebook and pintrest, however it won’t remove the fields.

    • This reply was modified 5 years, 8 months ago by kamarr.cos.
    • This reply was modified 5 years, 8 months ago by kamarr.cos.
    Thread Starter kamarr.cos

    (@kamarrcos)

    Update:

    I’ve tried removing the 'twitter' field that was created with the function,

    
    $user_contact['twitter'] = __( 'Twitter Username' );
    

    By using

    
    unset( $user_contact['twitter']    );
    

    which was successful.

    I’ve tried pointing to the original fields with no success. Maybe I’m not using the proper pointer. What would be the proper way to remove the facebook, twitter, google id, pinterest, and company name, or remove the original contact method fields and use the

    
    $user_contact['twitter'] = __( 'Twitter Username' );
    

    to add any and all desired fields.

    It would help to inspect the methods you start out with, but you said var_dump()-ing the array yielded null. I really doubt this is true now. Could you try var_dump()-ing or print_r()-ing this $user_contact array? Don’t forget to look in the source!

    Otherwise, to quickly list the proper pointers within the template, use them as values:

    $keys = array_keys($user_contact);
    $user_contact = array_combine($keys,$keys);
    return $user_contact;

    This will help in identifying what fields to unset.

    Thread Starter kamarr.cos

    (@kamarrcos)

    Thank you!!! @tacov you are the greatest.

    Using

    
    $keys = array_keys($user_contact);
    $user_contact = array_combine($keys,$keys);
    return $user_contact;
    

    I was able to find the proper pointers and have successfully moved the unwanted fields using the unset( $user_contact['twitter'] ); function.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘How to use the function wp_get_user_contact_methods() has a filter.’ is closed to new replies.