Created
March 6, 2024 14:19
-
-
Save PluginRepublicSupport/1b99fde18da2bdb7ede0c298a30789ef to your computer and use it in GitHub Desktop.
Add a range parameter to add-on field settings if the field type is "number"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add a custom checkbox parameter to number field settings | |
*/ | |
function pew_custom_checkbox_param( $group_key, $item_key, $item, $post_id ) { | |
$field_type = isset( $item['field_type'] ) ? $item['field_type'] : ''; | |
if ( 'number' === $field_type ) { | |
$enable_range_slider = isset( $item['enable_range_slider'] ) ? esc_html( $item['enable_range_slider'] ) : ''; | |
$checked = ! empty( $item['enable_range_slider'] ); | |
$input_attributes = array( | |
'type' => 'checkbox', | |
'class' => 'pewc-field-range-slider', | |
'name' => sprintf( '_product_extra_groups_%s_%s[enable_range_slider]', esc_attr( $group_key ), esc_attr( $item_key ) ), | |
'value' => 1, | |
); | |
error_log( print_r( $item, true ) ); | |
$input_html = sprintf( | |
'<input type="%s" class="%s" name="%s" value="%s" %s>', | |
'checkbox', | |
'pewc-field-range-slider', | |
sprintf( '_product_extra_groups_%s_%s[enable_range_slider]', esc_attr( $group_key ), esc_attr( $item_key ) ), | |
1, | |
checked( $checked, 1, false ) | |
); | |
printf( | |
'<div class="pewc-fields-wrapper pewc-my-new-fields"> | |
<div class="product-extra-field-third"> | |
%s | |
<label class="pewc-checkbox-field-label">Enable range slider</label> | |
</div> | |
</div>', | |
$input_html | |
); | |
} | |
} | |
add_action( 'pewc_end_product_extra_field', 'pew_custom_checkbox_param', 10, 4 ); | |
/** | |
* Add the custom param to field data | |
*/ | |
function prefix_item_params( $params, $field_id ) { | |
$params[] = 'enable_range_slider'; | |
return $params; | |
} | |
add_filter( 'pewc_item_params', 'prefix_item_params', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment