• Resolved a4jp

    (@a4jpcom)


    I want to use WPForms on a clients website but can you add CSS page IDs to the dynamic list of checkboxes first? Paid version as well as free if the checkboxes are included. It is a simple update and would reduce the amount of coding that needs to be done on my side when making websites.

    I see on many sites using WPForms they only have a relative position. For example,

    <div id="wpforms-777-field_24-container" class="wpforms-field wpforms-field-checkbox wpforms-list-inline" data-field-id="24">
    
    <label class="wpforms-field-label" for="wpforms-777-field_24">Catagory: </label>
    
    <ul id="wpforms-777-field_24">
    
    <li class="choice-0 depth-1">
    <input type="checkbox" id="wpforms-777-field_24_0" name="wpforms[fields][24][]" value="272">
    <label class="wpforms-field-label-inline" for="wpforms-777-field_24_0"> October</label>
    </li>
    
    <li class="choice-1 depth-1">
    <input type="checkbox" id="wpforms-777-field_24_1" name="wpforms[fields][24][]" value="273">
    <label class="wpforms-field-label-inline" for="wpforms-777-field_24_1"> Oh no</label>
    </li>
    
    <li class="choice-2 depth-1">
    <input type="checkbox" id="wpforms-777-field_24_2" name="wpforms[fields][24][]" value="274">
    <label class="wpforms-field-label-inline" for="wpforms-777-field_24_2"> Orange</label>
    </li>

    If I want to use CSS to highlight a label it’s gonna move each time if it is just connected to the position.

    Can you add the page ID to the LI so we can easily change setting without having to constantly update CSS selectors please?

    For example: <li class=”choice-2 depth-1″> -> <li class=”choice-2 depth-1 tag-link-265″>

    Just now, every time we add a new category a dynamic list it moves items and the CSS selector has to be updated almost everything every time depending on the position. If you have the ID as part of the referenceable code for each checkbox then this would never be a problem.

    You only need to add code like this <?php echo get_the_ID(); ?> the the CSS class. Or some thing like this <?php echo "tag-link-" . get_the_ID(); ?>

Viewing 13 replies - 16 through 28 (of 28 total)
  • Plugin Support Kenneth Macharia

    (@kmacharia)

    Hi @a4jpcom

    To follow-up on your newer thread, our developers shared the following snippet that you can use to achieve this requirement:

    /**
     * Add custom CSS classnames to dynamic choices.
     *
     * @param array $properties Properties for rendering the field on the frontend.
     * @param array $field      Original field data.
     * @param array $form_data  Form data.
     *
     * @return array
     */
    function wpforms_add_dynamic_choices_ids( $properties, $field, $form_data ) {
    
    	// Exit early if Dynamic Choices is not enabled.
    	if ( empty( $field['dynamic_choices'] ) ) {
    		return $properties;
    	}
    
    	// Get the prefix (post, page, category, post_tag, your_custom_post_type, your_custom_taxonomy).
    	$prefix = $field[ 'dynamic_' . $field['dynamic_choices'] ];
    
    	// Loop through all choices.
    	foreach ( $properties['inputs'] as $index => $input ) {
    		// Combine dynamic prefix with the real item (post, category etc) ID.
    		$class_name = $prefix . '-' . $input['attr']['value'];
    		// Add to list of choice item container classes.
    		$properties['inputs'][ $index ]['container']['class'][] = $class_name;
    	}
    
    	return $properties;
    }
    add_filter( 'wpforms_field_properties_checkbox', 'wpforms_add_dynamic_choices_ids', 10, 3 );
    add_filter( 'wpforms_field_properties_radio', 'wpforms_add_dynamic_choices_ids', 10, 3 );
    

    I hope this helps! We appreciate your patience while we were checking this issue.

    Thread Starter a4jp

    (@a4jpcom)

    How do we sort the dynamic checkboxes by slug/link name? The one we type. The JIS order for Japanese characters is terrible. Please update the options. Please also add classes for proper referencing.

    I tried the code below but couldn’t figure out how to change the sort of dynamic checkboxes. T-T

    function wpforms_sort_dynamic_choices_by_slug( $properties, $field, $form_data ) {

    // Check if form ID is either 777 or 888
    if ( ! in_array( $form_data[‘id’], [ 777, 888] ) ) {
    return $properties;
    }

    // Exit early if Dynamic Choices is not enabled.
    if ( empty( $field[‘dynamic_choices’] ) ) {
    return $properties;
    }

    // Sort the choices based on the slug name
    usort( $properties[‘inputs’], function( $a, $b ) {
    return strcmp( $a[‘label’], $b[‘label’] );
    } );

    return $properties;
    }
    add_filter( ‘wpforms_field_properties_checkbox’, ‘wpforms_sort_dynamic_choices_by_slug’, 10, 3 );
    add_filter( ‘wpforms_field_properties_radio’, ‘wpforms_sort_dynamic_choices_by_slug’, 10, 3 );

    • This reply was modified 1 year, 8 months ago by a4jp.
    • This reply was modified 1 year, 8 months ago by a4jp.
    Plugin Support Kenneth Macharia

    (@kmacharia)

    Hi @a4jpcom,

    Sorry, we don’t currently have a way to sort results on the field based on user input. This feature would need to be custom-built but I would encourage you to take a look at the wpforms_dynamic_choice_post_type_args filter that may be able to help in sorting all the results in the dynamic choices field.

    I hope this helps. Thanks.

    Plugin Support Kenneth Macharia

    (@kmacharia)

    Hey @a4jpcom

    We haven’t heard back from you in about a few days, so I’m going to go ahead and close this thread for now. But if you’d like us to assist further, please feel welcome to continue the conversation (please just see my post above).

    Thanks!

    Thread Starter a4jp

    (@a4jpcom)

    I sent in code to fix major problems with this plugin. I just can’t figure out why you don’t want to fix the plugin. Please don’t close problems that aren’t solved.

    Plugin Support Kenneth Macharia

    (@kmacharia)

    Hi @a4jpcom

    I’m sorry for any confusion here. It looks like we might not be on the same page. As far as the original question goes, you were looking to add page IDs to dynamic checkboxes and we went ahead and shared a code snippet for this. Could you please let us know if this snippet did not work for you?

    The sorting of these values appears to be a different question, could you please create a new thread so that we can easily track both issues?

    Thank you!

    Plugin Support Kenneth Macharia

    (@kmacharia)

    Hey @a4jpcom

    I’m sorry to reach out again. Could you please confirm if the code worked and if it would be okay to close this thread as resolved?

    Plugin Support Kenneth Macharia

    (@kmacharia)

    Hey @a4jpcom,

    I’ve not heard back from you in 5 days and so I’m going to close this ticket. Feel free to open a new conversation if you need additional help.

    Thanks!

    Thread Starter a4jp

    (@a4jpcom)

    No. I still have the problem.

    Plugin Support Kenneth Macharia

    (@kmacharia)

    Hi @a4jpcom

    I’m sorry to hear that this code did not work for you. Did you make any other changes to the code? Here is a screenshot from a fresh testing site using the code I shared earlier. The page IDs are appended to the list items when dynamic choices are enabled.

    Please let me know if I’m missing anything here. And in case the issue has to do with sorting items, could you please create a new thread so that it might help other users who might land on this post?

    Thanks!

    Plugin Support Kenneth Macharia

    (@kmacharia)

    As a follow-up, you could also use this code to sort the pages in ASC order:

    /**
     * Sort pages using the ID
     *
     * @link https://wpforms.com/developers/wpforms_dynamic_choice_post_type_args
     *
     * @param array $args       Arguments from post submission.     
     * @param array $field      Sanitized field data. 
     * @param int   $form_id    Form ID
     *
     * @return $args
     */
     
     function wpf_dev_dynamic_choices_categories( $args, $field, $form_id ) {
         
    	// Only on form #789 and field #10
    	if ( $form_id == 303 && $field[ 'id' ] == 3 ) {
    
    		$args[ 'orderby' ] = 'ID';
    		$args[ 'order' ] = 'ASC';        
    	
    	}
    	
    	return $args;
    }
    
    add_filter( 'wpforms_dynamic_choice_post_type_args', 'wpf_dev_dynamic_choices_categories', 10, 3 );
    
    

    I hope this helps!

    Plugin Support Kenneth Macharia

    (@kmacharia)

    Hi @a4jpcom,

    I would like to check in and find out if the code worked. Would you mind sharing an update when you have a chance?

    I will have to mark this thread as resolved if we don’t hear back soon!

    Thanks!

    Plugin Support Ralden Souza

    (@rsouzaam)

    Hi @a4jpcom,

    We haven’t heard back from you in about a few months, so I’m going to go ahead and close this thread for now. But if you’d like us to assist further, please feel welcome to continue the conversation.

    Thanks!

Viewing 13 replies - 16 through 28 (of 28 total)
  • The topic ‘Can you add CSS page IDs to the dynamic checkboxes please?’ is closed to new replies.