0

Shortcode Code - Note second function before start of $form_output in latter third of code:

//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() is not being processed. I suspect it is because two functions can't reside in the same shortcode.

Is there a solution other that copying the function code repeatedly throughout the original function?

3
  • You can also define function inside the function but make it on top. – prosti Jan 12 '17 at 3:14
  • Please improve your question title. Summarize your specific problem, do not include the words "WordPress" or "question". – fuxia Jan 12 '17 at 7:40
  • Got it boss man. – James Jan 12 '17 at 14:21
1

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');
7
  • Moved the function to the top and it is being called now. So best practice is to place all supporting functions at top and function attached to shortcode following? Is that a rule to live by? – James Jan 12 '17 at 4:03
  • @James, I would rather go and define these functions outside of the function closure. That would be the best. – prosti Jan 12 '17 at 4:30
  • The problem you had was based on the fact how the PHP parser works. – prosti Jan 12 '17 at 4:32
  • I see. Top or bottom doesn't matter as long as it is not inside the main function. Thank you! – James Jan 12 '17 at 4:32
  • @James Top or bottom doesn't matter as long as it is not inside the main function. Correct! – prosti Jan 12 '17 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.