Support » Plugin: Co-Authors Plus » How to utilize the coauthors_guest_author_fields filter?

  • Resolved sproutchris

    (@sproutchris)


    I’m looking to customize the guest authors profile fields and I see in that there is a filter named “coauthors_guest_author_fields” of the get_guest_author_fields() method of the CoAuthors_Guest_Authors class in the /php/class-coauthors-guest-authors.php file. Can I use this to customize the fields that I want saved and displayed in my site?

    If I try:

    php
    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',
    		'required' => false,
    		// 'sanitize_function' => 'wp_filter_post_kses',
    	);
    	return $fields_to_return;
    }
    

    It creates an identical field in every group, and the only one that will save is the field in the last group of fields.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Niels Lange

    (@nielslange)

    Hello @sproutchris 👋

    I looked into this case and it seems there’s a bug which is causing this problem. Your code is correct and the phone number field should only appear in the contact info section.

    I created a ticket for this problem on https://github.com/Automattic/Co-Authors-Plus/issues/759 and will look into it the next days.

    backbone

    (@backbone)

    You’re missing the $groups argument. https://gist.github.com/danielbachhuber/ece7e8bb1a97ac4a1977
    Follow the example in the link above. The below code is an expanded function to remove contact author fields, add additional contact author fields social media, and a phone field.

    
    add_filter( 'coauthors_guest_author_fields', 'edit_coauthors_guest_author_fields', 10, 2 );
    
    function edit_coauthors_guest_author_fields( $fields_to_return, $groups ) {
    
      // Unset contact fields in Guest Author (bonus, not part of the original question)
      $remove = array( 'aim', 'yahooim', 'jabber' );
      foreach ( $fields_to_return as $k => $value ) {
        if ( $value['key'] == in_array( $value['key'], $remove ) ) {
          unset( $fields_to_return[ $k ] );
        }
      }
    
      // Add social fields to contact-info group (bonus, not part of the original question)
      $global_fields = array();
      $socials = array( 'Twitter', 'LinkedIn', 'Instagram', 'YouTube', 'Facebook' );
      foreach ( $socials as $social ) {
        $global_fields[] = array(
          'key'   => preg_replace( ' ', '_', strtolower( $social ) ),
          'label' => $social,
          'group' => 'contact-info',
          'input' => 'url',
        );
      }
    
      // Add phone field contact-info group
      $phone_number = 'Phone Number';
      $global_fields[] = array(
        'key'   => preg_replace( ' ', '_', strtolower( $phone_number ) ),
        'label' => $phone_number,
        'group' => 'contact-info',
        'input' => 'number', //number only or delete to use group's text type
      );
    
      // Putting it all together
      $fields_to_return = array();
      foreach ( $global_fields as $single_field ) {
        if ( in_array( $single_field['group'], $groups ) || 'all' === $groups[0] && 'hidden' !== $single_field['group'] ) {
          $fields_to_return[] = $single_field;
        }
      }
    }
    

    The function get_guest_author_fields( $groups = ‘all’ ) is found in class-coauthors-guest-authors.php

    • This reply was modified 6 months ago by backbone. Reason: Clarification of the additonal functions beyond the orginal question
    Plugin Support Niels Lange

    (@nielslange)

    Nice catch, @backbone! 🙌

    @sproutchris , based on @backbone’s comment, I went ahead and created the following snippet, which adds the phone number to the contact info field 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;
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to utilize the coauthors_guest_author_fields filter?’ is closed to new replies.