Support

Account

Home Forums Add-ons Repeater Field Associating Admin Defined Repeaters to user meta

Solved

Associating Admin Defined Repeaters to user meta

  • Greetings ACF Pro Experts,

    I hope to be able to explain this clearly to make understood what I’m trying to accomplish. This is very specific and probably considered complex-ish. I’m happy to pay a bonus to someone that might know how to do this, everyone’s time is worth something.

    I have a premium theme that I’m trying to update to many recent WP changes and convert to a plugin. I’m looking to use ACF Pro to manage its Options Pages and options however I fear that ACF may not be able to do what I want without building many new custom fields for using it.

    Crucial Item #1

    A crucial feature is allowing the admin to define special custom affiliate field that users can save their information in and we can use that later to generate links. Currently we have our own custom options page and we build a table in wp to manage these. I’m hoping to be able to replace this by using the Repeater Field.

    Admin creates this field (logo, name, link, user_info). My first goal is to be able to create a shortcode that will display the form on the frontend.

    [my_custom_form id=’1′] Now the repeater field allows the admin to rearrange this order. Is it possible to disable that feature or is it possible to identify a specific row so I can associate user_meta to this 1 repeater option.

    eg: Admin has 3 Affiliate Repeater Fields. Bronze, Silver, Gold. But he has arranged them as Gold, Silver, Bronze. Is it possible for me to retrieve Bronze specifically.

    I don’t know of a way to REQUIRE that the admin define unique IDs to the repeater name for example so I can call it that way [my_custom_form id=’unique_id’]. And I don’t know of a way to prevent the admin from changing the order, or showing the admin the unique ID of each repeater field.

    Crucial Item #2

    After I have verified that when the admin uses [my_custom_form id=’1′] to call a specific row from the repeater field, when I output the aforementioned user_info field. If a user saves their affiliate username here, can I associate that to the user_meta. I know there is an update_field option but this field_key would be in the wp_options table. If I pass the user_id to the update_field option, if that key doesn’t exist would it work.

    Sorry for the big post I hope its clear, I love ACF to death and would be thrilled to build it as a core background for my plugin.

  • 1) To obtain a specific row by the value of its subfield, you have to iterate through all rows.

    function retrieve_row( $fieldNameOrArray, $subfieldName, $searchValue, $post_id = false ) {
    	
    	if ( is_array( $fieldNameOrArray ) )
    		$field =& $fieldNameOrArray;
    	else
    		$field = get_field( $fieldName, $post_id );
    	
    	if ( !$field || !is_array( $field ) || !isset( $field[0][ $subfieldName ] ) )
    		return false;
    	
    	foreach ( $field as $index => $row ) {
    		if ( $row[ $subfieldName ] == $searchValue )
    			return $row;
    	}
    	
    	return false;
    	
    }
    
    // Example
    $row = retrieve_row( 'repeaterFieldName', 'uniqueId', 'gold' );
    // Or if you already have the field loaded into a variable
    $row = retrieve_row( $repeaterFieldArray, 'uniqueId', 'gold' );

    To enforce unique values in a particular subfield, you need to place a script on the repeater field’s instructions:

    <script>
    jQuery( function() {
    	
    	// Definitions
    	
    	var repeaterFieldName = 'repeater',
    		uniqueFieldName = 'unique_id',
    		$repeaterFieldEl = jQuery( '.field.field_type-repeater[data-field_name="' + repeaterFieldName + '"]' );
    		
    	// Validation
    		
    	jQuery(document).on( 'acf/validate_field', function( event, $el ) {
    		
    		var fieldName = $el.data('field_name'),
    			fieldValue,
    			$uniqueFieldList,
    			$uniqueInputs;
    			
    		if ( fieldName != uniqueFieldName )
    			// Skip fields other than the unique one
    			return;
    		
    		fieldValue = $el.find('input[type=text], input[type=number]').val();
    		
    		if ( !fieldValue )
    			return;
    		
    		$uniqueFieldList = $repeaterFieldEl.find( '.row > .sub_field[data-field_name="' + uniqueFieldName + '"]' );
    		$uniqueInputs = $uniqueFieldList.find('input[type=text], input[type=number]');
    		
    		// Compare against other unique fields
    		$uniqueInputs.each( function( n, currentEl ) {
    			if ( $uniqueFieldList[n] === $el[0] ) {
    				// Skip comparing against self
    				return;
    			}
    			if ( currentEl.value == fieldValue ) {
    				// Not unique
    				$el
    					.data( 'validation', false )
    					.data( 'validation_message', 'This field must have a unique value.' );
    			}
    		});
    		
    	});
    	
    });
    </script>

    Just set the repeaterFieldName and uniqueFieldName at the beginning to something appropriate, and make sure that your unique field is required, or the validation will not run.

    2) Perhaps you should create another ACF group with those same fields and associate it to users. Now the fields will exist in the user meta. When you use update_field(), pass the field name, not the key, as the key is going to be different.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Associating Admin Defined Repeaters to user meta’ is closed to new replies.