要与ACF兼容,您应该首先了解ACF是如何将复选框值保存到数据库的。正如我现在所说,这是序列化数组。当我们知道必须以同样的方式保存前端复选框时。好消息是您只能将数组传递给update_post_meta
WordPress将负责序列化。
代码的问题是,可能没有传递复选框值数组。您应该调查此部分:esc_attr(strip_tags($_POST[\'customMetaAppliances[]\']))
.
有两件事困扰着我:
esc_attr
和strip_tags
函数需要字符串作为参数,而不是数组。它们可能会返回意外结果。如果要从post请求中获取复选框数组,则应使用不带方括号的字段名customMetaAppliances
我认为您可以用代码替换保存部分:
$customMetaAppliances = filter_input( INPUT_POST, \'customMetaAppliances\', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
update_field( \'customMetaAppliances\', $customMetaAppliances, $post_id );
个人安装ACF后,我使用
update_field
而不是
update_post_meta
.
我创建了完全工作类,它将在发布内容后在前端显示表单,并在提交时将值保存到发布ACF字段。
class WPSE_287946_Form {
/**
* Class constructor
*/
public function __construct() {
$this->define_hooks();
}
public function save_form() {
if( isset( $_POST[\'save\'] ) ) { // Submit button
$post_id = filter_input( INPUT_POST, \'id\', FILTER_SANITIZE_NUMBER_INT );
$name = filter_input( INPUT_POST, \'name\', FILTER_SANITIZE_STRING );
$color = filter_input( INPUT_POST, \'color\', FILTER_SANITIZE_STRING );
$accessories = filter_input( INPUT_POST, \'accessories\', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
update_field( \'name\', $name, $post_id );
update_field( \'color\', $color, $post_id );
update_field( \'accessories\', $accessories, $post_id );
// Redirect user because POST request on single post page will trigger 404 page.
wp_redirect( get_permalink( $post_id ) );
}
}
/**
* Display form
*/
public function display_form( $content ) {
$name = get_field( \'name\' );
$color = get_field( \'color\' );
$accessories = get_field( \'accessories\' );
ob_start();
?>
<form method="post">
<p>
<?php $this->display_text( \'name\', \'Name\', $name ); ?>
</p>
<p>
<?php $this->display_select( \'color\', \'Color\', $this->get_available_colors(), $color ); ?>
</p>
<p>
<?php $this->display_checkboxes( \'accessories\', \'Accessories\', $this->get_available_accessories(), $accessories ); ?>
</p>
<p>
<input type="hidden" name="id" value="<?php esc_attr_e( get_the_ID() ) ?>">
<input type="submit" name="save" value="Submit" />
</p>
</form>
<?php
$output = ob_get_contents();
ob_end_clean();
return $content . $output;
}
/**
* Display text field
*/
private function display_text( $name, $label, $value = \'\' ) {
?>
<label><?php esc_html_e( $label, \'wpse_287946\' ) ?></label>
<input type="text" name="<?php esc_attr_e( $name ) ?>" value="<?php esc_attr_e( $value ); ?>">
<?php
}
/**
* Display select field
*/
private function display_select( $name, $label, $options, $value = \'\' ) {
?>
<label><?php esc_html_e( $label, \'wpse_287946\' ) ?></label>
<select name="<?php esc_attr_e( $name ) ?>">
<?php $this->display_options( $options, $value ); ?>
</select>
<?php
}
/**
* Display options
*/
private function display_options( $options, $value ) {
foreach( $options as $option_value => $option_label ):
$selected = ( $option_value === $value ) ? \' selected\' : \'\';
?>
<option value="<?php esc_attr_e( $option_value ) ?>"<?php esc_attr_e( $selected ) ?>><?php esc_html_e( $option_label, \'wpse_287946\' ) ?></option>
<?php
endforeach;
}
/**
* Display checkboxes field
*/
private function display_checkboxes( $name, $label, $options, $values = array() ) {
$name .= \'[]\';
?>
<label><?php esc_html_e( $label, \'wpse_287946\' ) ?></label>
<?php
foreach ( $options as $option_value => $option_label ):
$this->display_checkbox( $name, $option_label, $option_value, $values );
endforeach;
}
/**
* Display single checkbox field
*/
private function display_checkbox( $name, $label, $available_value, $values = array() ) {
$checked = ( in_array($available_value, $values) ) ? \' checked\' : \'\';
?>
<label>
<input type="checkbox" name="<?php esc_attr_e( $name ) ?>" value="<?php esc_attr_e( $available_value ) ?>"<?php esc_attr_e( $checked ) ?>>
<?php esc_html_e( $label, \'wpse_287946\' ) ?>
</label>
<?php
}
/**
* Get available colors
*/
private function get_available_colors() {
return array(
\'red\' => \'Red\',
\'blue\' => \'Blue\',
\'green\' => \'Green\',
);
}
/**
* Get available accessories
*/
private function get_available_accessories() {
return array(
\'case\' => \'Case\',
\'tempered_glass\' => \'Tempered glass\',
\'headphones\' => \'Headphones\',
);
}
/**
* Define hooks related to plugin
*/
private function define_hooks() {
/**
* Add action to save form
*/
add_action( \'wp\', array( $this, \'save_form\' ) );
/**
* Add filter to display form
*/
add_filter( \'the_content\', array( $this, \'display_form\' ) );
}
}
new WPSE_287946_Form();