【问题标题】:PHP Set Checkbox to Checked by Default WordpressPHP 将复选框设置为默认选中 Wordpress
【发布时间】:2017-08-10 11:50:20
【问题描述】:

我检查了一些其他主题,但没有一个适合我在页面加载时将复选框设置为选中的情景。

复选框与数据库无关,它只是让代码查看它是否被选中。我知道如何在 HTML 和 PHP 中执行此操作,但我有一个 Wordpress 插件,它有一个复选框,我需要在页面加载时选中它。

以下是显示复选框的代码,但我不知道要添加什么以使其在页面加载时将复选框设置为选中:

$is_offers_enabled = get_post_meta($object_id, 'offers_for_woocommerce_enabled', true);
$is_offers_enabled = ($is_offers_enabled == 'yes') ? 'yes' : 'no';
     WCVendors_Pro_Form_Helper::input( apply_filters( 'ofwcv_offers_for_woocommerce_enabled', array( 
                    'id'        => 'offers_for_woocommerce_enabled',
                    'label'     => __( 'Enable Offers?', 'offers-for-woocommerce-wc-vendors' ), 
                    'type'      => 'checkbox',
                    'value'     => $is_offers_enabled,
                    'class'     => 'ofwcv_offers_for_woocommerce_enabled',
                    'wrapper_start' => '<div class="wcv-cols-group wcv-horizontal-gutters"><div class="all-50 small-100">', 
                    'wrapper_end'   =>  '</div>'
                    ) )
                );

【问题讨论】:

  • 你不能把 'checked' => 'checked' 添加到那个数组吗?

标签: php wordpress


【解决方案1】:

您应该能够将'checked =&gt; 'checked' 添加到您在问题中发布的属性列表中。

WCVendors_Pro_Form_Helper::input( apply_filters( 'ofwcv_offers_for_woocommerce_enabled', array( 
    'id'        => 'offers_for_woocommerce_enabled', 
    'label'     => __( 'Enable Offers?', 'offers-for-woocommerce-wc-vendors' ), 
    'type'      => 'checkbox',
    'value'     => $is_offers_enabled,
    'class'     => 'ofwcv_offers_for_woocommerce_enabled',
    'wrapper_start' => '<div class="wcv-cols-group wcv-horizontal-gutters"><div class="all-50 small-100">', 
    'wrapper_end'   =>  '</div>',
    custom_attributes(
                  array('checked' => 'checked')
     )
) )

【讨论】:

  • 没用。如果我更改它显示的标签文本,我知道这是显示复选框的正确区域。
  • @Stretcher25 也许它对属性的顺序很挑剔,需要在包装器之前列出它?
  • @Stretcher25 你也可以这样做'checked'=&gt;true
  • @Stretcher25 好的,我找不到关于该类的任何好的文档,但我发现了一些关于自定义属性的信息。也许这是一个可行的解决方法:'custom_attributes' =&gt; array( 'checked'=&gt;'checked')
  • 成功了!如何将此复选框设为只读?谢谢
猜你喜欢
  • 2017-03-27
  • 2021-04-26
  • 2016-09-19
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多