【问题标题】:Wordpress from guestform add image into post_content来自 guestform 的 Wordpress 将图像添加到 post_content
【发布时间】:2020-02-07 14:06:45
【问题描述】:

我试图让客人通过帖子类别在我的 wordpress 页面上发布活动,他们需要为帖子设置一个帖子缩略图和 1-3 张图片。 我用 set_post_thumbnail() 添加了帖子缩略图,但我无法弄清楚并在 wordpress 法典中找到了一些东西来帮助我,也许你们知道一些东西可以帮助我以正确的方式? 我到目前为止的代码:

//Upload Image
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );
    $attachment_id = media_handle_upload('file',$post_id);
    $attachment_id2 = media_handle_upload('file2',$post_id);
    $attachment_id3 = media_handle_upload('file3',$post_id);
    $attachment_id4 = media_handle_upload('file4',$post_id);

//Set Image as thumbnail
    set_post_thumbnail($post_id, $attachment_id); 

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我觉得这个问题有点含糊,所以我会用我认为你的意思来回答。

    首先,您需要表单上的enctype 属性。

    <form ... enctype="multipart/form-data">
        <input name="file" type="file">
        <input name="file2" type="file">
        <input name="file3" type="file">
        <input name="file4" type="file">
    </form>
    

    现在在您的流程文件中,您需要从 $_FILES 数组中获取每个文件。

    <?php
    
        $file = !empty( $_FILES[ 'file' ] ) ? $_FILES[ 'file' ] : null;
        $file2 = !empty( $_FILES[ 'file2' ] ) ? $_FILES[ 'file2' ] : null;
        $file3 = !empty( $_FILES[ 'file3' ] ) ? $_FILES[ 'file3' ] : null;
        $file4 = !empty( $_FILES[ 'file4' ] ) ? $_FILES[ 'file4' ] : null;
    

    最后,您需要一个自定义函数来处理每个文件的上传。这是我在我的项目中创建和使用的一个(针对这个问题进行了修改,所以请测试!)。

    <?php
    
    function my_asset_uploader( $file, $parent_id = 0, $allowed = [] ) {
        require_once ABSPATH . 'wp-admin/includes/file.php';
        require_once ABSPATH . 'wp-admin/includes/image.php';
    
        // Get basic attributes
        $filename   = basename( $file[ 'name' ] );
        $mime       = wp_check_filetype( $filename );
    
        // Get the content type from response headers
        $type   = !empty( $mime[ 'type' ] ) ? $mime[ 'type' ] : $file[ 'type' ];
        $ext    = !empty( $mime[ 'ext' ] ) ? $mime[ 'ext' ] : trim( explode( '|' , array_search( $type, get_allowed_mime_types() ) )[ 0 ] );
    
        // Basic checks
        if ( !$type || !$ext || ( !empty( $allowed ) && is_array( $allowed ) && !in_array( $ext, $allowed ) ) ) {
            // Not a valid file
            return new WP_Error( 'upload', 'Invalid file type. Please try another file.' );
        }
    
        // Move file to wp-content
        $body   = @file_get_contents( $file[ 'tmp_name' ] );
        $file   = wp_upload_bits( $filename, null, $body );
    
        // Upload check
        if ( !empty( $file[ 'error' ] ) ) {
            return new WP_Error( 'upload', $file[ 'error' ] );
        }
    
        // Write attachment location to the database
        $attachment_id = wp_insert_attachment( [
            'post_title'        => $filename,
            'post_mime_type'    => $file[ 'type' ]
        ], $file[ 'file' ], $parent_id, true );
    
        if ( is_wp_error( $attachment_id ) ) {
            return $attachment_id;
        }
    
        // Generate meta
        $attach_data = wp_generate_attachment_metadata( $attachment_id, $file[ 'file' ] );
        wp_update_attachment_metadata( $attachment_id, $attach_data );
    
        return $attachment_id;
    }
    

    用法

    <?php
    
    /**
     * Extensions we allow the user to upload
     */
    $allowed_extensions = [ 'jpg', 'jpeg', 'png', 'gif' ];
    
    /**
     * Insert post
     */
    $post_id = wp_insert_post( ... );
    
    /**
     * Get all files from the request payload
     */
    $all_images = array_filter( [
        !empty( $_FILES[ 'file' ] ) ? $_FILES[ 'file' ] : null,
        !empty( $_FILES[ 'file2' ] ) ? $_FILES[ 'file2' ] : null,
        !empty( $_FILES[ 'file3' ] ) ? $_FILES[ 'file3' ] : null,
        !empty( $_FILES[ 'file4' ] ) ? $_FILES[ 'file4' ] : null,
    ] );
    
    /**
     * Uploaded files
     */
    $attachment_ids = [];
    
    /**
     * Check we have any images before looping
     */
    if ( $all_images ) {
        foreach ( $all_images as $img_file ) {
            $this_id = my_asset_uploader( $img_file, $post_id, $allowed_extensions );
    
            if ( $this_id && !is_wp_error( $this_id ) ) {
                $attachment_ids[] = $this_id;
            }
        }
    }
    
    /**
     * Check at least one image was successfully uploaded
     */
    if ( !empty( $attachment_ids[ 0 ] ) ) {
        set_post_thumbnail( $post_id, $attachment_ids[ 0 ] );
    }
    

    这只是引导您朝着正确方向前进的起点。

    希望这会有所帮助!


    编辑

    将三张图片添加到 post_content...

    如果使用上述过程并且您拥有附件 ID 数组,您可以将每个附件的图像 html 附加到帖子内容,如下所示:

    <?php
    
    ...
    
    /**
     * Check at least one image was successfully uploaded
     */
    if ( !empty( $attachment_ids[ 0 ] ) ) {
        set_post_thumbnail( $post_id, $attachment_ids[ 0 ] );
    }
    
    /**
     * We have multiple files, lets append them to the post_content
     */
    if ( count( $attachment_ids ) > 1 ) {
    
        $appendage = '';
    
        foreach ( $attachment_ids as $i => $img_id ) {
            // Skip the first image because we used that as the thumbnail
            if ( $i === 0 ) continue;
    
            // Get the attachment HTML for a `large` sized image
            if ( $html = wp_get_attachment_image( $img_id, 'large' ) ) {
                $appendage .= $html;
            }
        }
    
        if ( !empty( $appendage ) ) {
    
            /**
             * Get the current post content
             */
            $current_content = get_post_field( 'post_content', $post_id, 'raw' );
    
            /**
             * Update the post
             */
            wp_update_post( [
                'ID' => $post_id,
                'post_content' => ( $current_content . $appendage ) // Joining the two strings
            ] );
        }
    
    }
    

    【讨论】:

    • 帮助很大,谢谢。我的主要问题是将 3 张图片添加到 post_content 中,这样如果我使用 the_content,图片就会显示在那里。
    猜你喜欢
    • 2021-10-15
    • 2022-12-21
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    相关资源
    最近更新 更多