使用wp_handle_upload和add_post_meta将图像放入WordPress自定义帖子类型的非ACF自定义元字段

问题描述

我已经写了一个非常基本的插件,可以在wordpress上使用,它创建了一个具有多个自定义元字段的自定义帖子类型(CPT)。

我似乎无法回显图像URL。我尝试了很多其他类似的帖子解决方案,但是它们似乎都以某种方式与我未使用的高级自定义字段(ACF)插件相关。

这是表单代码,捕获数据并尝试将其放入CPT:

<?PHP
// Handle Form Submissions
if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['post-type']) &&  $_POST['post-type'] == "testimonial") :
    // Do some minor form validation to make sure there is content
    if (isset ($_POST['your-profile-picture'])) {
        if (!function_exists('wp_handle_upload')) {
            require_once(ABSPATH . 'wp-admin/includes/file.PHP');
        }

        $uploaded_profile_picture = $_FILES['your-profile-picture'];

        $upload_overrides = array(
            'test_form' => false
        );

        $move_profile_picture = wp_handle_upload($uploaded_profile_picture,$upload_overrides);

        if ( $move_profile_picture && ! isset( $move_profile_picture['error'] ) ) {
            echo __('File is valid,and was successfully uploaded.','testmionial') . "\n";
            var_dump($move_profile_picture);
            $your_profile_picture = $move_profile_picture;
        } else {
            echo $move_profile_picture['error'];
        }
    } else { 
        echo 'Please provide a profile picture.';
    }
    if (isset ($_POST['your-name'])) {
        $post_title = $_POST['your-name'] . '\'s Testimonial';
        $your_name = $_POST['your-name'];
    } else { 
        echo 'Please enter your name.';
    }
    if (isset ($_POST['your-role'])) {
        $your_role = $_POST['your-role'];
    } else { 
        echo 'Please enter your role at this company.';
    }
    if (isset ($_POST['your-company'])) {
        $your_company = $_POST['your-company'];
    } else { 
        echo 'Please enter your company name.';
    }
    if (isset($_POST['your-testimonial'])) {
        $your_testimonial = $_POST['your-testimonial'];
    } else {
        echo 'Please enter your testimonial URL.';
    }
    if (isset ($_POST['your-background-image'])) {
        if (!function_exists('wp_handle_upload')) {
            require_once(ABSPATH . 'wp-admin/includes/file.PHP');
        }

        $uploaded_profile_picture = $_FILES['your-background-image'];

        $upload_overrides = array(
            'test_form' => false
        );

        $move_background_image = wp_handle_upload($uploaded_profile_picture,$upload_overrides);

        if ( $move_background_image && ! isset( $move_background_image['error'] ) ) {
            echo __('File is valid,'testimonial') . "\n";
            var_dump($move_background_image);
            $your_background_image = $move_background_image;
        } else {
            echo $move_background_image['error'];
        }
    } else { 
        echo 'Please provide a background image.';
    }

    /*if (isset ($_POST['your-profile-picture'])) {
        $your_profile_picture = $_POST['your-profile-picture'];
    } else { 
        echo 'Please provide a profile picture.';
    }
    if (isset ($_POST['your-background-image'])) {
        $your_background_image = $_POST['your-background-image'];
    } else { 
        echo 'Please select a background image.';
    }*/


    if (isset($_POST['post-type'])) {
        $post_type = $_POST['post-type'];
    } else {
        echo 'Unable to get post type.';
    }

    // Add the content of the form to $post as an array
    $testimonial_args = array(
        'post_title'                => $post_title,'your_profile_picture'      => $your_profile_picture,'your_name'                 => $your_name,'your_role'                 => $your_role,'your_company'              => $your_company,'your_testimonial'          => $your_testimonial,'your_background_image'     => $your_background_image,'post_type'                 => $post_type,'post_status'               => 'publish'
    );

    //save the new post
    $post_id = wp_insert_post($testimonial_args,true);

    $your_profile_picture           = $your_profile_picture;
    $your_name                      = $_POST['your-name'];
    $your_role                      = $_POST['your-role'];
    $your_company                   = $_POST['your-company'];
    $your_testimonial               = $_POST['your-testimonial'];
    $your_background_image          = $your_background_image;

    add_post_Meta($post_id,'your_profile_picture',$your_profile_picture,true);
    add_post_Meta($post_id,'your_name',$your_name,'your_role',$your_role,'your_company',$your_company,'your_testimonial',$your_testimonial,'your_background_image',$your_background_image,true);

    if ($post_id != '') : ?>
        <script>
            alert('Testimonial details successfully submitted!');
        </script>
    <?PHP else : ?>
        <script>
            alert('Testimonial details submission was unsuccessful!');
        </script>
    <?PHP endif;
endif; ?>

<div class="testimonial-form">
    <div class="testimonial-form-inner">
        <form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
            <div class="form-row">
                <label for="your-profile-picture">Your Profile Picture (145px x 145px)</label>
                <input type="file" id="your-profile-picture" accept=".jpg,.jpeg,.png" tabindex="1" name="your-profile-picture" onchange="validateImageFileType()" required/>
            </div>
            <div class="form-row">
                <label for="your-name">Your Name</label>
                <input type="text" id="your-name" value="" tabindex="1" size="50" name="your-name" required/>
            </div>
            <div class="form-row">
                <label for="your-role">Your Role</label>
                <input type="text" id="your-role" value="" tabindex="3" size="50" name="your-role" required/>
            </div>
            <div class="form-row">
                <label for="your-company">Your Company</label>
                <input type="text" id="your-company" value="" tabindex="4" size="50" name="your-company" required/>
            </div>
            <div class="form-row">
                <label for="your-testimonial">Your Testimonial</label>
                <textarea id="your-testimonial" name="your-testimonial" tabindex="5" rows="5" required></textarea>
            </div>
            <div class="form-row">
                <label for="your-background-image">Your Background Image (900px x 492px)</label>
                <input type="file" id="your-background-image" accept=".jpg,.png" tabindex="1" name="your-background-image" onchange="validateImageFileType()" required/>
            </div>

            <div class="form-row">
                <input id="submit" type="submit" value="Submit" tabindex="9" name="submit"/>
            </div>

            <input type="hidden" name="post-type" value="testimonial" />
            <?PHP wp_nonce_field( 'post-type' ); ?>
        </form>
    </div>
</div>

这是数据的输出,它通过查询循环,回显每个帖子的请求字段,但是将背景图像留为空白或为空:

<?PHP
// Loop through CPT Testimonial and add to slider
$args = array( 'post_type' => 'testimonial' );
$loop = new WP_Query( $args );
if ($loop != '') : ?>

    <div class="testimonials">

        Testimonials V1

        <div id="testimonial-slider" class="testimonial-slider"> 

            <?PHP
            while ( $loop->have_posts() ) : $loop->the_post(); ?>

                <?PHP
                $background_image_id    = get_post_meta(get_the_ID(),'your_background_image_id',true);
                $background_image       = wp_get_attachment_image_url( $background_image_id,'full' );
                $profile_image_id       = get_post_meta(get_the_ID(),'your_profile_picture_id',true);
                $profile_image          = wp_get_attachment_image_url( $profile_image_id,'full' );
                var_dump(get_post_meta(get_the_ID()));
                die(); ?>

                <div class="testimonial-slide" style="background-image: url(<?PHP echo $background_image; ?>);">

                    <div class="testimonial-profile-picture" style="background-image: url(<?PHP echo $profile_image; ?>);"></div>

                    <div class="testimonial-content">
                        <?PHP echo get_post_meta(get_the_ID(),true); ?>
                    </div>

                    <div class="testimonial-signature">
                        <div class="testimonial-name"><?PHP echo get_post_meta(get_the_ID(),true); ?></div>
                        <div class="testimonial-role"><?PHP echo get_post_meta(get_the_ID(),true); ?></div>
                        <div class="testimonial-company"><?PHP echo get_post_meta(get_the_ID(),true); ?></div>
                    </div>

                </div>

            <?PHP 
            endwhile; ?>

        </div>
    </div>

<?PHP endif; ?>

根据评论的要求,帖子的var dump如下:

array(6) {
    ["your_profile_picture"]=&gt;
    array(1) {
        [0]=&gt;
        NULL
    }
    ["your_name"]=&gt;
    array(1) {
        [0]=&gt;
        string(12) "Jason"
    }
    ["your_role"]=&gt;
    array(1) {
        [0]=&gt;
        string(13) "Web Developer"
    }
    ["your_company"]=&gt;
    array(1) {
        [0]=&gt;
        string(6) "Example"
    }
    ["your_testimonial"]=&gt;
    array(1) {
        [0]=&gt;
        string(231) "Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
    }
    ["your_background_image"]=&gt;
    array(1) {
        [0]=&gt;
        NULL
    }
}

最后,当提交一份完整的表格时,其中包含所有预期的数据,来自简单验证的响应是:

Please provide a profile picture.Please select a background image.

Notice: Undefined variable: your_profile_picture in /var/sites/d/doopsdesigns.co.uk/public_html/test/clicky/wp-content/plugins/testimonial-lister/templates/testimonial-submission-form.PHP on line 95

Notice: Undefined variable: your_background_image in /var/sites/d/doopsdesigns.co.uk/public_html/test/clicky/wp-content/plugins/testimonial-lister/templates/testimonial-submission-form.PHP on line 100

Notice: Undefined index: your-profile-picture in /var/sites/d/doopsdesigns.co.uk/public_html/test/clicky/wp-content/plugins/testimonial-lister/templates/testimonial-submission-form.PHP on line 109

Notice: Undefined index: your-background-image in /var/sites/d/doopsdesigns.co.uk/public_html/test/clicky/wp-content/plugins/testimonial-lister/templates/testimonial-submission-form.PHP on line 114

请问我们能否找到一种解决方案,将图像的URL回显为背景图像样式?

关于可能的重复,我正在努力的部分是将文件传递到数据库,我已将问题更新为我的最新代码,该代码正在尝试与引用的帖子类似的方法,但是没有关于如何进行解释的说明我将上传的图像传递给要插入数据库的数据。

也没有引用使用wp_handle_upload,这将是我的首选方法:)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)