Shortcode question

时间:2017-01-12 作者:James

Shortcode Code - 注意启动前的第二个功能$form_output 在代码的后三分之一中:

//Creates table, displays and validates bid form, and inserts valid bid data into table.
function bid_form_display(){
    //$error_msg = \'\';
    global $wpdb;

    // get post id for auction from post where auction is inserted. Used to track bids db.
    $postid = get_the_id();
    // creates jwp_bids table in database if it doesn\'t exist
    $table = $wpdb->prefix . "jwp_bids"; 
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE IF NOT EXISTS $table (
        `id` mediumint(15) NOT NULL AUTO_INCREMENT,
        `bid_amt` decimal(10,2) NOT NULL,
        `email` varchar(255) NOT NULL,
        `bid_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,  
        `post_id` mediumint(15) NOT NULL,
         UNIQUE (`id`)
    ) $charset_collate;";
    require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
    dbDelta( $sql );

    //Gets highest bid information and assigns to variables for display in post.
    $highest_bid_info = $wpdb->get_results(
        "Select max(bid_amt) AS bid, bid_time, email
        FROM $table
        WHERE post_id = $postid", ARRAY_A );
    $highest_bid_info = array_shift ( $highest_bid_info );
    $high_bid = $highest_bid_info[\'bid\'];
    $high_bidder = $highest_bid_info[\'email\'];
    $bid_time = $highest_bid_info[\'bid_time\'];

    //assign error messages to variables
    $not_human = "Human verification incorrect.";
    $bid_invalid = "Invalid or no bid placed. Please enter a valid bid.";
    $email_invalid = "Invalid or no email entered. Please enter a valid email address.";
    $insert_failed = "Oops! Sorry. Website malfunction. Please try again.";
    $bid_posted = "Thank you! Your bid was posted successfully.";

    //sanitize bid form post variables and assign to new variables.
    $email = htmlspecialchars($_POST["email"], ENT_QUOTES, \'UTF-8\');
    $bid_amount = htmlspecialchars($_POST["bid_amount"], ENT_QUOTES, \'UTF-8\');
    $human = htmlspecialchars($_POST["message_human"], ENT_QUOTES, \'UTF-8\');

    //process form entries, generate errors, if no errors insert bid info in db table
    if(!$human == 0){
        if($human != 2){
        generate_error_msg("error", $not_human); 
         } elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            generate_error_msg("error", $email_invalid);
              } elseif(empty($bid_amount)) { 
                  generate_error_msg("error", $bid_invalid);
                    } else {
                        //insert the posted form data into the database.
                        $insert_db = $wpdb->insert( 
                                        $table, 
                                            array( 
                                                \'email\' => $email,
                                                \'bid_amt\' => $bid_amount,
                                                \'post_id\' => $postid,
                                            )
                                        );
                                        if( $insert_db ){
                                            generate_error_msg("success", $bid_posted);
                                                } else {
                                                    generate_error_msg("error", $insert_failed);
                                                    }   
                            }
    }

//function to generate error messages for bid form
function generate_error_msg($type, $message){
    global $error_msg;

    if($type == "success") {
        $error_msg = "<div class=\'success\'>{$message}</div>";
        } else {
        $error_msg = "<div class=\'error\'>{$message}</div>";
    }
}

//HTML for current status of bids: Highest bid, email of bidder, time of bid.
    $form_output = \'\';
    $form_output .= \'<div id="currentbid">\';
        $form_output .= \'The current bid is $\' . $high_bid .\'.\';
        // Display current bid + $2 as minimum for next bid.      
        $form_output .= \'Your minimum bid is $\' . $high_bid . \'.\';
    $form_output .= \'</div>\';
//HTML for bid form
    $form_output .= \'<div id="bid">\';
        $form_output .= \'<?php echo\' . $error_msg . \'?>\';
        $form_output .= \'<form method="post">\';
            $form_output .= \'<p><label for="email">Notification email (IMPORTANT!) <span>*</span><br><input type="text" name="email" value="\' . $email . \'"></label></p>\';
            $form_output .= \'<p><label for="bid_amount">Enter your bid<span>*</span> <br><input type="text" name="bid_amount" value="\' . $bid_amount . \'"></label></p>\';
            $form_output .= \'<p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>\';
            $form_output .= \'<input type="hidden" name="submitted" value="1">\';
            $form_output .= \'<input type="hidden" name="post_id" value="\' . $postid . \'">\';
            $form_output .= \'<p><input type="submit"></p>\';
        $form_output .= \'</form>\';
    $form_output .= \'</div>\';

    return $form_output;

}

add_shortcode(\'bid_form\', \'bid_form_display\');
Question:function_generate_error_msg() 正在处理中。我怀疑这是因为两个函数不能驻留在同一个短代码中。

是否有其他解决方案可以在原始函数中重复复制函数代码

1 个回复
最合适的回答,由SO网友:prosti 整理而成

Why not like this:

//function to generate error messages for bid form
function generate_error_msg($type, $message){
    global $error_msg;

    if($type == "success") {
        $error_msg = "<div class=\'success\'>{$message}</div>";
        } else {
        $error_msg = "<div class=\'error\'>{$message}</div>";
    }
}

//Creates table, displays and validates bid form, and inserts valid bid data into table.
function bid_form_display(){
    //$error_msg = \'\';
    global $wpdb;

    // get post id for auction from post where auction is inserted. Used to track bids db.
    $postid = get_the_id();
    // creates jwp_bids table in database if it doesn\'t exist
    $table = $wpdb->prefix . "jwp_bids"; 
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE IF NOT EXISTS $table (
        `id` mediumint(15) NOT NULL AUTO_INCREMENT,
        `bid_amt` decimal(10,2) NOT NULL,
        `email` varchar(255) NOT NULL,
        `bid_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,  
        `post_id` mediumint(15) NOT NULL,
         UNIQUE (`id`)
    ) $charset_collate;";
    require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
    dbDelta( $sql );

    //Gets highest bid information and assigns to variables for display in post.
    $highest_bid_info = $wpdb->get_results(
        "Select max(bid_amt) AS bid, bid_time, email
        FROM $table
        WHERE post_id = $postid", ARRAY_A );
    $highest_bid_info = array_shift ( $highest_bid_info );
    $high_bid = $highest_bid_info[\'bid\'];
    $high_bidder = $highest_bid_info[\'email\'];
    $bid_time = $highest_bid_info[\'bid_time\'];

    //assign error messages to variables
    $not_human = "Human verification incorrect.";
    $bid_invalid = "Invalid or no bid placed. Please enter a valid bid.";
    $email_invalid = "Invalid or no email entered. Please enter a valid email address.";
    $insert_failed = "Oops! Sorry. Website malfunction. Please try again.";
    $bid_posted = "Thank you! Your bid was posted successfully.";

    //sanitize bid form post variables and assign to new variables.
    $email = htmlspecialchars($_POST["email"], ENT_QUOTES, \'UTF-8\');
    $bid_amount = htmlspecialchars($_POST["bid_amount"], ENT_QUOTES, \'UTF-8\');
    $human = htmlspecialchars($_POST["message_human"], ENT_QUOTES, \'UTF-8\');

    //process form entries, generate errors, if no errors insert bid info in db table
    if(!$human == 0){
        if($human != 2){
        generate_error_msg("error", $not_human); 
         } elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            generate_error_msg("error", $email_invalid);
              } elseif(empty($bid_amount)) { 
                  generate_error_msg("error", $bid_invalid);
                    } else {
                        //insert the posted form data into the database.
                        $insert_db = $wpdb->insert( 
                                        $table, 
                                            array( 
                                                \'email\' => $email,
                                                \'bid_amt\' => $bid_amount,
                                                \'post_id\' => $postid,
                                            )
                                        );
                                        if( $insert_db ){
                                            generate_error_msg("success", $bid_posted);
                                                } else {
                                                    generate_error_msg("error", $insert_failed);
                                                    }   
                            }
    }


//HTML for current status of bids: Highest bid, email of bidder, time of bid.
    $form_output = \'\';
    $form_output .= \'<div id="currentbid">\';
        $form_output .= \'The current bid is $\' . $high_bid .\'.\';
        // Display current bid + $2 as minimum for next bid.      
        $form_output .= \'Your minimum bid is $\' . $high_bid . \'.\';
    $form_output .= \'</div>\';
//HTML for bid form
    $form_output .= \'<div id="bid">\';
        $form_output .= \'<?php echo\' . $error_msg . \'?>\';
        $form_output .= \'<form method="post">\';
            $form_output .= \'<p><label for="email">Notification email (IMPORTANT!) <span>*</span><br><input type="text" name="email" value="\' . $email . \'"></label></p>\';
            $form_output .= \'<p><label for="bid_amount">Enter your bid<span>*</span> <br><input type="text" name="bid_amount" value="\' . $bid_amount . \'"></label></p>\';
            $form_output .= \'<p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>\';
            $form_output .= \'<input type="hidden" name="submitted" value="1">\';
            $form_output .= \'<input type="hidden" name="post_id" value="\' . $postid . \'">\';
            $form_output .= \'<p><input type="submit"></p>\';
        $form_output .= \'</form>\';
    $form_output .= \'</div>\';

    return $form_output;

}

add_shortcode(\'bid_form\', \'bid_form_display\');

相关推荐

Do not parse shortcode in CPT

我有一个CPT,我不想在它的内容中解析shortcode(使用\\u content()函数)。我可以使用remove\\u filter删除短代码的默认过滤器。但我如何确定我只是为了我想要的CPT而删除过滤器?我有一个在页面中使用的快捷码[我的自定义快捷码]。此短代码使用WP\\U查询和输出CPT帖子。我不想在这篇CPT文章中分析短代码。我是否应该在短代码解析挂钩之前用虚拟内容更改短代码,并在之后替换回来?或者我应该在我的CPT输出之前删除短代码的默认过滤器,然后在我的CPT输出完成后再次添加短代码的默