wordpress - 使用 Wordpress 插件保存数据时出现问题
问题描述
我使用Woo 预计发货日期插件在我的产品页面中添加自定义文本(字段)。问题是:
示例:如果我在该自定义字段中使用此文本:“有货”,然后保存更改,插件会在产品页面中显示“有货”......但如果我返回编辑产品页面上的任何其他内容,插件恢复默认句子“预计交货日期”,如果我保存更改而不再次修改该句子,则产品页面中会显示“预计交货日期”而不是新句子。它有点像一个烦人的循环,在保存更改后总是返回默认文本,如果我不看那个字段,我的自定义文本就消失了。
这是插件代码:
<?php
/**
* No cheating please
*/
if ( ! defined( 'WPINC' ) ) exit;
/**
* WCESD_Product_Settings Class
*/
class WCESD_Product_Settings {
/**
* Hold the instance
*
* @var string
*/
private static $instance;
use helperMethods;
/**
* Constructor method
*
* @return void
*/
public function __construct() {
if ( ! $this->enabled() ) {
return;
}
$this->init_hooks();
}
/**
* Init all the hooks
*
* @return void
*/
protected function init_hooks() {
add_action( 'woocommerce_product_options_shipping', array( $this, 'wc_esd_add_estimated_shipping_date' ) );
add_action( 'woocommerce_process_product_meta', array( $this, 'wc_esd_save_shipping_date') );
}
/**
* Add wcesd form
*
* @return void
*/
public function wc_esd_add_estimated_shipping_date() {
woocommerce_wp_checkbox( array(
'id' => 'wc_esd_date_enable',
'label' => __( 'Habilitar fecha estimada entrega', 'wcesd' ),
'description' => __( 'Enable or Disable woocommerce estimated shipping date', 'wcesd' ),
'desc_tip' => true,
) );
woocommerce_wp_text_input( array(
'id' => 'wc_esd_date',
'label' => __( 'Fecha estimada en días', 'wcesd' ),
'description' => __( 'Días del posible arribo del producto', 'wcesd' ),
'desc_tip' => true,
'type' => 'number',
'placeholder' => 5,
'value' => 5,
) );
woocommerce_wp_text_input( array(
'id' => 'wc_esd_date_message',
'label' => __( 'Frase', 'wcesd' ),
'description' => __( 'Agregue su mensaje', 'wcesd' ),
'desc_tip' => true,
'placeholder' => 'Estimated Delivery Date',
'value' => 'Estimated Delivery Date',
) );
do_action( 'wc_esd_add_estimated_shipping_date' );
}
/**
* Save wcesd form data
*
* @param int $product_id
*
* @return void
*/
public function wc_esd_save_shipping_date( $product_id ) {
if ( ! is_admin() || get_post_type() !== 'product' ) {
return;
}
$wc_esd_date_enable = isset( $_POST['wc_esd_date_enable'] ) ? sanitize_text_field( $_POST['wc_esd_date_enable'] ) : '';
$wc_esd_date = isset( $_POST['wc_esd_date'] ) ? sanitize_text_field( $_POST['wc_esd_date'] ) : '';
$wc_esd_date_message = isset( $_POST['wc_esd_date_message'] ) ? sanitize_text_field( $_POST['wc_esd_date_message'] ) : '';
update_post_meta( $product_id, 'wc_esd_date_enable', $wc_esd_date_enable );
update_post_meta( $product_id, 'wc_esd_date', $wc_esd_date );
update_post_meta( $product_id, 'wc_esd_date_message', $wc_esd_date_message );
do_action( 'wc_esd_save_shipping_date', $product_id );
}
/**
* Get instance
*
* @return object
*/
public static function init() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Disable cloning this class
*
* @return void
*/
private function __clone() {
//
}
private function __wakeup() {
//
}
}
WCESD_Product_Settings::init();
知道如何解决这个问题吗?谢谢 :)
解决方案
请删除值字段
woocommerce_wp_text_input( array(
'id' => 'wc_esd_date_message',
'label' => __( 'Frase', 'wcesd' ),
'description' => __( 'Agregue su mensaje', 'wcesd' ),
'desc_tip' => true,
'placeholder' => 'Estimated Delivery Date',
/*'value' => 'Estimated Delivery Date',*/
) );
然后它应该工作。
推荐阅读
- eclipse - 如何在 Spring 工具套件上运行声纳 lint
- powershell - 如何在此脚本中插入错误操作以静默继续?
- c++ - zsh/sh/bash 如何检查从 c++ 程序启动的命令“cat &”的信号 SIGTTIN
- telegram - 电报即时视图模板
- mysql - 我正在尝试从 pyspark 访问 mysql 表。我正在尝试使用:
- python-3.x - Beautiful Soup Login with Python 3 - 这个表单的 POST url 是什么?
- python - 有没有办法让熊猫从用户选择的 excel 表中构建数据框?
- javascript - 如何将多个文件选择放入一个数组中,然后输出到 HTML
- javascript - 运行应用程序时出现 Sequelize index has been exist 错误
- linux - 如何使用 find、ls 命令根据数组中的条目显示文件名?