Build Form on Dashboard

时间:2015-06-12 作者:Irwan

我想在“主题选项”上创建自定义表单

基本上,我已经构建了:

/* == THEME OPTIONS == */

// This tells WordPress to call the function named "setup_theme_admin_menus"
// when it\'s time to create the menu pages.
add_action("admin_menu", "setup_theme_admin_menus");

function setup_theme_admin_menus() {
    add_submenu_page(\'themes.php\', 
        \'Generate Coupons\', \'Generate Coupons\', \'manage_options\', 
        \'generate-coupons-elements\', \'theme_generate_coupons_settings\'); 
}

function theme_generate_coupons_settings() {
?>
    <div class="wrap">
        <?php screen_icon(\'themes\'); ?> <h2>Generate Coupons</h2>

        <form method="POST" action="">
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">
                        <label for="coupon">
                            Number of coupons:
                        </label> 
                    </th>
                    <td>
                        <input type="text" name="coupon" size="25" />
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">
                        <label for="discount">
                            Amount of discount:
                        </label> 
                    </th>
                    <td>
                        <input type="text" name="discount" size="25" />
                    </td>
                </tr>
            </table>
            <p>
                <input type="submit" value="Generate" class="button-primary"/>
            </p>
        </form>
    </div>
<?php
}

/* == END OF THEME OPTIONS == */
在我的仪表板上显示字段表单的代码。

我在WooCommerce文档中看到:

$coupon_code = \'UNIQUECODE\'; // Code
$amount = \'10\'; // Amount
$discount_type = \'fixed_cart\'; // Type: fixed_cart, percent, fixed_product, percent_product

$coupon = array(
    \'post_title\' => $coupon_code,
    \'post_content\' => \'\',
    \'post_status\' => \'publish\',
    \'post_author\' => 1,
    \'post_type\'     => \'shop_coupon\'
);

$new_coupon_id = wp_insert_post( $coupon );

// Add meta
update_post_meta( $new_coupon_id, \'discount_type\', $discount_type );
update_post_meta( $new_coupon_id, \'coupon_amount\', $amount );
update_post_meta( $new_coupon_id, \'individual_use\', \'no\' );
update_post_meta( $new_coupon_id, \'product_ids\', \'\' );
update_post_meta( $new_coupon_id, \'exclude_product_ids\', \'\' );
update_post_meta( $new_coupon_id, \'usage_limit\', \'\' );
update_post_meta( $new_coupon_id, \'expiry_date\', \'\' );
update_post_meta( $new_coupon_id, \'apply_before_tax\', \'yes\' );
update_post_meta( $new_coupon_id, \'free_shipping\', \'no\' );
我的问题是,如何让我的第一个代码运行第二个代码并捕获从该代码发送的变量?我知道第二个代码只生成1张优惠券,但稍后我可以根据表单发送的“优惠券”编号循环它。

我希望你能在这件事上帮助我。

非常感谢。

1 个回复
SO网友:Irwan

经过多次尝试和错误,我终于找到了答案。

如果有人想知道这是如何工作的,我将完整的代码放在这里:

/* == THEME OPTIONS == */

add_action("admin_menu", "setup_theme_admin_menus");

function setup_theme_admin_menus() {
    add_submenu_page(\'themes.php\', 
        \'Generate Coupons\', \'Generate Coupons\', \'manage_options\', 
        \'generate-coupons-elements\', \'theme_generate_coupons_settings\'); 
}

function theme_generate_coupons_settings() {
?>
  <div class="wrap">
    <?php screen_icon(\'themes\'); ?> <h2>Generate Coupons</h2>

    <form method="POST" action="">
      <table class="form-table">
        <tr valign="top">
          <th scope="row">
            <label for="coupon">
              Number of coupons:
            </label> 
          </th>
          <td>
            <input type="text" name="coupon" size="25" />
          </td>
        </tr>
        <tr valign="top">
          <th scope="row">
            <label for="discount">
              Amount of discount:
            </label> 
          </th>
          <td>
            <input type="text" name="discount" size="25" />
          </td>
        </tr>
      </table>
      <p>
        <input type="hidden" name="update_settings" value="Y" />
        <input type="submit" value="Generate" class="button-primary"/>
      </p>
    </form>
  </div>
<?php
  if (isset($_POST["update_settings"])) {
    $coupon = esc_attr($_POST["coupon"]); 
    $discount = esc_attr($_POST["discount"]); 
    $coupon_code = \'UNIQUECODES\'; // Code
    $amount = $discount; // Amount
    $discount_type = \'fixed_cart\'; // Type: fixed_cart, percent, fixed_product, percent_product

    $coupon = array(
      \'post_title\' => $coupon_code,
      \'post_content\' => \'\',
      \'post_status\' => \'publish\',
      \'post_author\' => 1,
      \'post_type\'   => \'shop_coupon\'
    );

    $new_coupon_id = wp_insert_post( $coupon );

    // Add meta
    update_post_meta( $new_coupon_id, \'discount_type\', $discount_type );
    update_post_meta( $new_coupon_id, \'coupon_amount\', $amount );
    update_post_meta( $new_coupon_id, \'individual_use\', \'no\' );
    update_post_meta( $new_coupon_id, \'product_ids\', \'\' );
    update_post_meta( $new_coupon_id, \'exclude_product_ids\', \'\' );
    update_post_meta( $new_coupon_id, \'usage_limit\', \'1\' );
    update_post_meta( $new_coupon_id, \'usage_limit_per_user\', \'1\' );
    update_post_meta( $new_coupon_id, \'expiry_date\', \'\' );
    update_post_meta( $new_coupon_id, \'apply_before_tax\', \'yes\' );
    update_post_meta( $new_coupon_id, \'free_shipping\', \'no\' );
    ?>
      <div id="message" class="updated">Coupon generated!</div>
    <?php
  }
}

/* == END OF THEME OPTIONS == */

结束