0

I am stuck editing this form submission. When a user fills out the form, I want to map the 'stm_seller_notes' to the default wordpress content area.

There are two problems I am trying to solve, stop the input stripping HTML and map it to 'post_content'.

<textarea name="stm_seller_notes"> </textarea>
// ADD A CAR - ORIGNAL
function stm_add_a_car()
{
    check_ajax_referer( 'stm_add_a_car', 'security', false );

    $response = array();
    $first_step = array(); //needed fields
    $second_step = array(); //secondary fields
    $car_features = array(); //array of features car
    $videos = array(); /*videos links*/
    $notes = esc_html__( 'N/A', 'motors' );
    $registered = '';
    $vin = '';

    $history = array(
        'label' => '',
        'link' => ''
    );

    $location = array(
        'label' => '',
        'lat' => '',
        'lng' => '',
    );

    if ( !is_user_logged_in() ) {
        $response['message'] = esc_html__( 'Please, log in', 'motors' );
        wp_send_json( $response );
    } else {
        $user = stm_get_user_custom_fields( '' );
        $restrictions = stm_get_post_limits( $user['user_id'] );
    }


    $response['message'] = '';
    $error = false;

    $demo = stm_is_site_demo_mode();
    if ( $demo ) {
        $error = true;
        $response['message'] = esc_html__( 'Site is on demo mode', 'motors' );
    }

    $update = false;
    if ( !empty( $_POST['stm_current_car_id'] ) ) {
        $post_id = intval( $_POST['stm_current_car_id'] );
        $car_user = get_post_meta( $post_id, 'stm_car_user', true );
        $update = true;

        /*Check if current user edits his car*/
        if ( intval( $car_user ) != intval( $user['user_id'] ) ) {
            wp_die();
        }
    }

    /*Get first step*/
    if ( !empty( $_POST['stm_f_s'] ) ) {
        foreach ( $_POST['stm_f_s'] as $post_key => $post_value ) {
            if ( $_POST['stm_f_s'][$post_key] != "" ) {
                $postKey = str_replace( "_pre_", "-", $post_key );
                $first_step[sanitize_title( $postKey )] = sanitize_text_field($_POST['stm_f_s'][$post_key]) ;
            } else {
                $error = true;
                $response['message'] = esc_html__( 'Enter required fields', 'motors' );
            }
        }
    }

    if ( empty( $first_step ) ) {
        $error = true;
        $response['message'] = esc_html__( 'Enter required fields', 'motors' );
    }

    /*Getting second step*/
    foreach ( $_POST as $second_step_key => $second_step_value ) {
        if ( strpos( $second_step_key, 'stm_s_s_' ) !== false ) {
            if ( $_POST[$second_step_key] != "" ) {
                $original_key = str_replace( 'stm_s_s_', '', $second_step_key );
                $second_step[sanitize_title( $original_key )] = sanitize_text_field( $_POST[$second_step_key] );
            }
        }
    }

    /*Getting car features*/
    if ( !empty( $_POST['stm_car_features_labels'] ) ) {
        foreach ( $_POST['stm_car_features_labels'] as $car_feature ) {
            $car_features[] = esc_attr( $car_feature );
        }
    }

    /*Videos*/
    if ( !empty( $_POST['stm_video'] ) ) {
        foreach ( $_POST['stm_video'] as $video ) {

            if ( ( strpos( $video, 'youtu' ) ) > 0 ) {
                $is_youtube = array();
                parse_str( parse_url( $video, PHP_URL_QUERY ), $is_youtube );
                if ( !empty( $is_youtube['v'] ) ) {
                    $video = 'https://www.youtube.com/embed/' . $is_youtube['v'];
                }
            }

            $videos[] = esc_url( $video );
            $videos = array_filter( $videos );
        }
    }

    /*Note*/
    if ( !empty( $_POST['stm_seller_notes'] ) ) {
        $notes = sanitize_textarea_field( $_POST['stm_seller_notes'] );
    }

    /*Registration date*/
    if ( !empty( $_POST['stm_registered'] ) ) {
        $registered = sanitize_text_field( $_POST['stm_registered'] );
    }

    /*Vin*/
    if ( !empty( $_POST['stm_vin'] ) ) {
        $vin = sanitize_text_field( $_POST['stm_vin'] );
    }

    /*History*/
    if ( !empty( $_POST['stm_history_label'] ) ) {
        $history['label'] = sanitize_text_field( $_POST['stm_history_label'] );
    }

    if ( !empty( $_POST['stm_history_link'] ) ) {
        $history['link'] = sanitize_text_field( $_POST['stm_history_link'] );
    }

    /*Location*/
    if ( !empty( $_POST['stm_location_text'] ) ) {
        $location['label'] = sanitize_text_field( $_POST['stm_location_text'] );
    }

    if ( !empty( $_POST['stm_lat'] ) ) {
        $location['lat'] = sanitize_text_field( $_POST['stm_lat'] );
    }

    if ( !empty( $_POST['stm_lng'] ) ) {
        $location['lng'] = sanitize_text_field( $_POST['stm_lng'] );
    }

    if ( empty( $_POST['stm_car_price'] ) ) {
        $error = true;
        $response['message'] = esc_html__( 'Please add car price', 'motors' );
        $price = '';
        $normalPrice = '';

    } else {
        $normalPrice = abs( intval( $_POST['stm_car_price'] ) );
        $price = ( function_exists( 'stm_convert_to_normal_price' ) ) ? stm_convert_to_normal_price( $normalPrice ) : $normalPrice;
    }

    if ( isset( $_POST['car_price_form_label'] ) && !empty( $_POST['car_price_form_label'] ) ) {

        if ( empty( $_POST['stm_car_price'] ) ) {
            $error = false;
            unset( $response['message'] );
        }

        $location['car_price_form_label'] = sanitize_text_field( $_POST['car_price_form_label'] );
    } else {
        $location['car_price_form_label'] = '';
    }

    if ( isset( $_POST['stm_car_sale_price'] ) ) {
        $salePrice = abs( sanitize_text_field( $_POST['stm_car_sale_price'] ) );
        $location['stm_car_sale_price'] = ( function_exists( 'stm_convert_to_normal_price' ) ) ? stm_convert_to_normal_price( $salePrice ) : $salePrice;
    }

    $generic_title = '';
    if ( !empty( $_POST['stm_car_main_title'] ) ) {
        $generic_title = sanitize_text_field( $_POST['stm_car_main_title'] );
    }

    $motors_gdpr_agree = ( isset( $_POST['motors-gdpr-agree'] ) && !empty( $_POST['motors-gdpr-agree'] ) ) ? $_POST['motors-gdpr-agree'] : false;

    if ( $motors_gdpr_agree && $motors_gdpr_agree == 'not_agree' ) {
        $error = true;
        $gdpr = get_option( 'stm_gdpr_compliance', '' );
        $ppLink = ( $gdpr['stmgdpr_privacy'][0]['privacy_page'] != 0 ) ? get_the_permalink( $gdpr['stmgdpr_privacy'][0]['privacy_page'] ) : '';
        $ppLinkText = ( !empty( $gdpr ) && !empty( $gdpr['stmgdpr_privacy'][0]['link_text'] ) ) ? $gdpr['stmgdpr_privacy'][0]['link_text'] : '';
        $mess = sprintf( __( "Providing consent to our <a href='%s'>%s</a> is necessary in order to use our services and products.", 'motors' ), $ppLink, $ppLinkText );


        $response['html'] = 'html';
        $response['message'] = $mess;
    }

    if(get_theme_mod('enable_plans', false) && stm_is_multiple_plans() && $_POST['btn-type'] != 'pay') {
        if(empty($_POST['selectedPlan'])) {
            $error = true;
            $response['message'] = esc_html__( 'Please select plan', 'motors' );
        }
    }

    /*Generating post*/
    if ( !$error ) {

        if ( $restrictions['premoderation'] ) {
            $status = 'pending';
            $user = stm_get_user_custom_fields( '' );
        } else {
            $status = 'publish';
        }

        if ( $_POST['btn-type'] == 'pay' ) {
            $status = 'pending';
        }

        $post_data = array(
            'post_type' => stm_listings_post_type(),
            'post_title' => '',
            'post_status' => $status,
        );

        if(!$update && stm_get_wpb_def_tmpl()) {
            $post_data['post_content'] = stm_get_wpb_def_tmpl();
        }

        foreach ( $first_step as $taxonomy => $title_part ) {
            $term = get_term_by( 'slug', $title_part, $taxonomy );
            $post_data['post_title'] .= $term->name . ' ';
        }

        if ( !empty( $generic_title ) ) {
            $post_data['post_title'] = $generic_title;
        }


        if ( !$update ) {
            $post_id = wp_insert_post( apply_filters( 'stm_listing_save_post_data', $post_data ), true );
            if(!is_wp_error($post_id) && stm_get_wpb_def_tmpl()) {
                update_post_meta( $post_id,'_wpb_vc_js_status', 'true');
            }
        }

        if ( !is_wp_error( $post_id ) ) {

            if ( $update ) {

                $ppl = get_post_meta($post_id, 'pay_per_listing', true);
                $ppOrderId = get_post_meta($post_id, 'pay_per_order_id', true);

                if(!empty($ppl) && !empty($ppOrderId)) {
                    $order = new WC_Order( $ppOrderId );
                    $orderData = (Object)$order->get_data();

                    if($orderData->status != 'completed') {
                        $status = 'pending';
                    }
                } elseif (!empty($ppl) && empty($ppOrderId)) {
                    $status = 'pending';
                }

                $post_data_update = array(
                    'ID' => $post_id,
                    'post_status' => $status,
                );

                if ( !empty( $generic_title ) ) {
                    $post_data_update['post_title'] = $generic_title;
                }

                wp_update_post( apply_filters( 'stm_listing_save_post_data', $post_data_update ) );

            }

            $terms = array();

            /*Set categories*/
            foreach ( $first_step as $tax => $term ) {
                $tax_info = stm_get_all_by_slug( $tax );
                if ( !empty( $tax_info['numeric'] ) and $tax_info['numeric'] ) {
                    update_post_meta( $post_id, $tax, abs( sanitize_title( $term ) ) );
                    $meta[$tax] = abs( sanitize_title( $term ) );
                } else {
                    $terms[$tax] = $term;
                    $meta[$tax] = sanitize_title( $term );
                }
            }

            /*Set categories*/
            foreach ( $second_step as $tax => $term ) {

                $term = apply_filters( 'stm_change_value', $term );

                if ( !empty( $tax ) ) {
                    $tax_info = stm_get_all_by_slug( $tax );
                    if ( !empty( $tax_info['numeric'] ) and $tax_info['numeric'] ) {
                        update_post_meta( $post_id, $tax, $term );
                        $meta[$tax] = $term;
                    } else {
                        $terms[$tax] = $term;
                        $meta[$tax] = $term;
                    }
                }
            }

            $meta = array(
                'stock_number' => $post_id,
                'stm_car_user' => $user['user_id'],
                'price' => $price,
                'stm_genuine_price' => $price,
                'title' => 'hide',
                'breadcrumbs' => 'show',
            );

            if ( !empty( $videos ) ) {
                $meta['gallery_video'] = $videos[0];

                if ( count( $videos ) > 1 ) {
                    array_shift( $videos );
                    $meta['gallery_videos'] = array_filter( array_unique( $videos ) );
                }
            } else {
                $meta['gallery_video'] = '';
                $meta['gallery_videos'] = '';
            }

            $meta['vin_number'] = $vin;
            $meta['registration_date'] = $registered;
            $meta['history'] = $history['label'];
            $meta['history_link'] = $history['link'];
            $meta['stm_car_location'] = $location['label'];
            $meta['stm_lat_car_admin'] = $location['lat'];
            $meta['stm_lng_car_admin'] = $location['lng'];
            $meta['additional_features'] = implode( ',', $car_features );
            $terms['stm_additional_features'] = $car_features;

            update_post_meta( $post_id, 'price', $price );
            update_post_meta( $post_id, 'stm_genuine_price', $price );
            update_post_meta( $post_id, 'motors_gdpr_agree', get_the_date( 'd-m-Y', $post_id ) );
            update_post_meta( $post_id, 'listing_seller_note', $notes );

            if ( isset( $location['car_price_form_label'] ) ) {
                $meta['car_price_form_label'] = $location['car_price_form_label'];
            }

            if ( isset( $location['stm_car_sale_price'] ) && !empty( $location['stm_car_sale_price'] ) ) {
                $meta['sale_price'] = $location['stm_car_sale_price'];
                $meta['stm_genuine_price'] = $location['stm_car_sale_price'];
            } else {
                $meta['sale_price'] = '';
            }

            foreach ( apply_filters( 'stm_listing_save_post_meta', $meta, $post_id, $update ) as $key => $value ) {
                update_post_meta( $post_id, $key, $value );
            }

            foreach ( apply_filters( 'stm_listing_save_post_terms', $terms, $post_id, $update ) as $tax => $term ) {
                wp_delete_object_term_relationships( $post_id, $tax );
                wp_add_object_terms( $post_id, $term, $tax );
                update_post_meta( $post_id, $tax, sanitize_title( $term ) );
            }

            update_post_meta( $post_id, 'title', 'hide' );
            update_post_meta( $post_id, 'breadcrumbs', 'show' );

            $response['post_id'] = $post_id;
            $response['redirect_type'] = sanitize_text_field( $_POST['btn-type'] );
            if ( ( $update ) ) {
                $response['message'] = esc_html__( 'Car Updated, uploading photos', 'motors' );
            } else {
                $response['message'] = esc_html__( 'Car Added, uploading photos', 'motors' );
            }

            if ( !$update ) {
                $title_from = get_theme_mod( 'listing_directory_title_frontend', '' );
                if ( !empty( $title_from ) ) {
                    wp_update_post( array( 'ID' => $post_id, 'post_title' => stm_generate_title_from_slugs( $post_id ) ) );
                }
            }

            if(stm_is_multiple_plans()) {
                $planId = $_POST['selectedPlan'];

                if(!update) MultiplePlan::addPlanMeta($planId, $post_id, 'active');
                else MultiplePlan::updatePlanMeta($planId, $post_id, 'active');
            }

            do_action( 'stm_after_listing_saved', $post_id, $response, $update );

        } else {
            $response['message'] = $post_id->get_error_message();
        }
    }

    wp_send_json( apply_filters( 'stm_filter_add_a_car', $response ) );
}

What I have tried is this but it does not submit to the default 'post_content'. I have marked my edits with *edit* so that someone can see where I am editing and not have to look at all of this code.

//ADD A CAR - with *edit*
function stm_add_a_car()
{
    check_ajax_referer( 'stm_add_a_car', 'security', false );

    $notes = ''; // *edit* Stop escaping html


    /*Note*/
    if ( !empty( $_POST['stm_seller_notes'] ) ) {
        $notes = $_POST['stm_seller_notes']; // *edit* // Remove sanitize_textarea_field
    }

    /*Generating post*/
    if ( !$error ) {

        if ( $restrictions['premoderation'] ) {
            $status = 'pending';
            $user = stm_get_user_custom_fields( '' );
        } else {
            $status = 'publish';
        }

        if ( $_POST['btn-type'] == 'pay' ) {
            $status = 'pending';
        }

        $post_data = array(
            'post_type' => stm_listings_post_type(),
            'post_title' => '',
            'post_status' => $status,
            'post_content' => $notes, // *edit* Add notes to post_data['post_content']
        );

        if ( !$update ) {
            $post_id = wp_insert_post( apply_filters( 'stm_listing_save_post_data', $post_data ), true );
            if(!is_wp_error($post_id) && stm_get_wpb_def_tmpl()) {
                update_post_meta( $post_id,'_wpb_vc_js_status', 'true');
            }
        }

        if ( !is_wp_error( $post_id ) ) {

            if ( $update ) {

                $ppl = get_post_meta($post_id, 'pay_per_listing', true);
                $ppOrderId = get_post_meta($post_id, 'pay_per_order_id', true);

                if(!empty($ppl) && !empty($ppOrderId)) {
                    $order = new WC_Order( $ppOrderId );
                    $orderData = (Object)$order->get_data();

                    if($orderData->status != 'completed') {
                        $status = 'pending';
                    }
                } elseif (!empty($ppl) && empty($ppOrderId)) {
                    $status = 'pending';
                }

                $post_data_update = array(
                    'ID' => $post_id,
                    'post_status' => $status,
                );

                if ( !empty( $generic_title ) ) {
                    $post_data_update['post_title'] = $generic_title;
                }

                wp_update_post( apply_filters( 'stm_listing_save_post_data', $post_data_update ) );

            }

            update_post_meta( $post_id, 'price', $price );
            update_post_meta( $post_id, 'stm_genuine_price', $price );
            update_post_meta( $post_id, 'motors_gdpr_agree', get_the_date( 'd-m-Y', $post_id ) );
            // *edit* Remove -> update_post_meta( $post_id, 'listing_seller_note', $notes );

            $response['post_id'] = $post_id;
            $response['redirect_type'] = sanitize_text_field( $_POST['btn-type'] );
            if ( ( $update ) ) {
                $response['message'] = esc_html__( 'Car Updated, uploading photos', 'motors' );
            } else {
                $response['message'] = esc_html__( 'Car Added, uploading photos', 'motors' );
            }

            if ( !$update ) {
                $title_from = get_theme_mod( 'listing_directory_title_frontend', '' );
                if ( !empty( $title_from ) ) {
                    wp_update_post( array( 'ID' => $post_id, 'post_title' => stm_generate_title_from_slugs( $post_id ), 'post_content' => $notes ) );
                } // *edit* Add notes to update post_content
            }

            do_action( 'stm_after_listing_saved', $post_id, $response, $update );

        } else {
            $response['message'] = $post_id->get_error_message();
        }
    }

    wp_send_json( apply_filters( 'stm_filter_add_a_car', $response ) );
}

I have removed much of the code from the second block as it is mainly the 'generating post' section that I am concerned with.

How do I save the content submitted through <textarea value="stm_seller_notes"> to the WordPress post_content field?

Any help greatly appreciated!

  • Can you please edit your question and add the relevant part of the code? Thanks. – Christine Cooper Aug 12 at 7:34
  • Hi Christine, thanks for your response. I have removed lots of the code that is irrelevant to my question. – Aran Joyce Aug 13 at 4:10

Your Answer

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

Browse other questions tagged or ask your own question.