如何在PHP中为某些选项设置默认全部选中

时间:2015-12-11 03:08:37

标签: php html plugins

我在WP网站上使用插件付费会员订阅

我创建了一些订阅计划,如下图所示: enter image description here

我想为所有计划设置默认选项,如下图所示: enter image description here

我应该添加以下脚本的代码:

<div class="pms-meta-box-field-wrapper">
    <label class="pms-meta-box-field-label"><?php _e( 'Subscription Plans', 'paid-member-subscriptions' ); ?></label>

    <?php
    $subscription_plans = pms_get_subscription_plans();
    $selected_subscription_plans = get_post_meta( $post->ID, 'pms-content-restrict-subscription-plan' );
    ?>

    <?php if( !empty( $subscription_plans ) ): foreach( $subscription_plans as $subscription_plan ): ?>

        <label class="pms-meta-box-checkbox-label" for="pms-content-restrict-subscription-plan-<?php echo $subscription_plan->id ?>">
            <input type="checkbox" value="<?php echo $subscription_plan->id; ?>" <?php if( in_array( $subscription_plan->id, $selected_subscription_plans ) ) echo 'checked="checked"'; ?> name="pms-content-restrict-subscription-plan[]" id="pms-content-restrict-subscription-plan-<?php echo $subscription_plan->id ?>">
            <?php echo $subscription_plan->name; ?>
        </label>

    <?php endforeach; ?>
        <p class="description"><?php echo __( 'Members subscribed to these Subscription plans will be able to view this page.', 'paid-member-subscriptions' ); ?></p>
    <?php else: ?>
        <p class="description"><?php printf( __( 'You do not have any active Subscription Plans yet. Please create them <a href="%s">here</a>', 'paid-member-subscriptions' ), admin_url( 'edit.php?post_type=pms-subscription' ) ); ?></p>
    <?php endif; ?>

</div>

(source: .../wp-content/plugins/paid-member-subscriptions/includes/admin/meta-boxes/views/view-meta-box-single-content-restriction.php)

2 个答案:

答案 0 :(得分:1)

在输入标记中添加已选中

<input type="checkbox" checked>

答案 1 :(得分:1)

只需更改以下行

即可
<input type="checkbox" value="<?php echo $subscription_plan->id; ?>" <?php if( in_array( $subscription_plan->id, $selected_subscription_plans ) ) echo 'checked="checked"'; ?> name="pms-content-restrict-subscription-plan[]" id="pms-content-restrict-subscription-plan-<?php echo $subscription_plan->id ?>">

到这个

<input type="checkbox" value="<?php echo $subscription_plan->id; ?>" checked name="pms-content-restrict-subscription-plan[]" id="pms-content-restrict-subscription-plan-<?php echo $subscription_plan->id ?>">

整个if子句已被替换为单词checked