【问题标题】:Adding a few images to a new post from front-end form? [closed]从前端表单向新帖子添加一些图像? [关闭]
【发布时间】:2020-11-20 08:56:24
【问题描述】:

有一个前端表单来创建帖子,我只能将文本数据添加到新帖子(标题、内容、文本类型的自定义字段)。当我想创建一个新帖子时,我也希望能够为它们分配图像。如果可能的话,我希望每个帖子的图像数量不同。 (例如,用户可以为新帖子分配 1 到 5 张图片)。

这是一个 WordPress 网站,我使用 Avada 主题。自定义帖子类型是 Avada 的默认 Portfolio 帖子。但是,任何带有一些解释的概括性答案都将不胜感激。

这是我的子主题中 fuctions.php 中的 php 代码:

    if(isset($_POST['title'])){

    $custom_field_address1 = $_POST['address1'];

    $my_post = array(

    'post_title' => $_POST['title'],
    'post_content' => $_POST['description'],
        
    'post_status' => 'publish', 
    'post_type' => 'your_post_type_name',
    'meta_input' => array(
        'address1' => $custom_field_address1,
        )
    );
    
    $post_id = wp_insert_post($my_post);

    add_post_meta( $post_id, 'address1', $custom_field_address1, false );
    echo 'New Post Saved !';
    
    die;
   }

我的前端表单(当我想添加新帖子时,我希望能够从这里添加一些图片):

<form method="post">
<div class="form-group">
      <label for="title">Post Title:</label>
      <input type="text" class="form-control" id="title" name="title">
</div>
    
    
<div class="form-group">
      <label for="pwd">Post Description :</label>
      <textarea class="form-control"  name="description"></textarea>
</div>
      
<div class="form-group">
      <label for="address1">Address :</label>
      <input type="text" name="address1" id="address1">
</div>

<BR>
<button type="submit">Submit</button>
</form>

【问题讨论】:

    标签: php wordpress forms wordpress-theming


    【解决方案1】:

    这是在帖子中添加图像的过程:

    1. get_post_field获取现有的帖子内容
    2. 使用wp_insert_attachment 上传图片,这将为您提供图片 身份证
    3. 以某种方式从帖子内容中获取图像大小和 alt
    4. 让 WP 使用wp_get_attachment_image 为图像生成标记
    5. 用新图像 html 替换旧图像占位符
    6. 当所有图片占位符都被替换为img标签时,将修改后的帖子内容保存到数据库wp_update_post

    可选择使用set_post_thumbnail 将任何图片设置为帖子缩略图/特色图片。

    global $user_ID;
    
    $new_post = array(
        'post_title' => 'Title Of Post',
        'post_content' => 'Contents',
        'post_status' => 'publish',
        'post_date' => date('Y-m-d H:i:s'),
        'post_author' => $user_ID,
        'post_type' => 'post',
        'post_category' => array(1),
    );
    
    $post_id = wp_insert_post($new_post);
    //$post_id = 1234; //example Post ID
    $images = array('filename1.png', 'filename2.png', ... 'filename10.png'); //example images
    
    // Get post content
    $post_content = get_post_field( 'post_content', $post_id );
    
    // Get the path to the upload directory.
    $wp_upload_dir = wp_upload_dir();
    
    foreach($images as $name) {
      $attachment = array(
        'guid'=> $wp_upload_dir['url'] . '/' . basename( $name ), 
        'post_mime_type' => 'image/png',
        'post_title' => 'my description',
        'post_content' => 'my description',
        'post_status' => 'inherit'
      );
    
      /**
        * STEP 1
        * add images as attachments to WordPress
        */
      $image_id = wp_insert_attachment($attachment, $name, $post_id);
      // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
      require_once( ABSPATH . 'wp-admin/includes/image.php' );
      // Generate the metadata for the attachment, and update the database record.
      $attach_data = wp_generate_attachment_metadata( $image_id, $name );
      wp_update_attachment_metadata( $image_id, $attach_data );
    
      /**
        * STEP 2
        * Grab image data from $post_content
        */
      // strpos() + substr() maybe?
      $width = 123;
      $height = 123;
      $alt = 'Hello';
    
      /**
        * STEP 3
        * get html markup for image
        */  
      // Let WP generate the html markup for the image, adjust size as needed (thumbnail,medium,large,full,array(width,height))
      $image_html = wp_get_attachment_image( $image_id, array( $width, $height ), false, array( 'alt' => $alt ) );
    
      /**
        * STEP 4
        * Replace placeholders in content with img markup
        */
      preg_replace( $pattern, $image_html, $post_content ) // I don't understand regex well enough to give an example, so you need to figure it out yourself
    
      /**
        * OPTIONAL
        * set image as featured
        */
      if ( $name === 'example' ) {
        set_post_thumbnail( $post_id, $image_id );
      }
    
    }
    
    /**
      * STEP 5
      * Update post content
      */
    $post_with_imported_images = array(
      'ID'           => $post_id,
      'post_content' => $post_content,
    );
    wp_update_post( $post_with_imported_images );
    

    【讨论】:

    • 快速浏览一下,我注意到您的意思是向现有帖子添加图像。我希望它在我创建帖子时添加图像。我会尝试使用你漂亮的代码来实现我想要的。或者,如果可以的话,您可以为此目的对其进行编辑,我将不胜感激。
    • 我更新了答案。您只需要发布您想要的其他帖子即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多