• Resolved Kir 2012

    (@kir-2012)


    Hi there,
    I have a multi select form where a user can select from other users of a certain type to display on their post. The form uses a relationship field.

    1) – I can see that it is possible to change in the relationship options how the selections are displayed in the multi-select, and I am using {@user_nicename}; but is it possible to show two options?

    I would like each menu option to be listed as {@user_nicename} – {@type}. So that the menu option could say: BOB’S CAFE – FOOD & DRINK.

    2) – It would also be great to use the icon option, and I see that if you’re using posts then you get the featured image, but for users as in my case, there is an icon. How can I change this icon? I would like to replace the icon with the user avatar instead, is that possible?

    Kind regards






Viewing 1 replies (of 1 total)
  • Plugin Support Paul Clark

    (@pdclark)

    Below is a function which will do what you have described. It can be installed as a plugin by placing it in wp-content/plugins, or can be added to a theme’s functions.php. A Code Snippets plugin can also be used, but that is generally not recommended for many reasons.

    <?php
    /**
     * Plugin Name: User Relationship Display
     * Description: Filter Pods user relationship field by the name <code>user_relationship</code> to display the user display name and field <code>type_field</code>.
     * Version: 1
     */
    add_filter(
    	'pods_field_pick_data',
    	function ( $data, $field_name, $value, $options, $pod, $id ){
    
    		$field_to_filter      = 'user_relationship';  // This is the name of the field which is a user relationship field.
    		$type_user_meta_key   = 'type_field';         // This is the name of the field referred to as "type".
    
    		if ( $field_to_filter === $field_name ) {
    				foreach ( $data as $user_id => & $value ) {
    						if( ! empty( $user_id ) ) {
    								$user = get_user_by( 'id', $user_id );
    								$user_pod = pods( 'user', $user_id );
    								$type = $user_pod->display( $type_user_meta_key );
    
    								// Default to the user display name.
    								$value = $user->display_name;
    
    								// Add the type field after a hyphen if it is not empty.
    								if ( ! empty( $type ) )  {
    									$value = sprintf(
    										'%s - %s',
    										$value,
    										$type
    									);
    								}
    						}
    				}
    		}
    		return $data;
    	},
    	20,
    	6
    );

    The code above is an adaptation of https://docs.pods.io/code-snippets/multiple-fields-pick-list/. It has these differences from the documentation:

    • The name of your user relationship field is assumed to be user_relationship. In the linked example, this was incorrectly noted as pods_field_RELPODNAME. There is no prefix. It is the key of the relationship field.
    • The name of your type field is assumed to be type_field. In this case, type_field was used because when I attempted to create a field named type, Pods said it was not allowed.
    • The value of type is accessed with $user_pod->display( $type_user_meta_key ). This will apply appropriate formatting no matter what datatype you have selected for the field. This is the same as $p->display('RELATIONSHIPFIELD.name') in the linked example —?note how dots can be used to traverse relationships, as $p and $user_pod are an instantiated Pods object.
    • $user_pod = pods( 'user', $user_id ); above is the same as $p = pods('RELPODNAME', $dataid); in the example. The idea is that the first argument, user or RELPODNAME is the name of the Pod post type or object type, in your case user, and $dataid or $user_id is the ID of the object displayed in the relationship field — it could be a post, a user, a file attachment, a taxonomy term, etc.: it will always be the ID of the object.

    Once the file is running, you should only need to change user_relationship to the name of your relationship field and type_field to the name of the field you referred to as type.

Viewing 1 replies (of 1 total)
  • The topic ‘Change icon to user avatar, for user relationship field in select form’ is closed to new replies.