Search code examples
wordpresswordpress-themingcustom-wordpress-pages

How to insert an attachment and update a custom field in a post type in Wordpress


I have a custom post type named ['notifications'] with a custom field named ['attachment'] for all posts in ['notifications'] .

  • I want user to upload an attachment into the library from the front end
  • If upload is successful
  • Get the filename of the attachment
  • Then update a post in custom post type ['notifications'] by its id
  • Update custom field ['attachment'] in a post to filename

The meta_key for the custom field is ['_ct_text_57fc8ec4573cd']

This is what I have so far

FOR THE FRONT END

<?php
   if (isset($_POST['upload'])) {
      if (!empty($_FILES)) {
         $file = $_FILES['file'];
         $attachment_id = upload_user_file($file);
      }
   }
?>

<form action="" enctype="multipart/form-data" method="post">
   <input name="file" type="file">
</form>

INSIDE FUNCTIONS.PHP

function upload_user_file($file = array()) {

   require_once(ABSPATH. 'wp-admin/includes/admin.php'); 

   $file_return = wp_handle_upload($file, array('test_form' => false));

   if (isset($file_return['error']) || isset($file_return['upload_error_handler'])) {
       return false;
   } else {

       $filename = $file_return['file'];

       $post_ID_attachment = 33;

       $attachment = array('post_mime_type' => $file_return['type'],
           'post_title' => $post_ID_attachment, 
           'post_content' => '',
           'post_status' => 'inherit', 
           'guid' => $file_return['url']
       );

       $attachment_id = wp_insert_attachment($attachment, $file_return['url']);

       require_once(ABSPATH. 'wp-admin/includes/image.php');

       $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);

       wp_update_attachment_metadata($attachment_id, $attachment_data);

       if (0 < intval($attachment_id)) {
           return $attachment_id;
       }

       /* UPDATE ATTACHMENT BELOW*/
       update_post_meta($post_ID_attachment, '_ct_text_57fc8ec4573cd', $filename);
   }
      return false;
}

Not sure if I am doing it correctly. The code above inserts attachment successfully but it is not updating the custom field in post type ['notifications']


Solution

  • You have a return statement in your function, before update_post_meta query. Try following code:

    function upload_user_file($file = array()) {
    
        require_once(ABSPATH. 'wp-admin/includes/admin.php'); 
    
        $file_return = wp_handle_upload($file, array('test_form' => false));
    
        if (isset($file_return['error']) || isset($file_return['upload_error_handler'])) {
           return false;
        } else {
    
            $filename = $file_return['file'];
    
            $post_ID_attachment = 33;
    
            $attachment = array('post_mime_type' => $file_return['type'],
              'post_title' => $post_ID_attachment, 
              'post_content' => '',
              'post_status' => 'inherit', 
              'guid' => $file_return['url']
           );
    
           $attachment_id = wp_insert_attachment($attachment, $file_return['url']);
    
           require_once(ABSPATH. 'wp-admin/includes/image.php');
    
           $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
    
           wp_update_attachment_metadata($attachment_id, $attachment_data);
    
           /* UPDATE ATTACHMENT BELOW*/
           update_post_meta($post_ID_attachment, '_ct_text_57fc8ec4573cd', $filename);
    
           if (0 < intval($attachment_id)) {
               return $attachment_id;
           }
       }
       return false;
    }
    

    Also I'm not sure you need these require_once-s in your code, since it in function.php, everything should be loaded.