0

我有一个表单,它使用 public.php 中的这个函数保存来自各个字段的数据:

public function save_ad_content() {

        // If $_POST['aas_upload_ad'] is set
        if ( ! empty( $_POST['advanced-ads-selling-upload-ad'] ) && $_POST['advanced-ads-selling-upload-ad'] == 'advanced-ads-selling-upload-ad' ) {
            $data = array(); // Declare variables
            switch ( $_POST['advads_selling_ad_type'] ) { // Switch case on ad_type selected
                case 'image':
                    $errors     = ''; // Declare variables
                    $file_name  = $_FILES['advads_selling_ad_image']['name']; // Get file name
                    $file_size  = $_FILES['advads_selling_ad_image']['size']; // Get file size
                    $file_tmp   = $_FILES['advads_selling_ad_image']['tmp_name']; // Get file's temporary name
                    $file_type  = $_FILES['advads_selling_ad_image']['type']; // Get file type
                    $target_url = ! empty( $_POST['advads_selling_ad_url'] ) ? $_POST['advads_selling_ad_url'] : '';

                    $file_datas = explode( '.', $file_name ); // Explode file name to retrieve extension
                    foreach ( $file_datas as $file_data ) {
                        $file_data_arr[] = $file_data; // Convert associative array to normal array
                    }

                    $file_ext   = strtolower( end( $file_data_arr ) ); // Get file extension
                    $expensions = array( 'jpeg', 'jpg', 'png', 'gif' ); // Declare variables for allowed extension types

                    if ( in_array( $file_ext, $expensions ) === false ) { // if file extensions is within allowed extensions
                        $errors = esc_html__( 'Extension not allowed, please choose a JPEG, PNG or GIF file.', 'advanced-ads-selling' );
                    }

                    $max_file_size = apply_filters( 'advanced-ads-selling-upload-file-size', 1048576 );
                    if ( $file_size > $max_file_size ) {
                        $errors = sprintf(
                            // translators: %s is a file size with one decimal
                            __( 'The allowed file size is %s MB', 'advanced-ads-selling' ),
                            number_format_i18n( $max_file_size / 1000000, 1 )
                        );
                    }

                    if ( empty( $errors ) ) {

                        // Get the path to the upload directory.
                        $wp_upload_dir = wp_upload_dir();

                        // These files need to be included as dependencies when on the front end.
                        require_once ABSPATH . 'wp-admin/includes/image.php';
                        require_once ABSPATH . 'wp-admin/includes/file.php';
                        require_once ABSPATH . 'wp-admin/includes/media.php';

                        // Insert the attachment.
                        $attach_id = media_handle_upload( 'advads_selling_ad_image', 0 );
                        $post_guid = get_the_guid( $attach_id );

                        $ad_id           = Advanced_Ads_Selling_Order::order_item_id_to_ad_id( $_POST['advads_selling_order_item'] );
                        $ad_post_content = '<img src="' . $post_guid . '" alt="' . $file_name . '" />';
                        /*
                        if( !empty( $target_url ) ) {//check when target url then wrap link with them
                            $ad_post_content    = sprintf( '<a href="%s" target="_blank">%s</a>', $target_url, $ad_post_content );
                        }*/

                        // get ad object
                        $ad = new Advanced_Ads_Ad( $ad_id );

                        $ad->type = 'image';

                        $output['image_id'] = $attach_id;

                        $ad->set_option( 'output', $output );

                        $ad->url = esc_url( $_POST['advads_selling_ad_url'] );

                        // double check if we can use fopen
                        /*
                        if ( !empty ( $post_guid ) && ini_get('allow_url_fopen') && function_exists( 'getimagesize' ) ) {
                            $image_size = getimagesize( $post_guid );

                            $ad->width  = $image_size[0];
                            $ad->height = $image_size[1];
                        } else {
                            $ad->width  = 0;
                            $ad->height = 0;
                        }*/

                        $ad->content = $ad_post_content;
                        $ad->status  = 'pending';

                        if ( $attach_id ) {
                            $attachment_meta = wp_get_attachment_metadata( $attach_id );
                            $all_posts_id    = get_post_meta( $attach_id, '_advanced-ads_parent_id' );

                            if ( ! in_array( $ad_id, $all_posts_id ) ) {
                                add_post_meta( $attach_id, '_advanced-ads_parent_id', $ad_id, false );
                            }
                        }

                        $ad->width  = isset( $attachment_meta['width'] ) ? absint( $attachment_meta['width'] ) : 0;
                        $ad->height = isset( $attachment_meta['height'] ) ? absint( $attachment_meta['height'] ) : 0;

                        // update the ad post
                        $new_ad_content = array(
                            'ID'          => $ad_id,
                            'post_status' => 'pending',
                        );

                        $return = wp_update_post( $new_ad_content );

                        $ad->save();

                        $_POST['success'] = 'success';
                    } else {
                        $_POST['errors'] = $errors;
                    }

                    break;

                case 'plain':
                    if ( ! isset( $_POST['advads_selling_ad_content'] ) || ! trim( $_POST['advads_selling_ad_content'] ) ) {
                        $errors = esc_html__( 'Ad content missing.', 'advanced-ads-selling' );
                    }

                    if ( empty( $errors ) ) {
                    $ad_id = Advanced_Ads_Selling_Order::order_item_id_to_ad_id( $_POST['advads_selling_order_item'] );

                    // update the ad post
                    $new_ad_content = array(
                        'ID'           => $ad_id,
                        'post_content' => trim( $_POST['advads_selling_ad_content'] ),
                        'post_status'  => 'pending',
                    );

                    $return = wp_update_post( $new_ad_content );
                    if ( is_wp_error( $return ) ) {
                            die( esc_html__( 'Error when submitting the ad. Please contact the site admin.', 'advanced-ads-selling' ) );
                        }

                        $_POST['success'] = 'success';
                    } else {
                        $_POST['errors'] = $errors;
                    }
            }
            
            /**
             * Allow add-ons to add their own logic for another custom ad type
             */
            do_action( 'advanced-ads-selling-save-ad-content-after', $_POST );
        }
    }

我有 2 个可用的钩子:

do_action( 'advanced-ads-selling-ad-setup-form-types-after', $ad_types, $_item );

用于显示自定义字段

add_action('advanced-ads-selling-save-ad-content-after');

用于保存自定义字段。

所以我写了我的第一个回调来显示我自己的字段:

add_action('advanced-ads-selling-ad-setup-form-types-after', 'display_ads_custom_field');

function display_ads_custom_field () {

?>
                    <div class="myfield">
                        <p><?php esc_html_e( 'My Field Title' ); ?></p>

                        <textarea class="myfield"></textarea>

                    </div>
                    <?php


}

然后我将保存钩子与保存功能一起使用:

add_action('advanced-ads-selling-save-ad-content-after', 'save_ad_content');

但是没有显示任何字段,也没有保存任何内容。

最初,我能够使用我的 advanced-ads-sales-ad-setup-form-types-after 钩子显示该字段,但它没有被保存。现在它甚至没有显示。

我究竟做错了什么?在表单标签之间定义优先级是否必要?表单保存功能和我的字段之间还没有联系,那么它应该如何保存它的值呢?

提前致谢!

最后这是我的代码,没有错误并提交,但不会保存:

add_action('advanced-ads-selling-ad-setup-form-types-after', 'display_ad_headline_field');

function display_ad_headline_field () {

                    ?>
                    <div class="my_adfield">
                        <p><?php esc_html_e( 'My ad field name', 'advanced-ads-selling' ); ?></p>
                        <textarea name="my_adfield_name"></textarea>
                    </div>
                    <?php

                }

add_action( 'advanced-ads-selling-save-ad-content-after', 'save_my_field' );

function save_my_field($ad_id) {

if ( ! isset( $_POST['my_adfield_name'] ) || ! trim( $_POST['my_adfield_name'] ) ) {
                        $errors = esc_html__( 'Ad content missing.', 'advanced-ads-selling' );
                    }

                    if ( empty( $errors ) ) {
                    $ad_id = Advanced_Ads_Selling_Order::order_item_id_to_ad_id( $_POST['advads_selling_order_item'] );

                    // update the ad post
                    $new_ad_content = array(
                        'ID'           => $ad_id,
                        'post_content' => trim( $_POST['my_adfield_name'] ),
                        'post_status'  => 'pending',
                    );

                    $return = wp_update_post( $new_ad_content );
                    if ( is_wp_error( $return ) ) {
                            die( esc_html__( 'Error when submitting the ad. Please contact the site admin.', 'advanced-ads-selling' ) );
                        }

                        $_POST['success'] = 'success';
                    } else {
                        $_POST['errors'] = $errors;
                    }

            }
4

0 回答 0