Automattic / Co-Authors-Plus

Multiple bylines and Guest Authors for WordPress
https://wordpress.org/plugins/co-authors-plus/
GNU General Public License v2.0
291 stars 204 forks source link

Filter coauthors_guest_author_fields leads to duplicate fields #759

Closed nielslange closed 3 years ago

nielslange commented 3 years ago

Reported in https://wordpress.org/support/topic/how-to-utilize-the-coauthors_guest_author_fields-filter/

Steps to reproduce

  1. Add the following code to your site:

    add_filter( 'coauthors_guest_author_fields', 'edit_coauthors_guest_author_fields', 10, 1 );
    function edit_coauthors_guest_author_fields( $fields_to_return ) {
    $fields_to_return[] = array(
    'key' => 'name',
    'label' => 'Phone number',
    'group' => 'contact-info',
    );    
    
    return $fields_to_return;
    }
  2. Go to /wp-admin/post-new.php?post_type=guest-author
  3. See that the field Phone number appears in all sections

Current behaviour

The field Phone number appears in all sections, Name, Contact Info and About the guest author.

Expected behaviour

The field Phone number appears only in the section Contact Info.

Screenshots

Current behaviour:

![current](https://user-images.githubusercontent.com/3323310/112571384-7baa8a00-8e1a-11eb-835a-fbeafea7d721.png)
Expected behaviour:

![expected](https://user-images.githubusercontent.com/3323310/112571387-7d744d80-8e1a-11eb-8049-3ca51820321d.png)
nielslange commented 3 years ago

This problem was caused due to the missing $groups parameter in the functions call. The correct function looks like this and adds the phone number to the contact info section as expected:

add_filter( 'coauthors_guest_author_fields', 'edit_coauthors_guest_author_fields', 10, 2 );
function edit_coauthors_guest_author_fields( $fields_to_return, $groups ) {
  if ( in_array( 'all', $groups ) || in_array( 'contact-info', $groups ) ) {
    $fields_to_return[] = array(
      'key' => 'name',
      'label' => 'Phone number',
      'group' => 'contact-info',
    );
  }

  return $fields_to_return;
}