Home › Forums › Backend Issues (wp-admin) › Field not submitted in $_POST
Hi support,
I’ve created a custom acf_field to create an autocomplete list.
This field create a multiple select for the acf.
Unfortunately, when I submit my form, the field is not in the $_POST variable and so the value is not updated.
Here’s my custom acf field:
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// check if class already exists
if ( ! class_exists( 'List_Selector' ) ) :
class List_Selector extends \acf_field {
function __construct( $settings ) {
/*
* name (string) Single word, no spaces. Underscores allowed
*/
$this->name = 'list_selector';
/*
* label (string) Multiple words, can include spaces, visible when selecting a field type
*/
$this->label = __( 'List selector', 'piwik' );
/*
* category (string) basic | content | choice | relational | jquery | layout | CUSTOM GROUP NAME
*/
$this->category = 'basic';
$this->defaults = array();
$this->settings = $settings;
// do not delete!
parent::__construct();
}
function render_field_settings( $field ) {
}
function render_field( $field ) {
$id = str_replace(['/', '-'], ['_','_'], $field['id']);
?>
<div class="list_selector" style="overflow: hidden">
<div class="search_engine">
<input id="search_engine_<?php echo esc_attr($id); ?>" type="text" name="search_engine" class="autocomplete" autocomplete="off"/>
<input type="hidden" id="search_engine_id_<?php echo esc_attr($id); ?>" value=""/>
</div>
<div class="button_add">
<input type="button" name="button_add" value=">" id="button_add_<?php echo esc_attr($id); ?>" disabled="disabled"/>
</div>
<div class="selector">
<select name="<?php echo esc_attr( $field['name'] ) ?>[]" multiple="true" id="select_<?php echo esc_attr($id);?>" style="float: left">
<?php foreach ( $field['value'] as $id_elem ):
$id_elem = intval($id_elem);
?>
<option value="<?php echo esc_attr($id_elem) ; ?>" selected="selected"><?php echo esc_html(get_the_title( $id_elem )); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="buttons">
<div><input type="button" id="up_<?php echo esc_attr($id); ?>" name="up" value="Up" class="button button-primary"/></div>
<div><input type="button" id="down_<?php echo esc_attr($id); ?>" name="down" value="Down" class="button button-primary"/></div>
</div>
<script type="text/javascript">
var list_<?php echo esc_js($id);?> = <?php echo json_encode( $field['choices'] );?>;
document.addEventListener("DOMContentLoaded", function(event) {
autocomplete(document.getElementById("search_engine_<?php echo esc_js($id);?>"), list_<?php echo esc_js($id);?>, document.getElementById("search_engine_id_<?php echo esc_js($id);?>"), document.getElementById("button_add_<?php echo esc_js($id);?>"), document.getElementById("select_<?php echo esc_js($id);?>"), document.getElementById("up_<?php echo esc_js($id);?>"), document.getElementById("down_<?php echo esc_js($id);?>"));
});
</script>
</div>
<?php
}
function input_admin_enqueue_scripts() {
// vars
$url = $this->settings['url'];
$version = $this->settings['version'];
// register & include JS
wp_register_script( 'piwik_autocomplete', "{$url}assets/js/autocomplete.js", array( 'acf-input' ), $version );
wp_enqueue_script( 'piwik_autocomplete' );
// register & include CSS
wp_register_style( 'piwik_autocomplete', "{$url}assets/css/autocomplete.css", array( 'acf-input' ), $version );
wp_enqueue_style( 'piwik_autocomplete' );
}
function load_value( $value, $post_id, $field ) {
if ( acf_is_empty( $value ) ) {
return array();
}
return acf_array( $value );
}
function update_value( $value, $post_id, $field ) {
// Bail early if no value.
if ( empty( $value ) ) {
return $value;
}
// Format array of values.
// - Parse each value as string for SQL LIKE queries.
if ( is_array( $value ) ) {
$value = array_map( 'strval', $value );
}
// return
return $value;
}
}
// initialize
new List_Selector( $this->settings );
// class_exists check
endif;
Can you help me?
Kind regards
Mat
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.