如何构建一个在创建表单时生成短代码的插件

时间:2017-06-02 06:36:51

标签: php wordpress plugins

我不知道如何创建一个能够以联系人形式7生成短代码库的插件。

m actually creating a plugin right now that can add form, but I don知道逻辑是如何进行的,它会产生一个短代码。

请任何有用的想法。

    function crm_insert_into_db() {
    global $wpdb;
    // creates my_table in database if not exists
    $table = $wpdb->prefix . "customers"; 
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE IF NOT EXISTS $table (
    `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    `name` text NOT NULL,
    `email` text NOT NULL,
    `phone_number` int NOT NULL,
    `budget` int NOT NULL,
    `message` text NOT NULL,
    `date` date NOT NULL,
    UNIQUE (`id`)
    ) $charset_collate;";
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
    // starts output buffering
    ob_start();
    ?>
    <form method="post" id="v_form">
    <label for="customer_name"><h3>Your name?</h3></label>
    <input type="text" name="customer_name" id="customer_name" />

    <label for="customer_email"><h3>Your Email?</h3></label>
    <input type="email" name="customer_email" id="customer_email" />

    <label for="customer_number"><h3>Your Phone Number?</h3></label>
    <input type="text" name="customer_number" maxlength="12" 
    id="customer_number" />

    <label for="customer_budget"><h3>Your Budget?</h3></label>
    <input type="text" name="customer_budget" maxlength="3" 
    id="customer_budget" />

    <label for="customer_message"><h3>Your Message?</h3></label>
    <textarea name="customer_message" id="customer_message" rows="10" 
    cols="35"></textarea>
    <input type="submit" name="submit_form" value="submit" />
    </form>
    <?php
    $html = ob_get_clean();
    // does the inserting, in case the form is filled and submitted
    if ( isset( $_POST["submit_form"] ) && $_POST["customer_name"] != "" ) {
    $table = $wpdb->prefix."customers";
    $name = strip_tags($_POST["customer_name"], "");
    $email = strip_tags($_POST["customer_email"], "");
    $number = strip_tags($_POST["customer_number"], "");
    $budget = strip_tags($_POST["customer_budget"], "");
    $message = strip_tags($_POST["customer_message"], "");
    $date = strip_tags(date("Y/m/d"), "");
    $wpdb->insert( 
        $table, 
        array( 
            'name' => $name,
            'email' => $email,
            'phone_number' => $number,
            'budget' => $budget,
            'message' => $message,
            'date' => $date,
        )
    );
    $html = "<p>Your name <strong>$name</strong> was successfully recorded. 
    Thanks!!</p>";
    }
    return $html;
    }
    // adds a shortcode you can use: [crm-form]
    add_shortcode('crm-form', 'crm_insert_into_db');

1 个答案:

答案 0 :(得分:0)

这对您有用。

// Add Shortcode
function custom_shortcode() {
//Do work here
}
add_shortcode( '', 'custom_shortcode' );