Wordpress插件无法将数据发布到另一个页面并插入到wpdb

时间:2017-08-29 21:08:06

标签: php wordpress mysqli custom-wordpress-pages

我创建了一个名为compoco_wholesale.php的插件,它从add_menu.php获取表单数据,然后使用wpdb对象将其插入到db中:

<?php
include 'add_menu.php';

    global $product;
    global $wpdb;

    $wholesale_table = $wpdb->prefix . 'woocommerce_wholesale';
    if(isset($_POST['min_quan']) && isset($_POST['discount'])){
        $min_quan = $_POST['min_quan'];
        $discount = $_POST['discount'];

    $sql = "INSERT INTO $wholesale_table (min_quan, discount) 
              VALUES ('$min_quan', '$discount')";
    if ($wpdb->query($sql)) {
        echo 'Query successfully inserted';
    } else {
        echo 'Query not inserted';
    }

}

add_menu.php代码:

<?php
add_action('admin_menu', 'compoco_wholesale');

function compoco_wholesale() {
    add_menu_page( 'Compoco Wholesale', 'Compoco Wholesale', 'manage_options', 'cpc_ws', 'wholesale_options' );
    add_submenu_page( 'cpc_ws', 'Manage', 'Wholesale Prices', 'manage_options', 'cpc_sub_menu', 'compoco_wholesale_sub');
   if(wp_get_current_user()->roles[0] = 'administrator') echo yes;
}

add_filter( 'woocommerce_get_price_html', function( )
{


}, 10, 2 );

add_theme_support('woocommerce');
function wholesale_options()
{

    ?>
    <?php
}
//Compoco wholesale prices front options
function compoco_wholesale_sub()
{
    $cw_quant = (int)0;
    $cw_perc = (int)0;
    ?>
    <h1>Wholesale prices</h1>
    <p>Please enter the quantity and percentage of discount</p>
    <form action="<?php echo plugin_dir_url( __FILE__ ); ?>compoco_wholesale.php" method="POST">
        <input type="number" name="min_quan">
        <input type="number" name="discount">
        <input type="hidden" name="action" value='submit-form'>
        <button type="submit" name="submit">Send</button>
    </form>
    <?php

}

在此文件中,表单数据应该发送到compoco_wholesale.php,但我收到错误并且数据未存储在数据库中。

Call to undefined function add_action() in C:\wamp64\www\compoco\wp-content\plugins\compoco-wholesale\add_menu.php on line 2

我做错了什么?

0 个答案:

没有答案