0

My custom meta box correctly saves my file upload. When the user wants to clear the field, they can click delete and I want to delete that meta data of the file URL only. How can I assign the meta_id correctly to the $meta_id variable in my function?

enter image description here

And I need to assign this meta_id, for example, to $meta_id

enter image description here

Here is the function creating this box as well as the save function.

function wp_custom_attachment() {

wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce');

$html = '<p class="description">';
    $html .= 'Upload your PDF here.';
$html .= '</p>';
$html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25" />';

$pdf = get_post_meta(get_the_ID(), 'wp_custom_attachment', true);

if($pdf['url']){
    $html .=  '<p style="overflow-wrap: break-word;">Current file: ' .$pdf['url'] . '</p>';
    } 

$delete_nonce = wp_create_nonce( 'delete-meta_' . $meta_id );
$html.=get_submit_button( __( 'Delete' ), "delete:wp_custom_attachment:meta-{$meta_id}::_ajax_nonce=$delete_nonce deletemeta", "deletemeta[{$meta_id}]", false, array( 'tabindex' => '6' ) );
$html.=wp_nonce_field( 'change-meta', '_ajax_nonce', false, false );

echo $html;

} // end wp_custom_attachment

function save_custom_meta_data($id) {

/* --- security verification --- */
if(!wp_verify_nonce($_POST['wp_custom_attachment_nonce'], plugin_basename(__FILE__))) {
  return $id;
} // end if

if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  return $id;
} // end if

if('page' == $_POST['post_type']) {
  if(!current_user_can('edit_page', $id)) {
    return $id;
  } // end if
} else {
    if(!current_user_can('edit_page', $id)) {
        return $id;
    } // end if
} // end if
/* - end security verification - */

// Make sure the file array isn't empty
if(!empty($_FILES['wp_custom_attachment']['name'])) {

    // Setup the array of supported file types. In this case, it's just PDF.
    $supported_types = array('application/pdf');

    // Get the file type of the upload
    $arr_file_type = wp_check_filetype(basename($_FILES['wp_custom_attachment']['name']));
    $uploaded_type = $arr_file_type['type'];

    // Check if the type is supported. If not, throw an error.
    if(in_array($uploaded_type, $supported_types)) {

        // Use the WordPress API to upload the file
        $upload = wp_upload_bits($_FILES['wp_custom_attachment']['name'], null, file_get_contents($_FILES['wp_custom_attachment']['tmp_name']));

        if(isset($upload['error']) && $upload['error'] != 0) {
            wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
        } else {
            update_post_meta($id, 'wp_custom_attachment', $upload);     
        } // end if/else

    } else {
        wp_die("The file type that you've uploaded is not a PDF.");
    } // end if/else

} // end if

} // end save_custom_meta_data
add_action('save_post', 'save_custom_meta_data');
  • Yes, you should combine the CRUD functions (Create, Read, Update, Delete) in your callback handler. – fuxia Jan 17 '16 at 6:01
  • Post your save handling function as well, please. – yivi Jan 17 '16 at 9:13
  • what's $meta_id in this context? it isn't initialized on this block of code. are you saving the uploaded as a regular attachment, or just a plain file upload? – yivi Jan 17 '16 at 9:31
  • @yivi I've added the save function above. I hope that helps. Thank you for having a look! – NotAnotherCliche Jan 17 '16 at 12:35
0

I found a function to get the Meta ID by Meta Key. This allowed me to correctly assign my $meta_ID variable, and now the delete ajax button works perfectly.

No need to update the save function at all.

function get_mid_by_key( $post_id, $meta_key ) {
  global $wpdb;
  $mid = $wpdb->get_var( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, $meta_key) );
  if( $mid != '' )
    return (int)$mid;

  return false;
}

function wp_custom_attachment() {

wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce');

$html = '<p class="description">';
    $html .= 'Upload your PDF here.';
$html .= '</p>';
$html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25" />';

$pdf = get_post_meta(get_the_ID(), 'wp_custom_attachment', true);

if($pdf['url']){
    $html .=  '<p style="overflow-wrap: break-word;">Current file: ' .$pdf['url'] . '</p>';
    } 

$meta_id=get_mid_by_key( get_the_ID(), 'wp_custom_attachment' );

$delete_nonce = wp_create_nonce( 'delete-meta_' . $meta_id );
$html.=get_submit_button( __( 'Delete' ), "delete:wp_custom_attachment:meta-{$meta_id}::_ajax_nonce=$delete_nonce deletemeta", "deletemeta[{$meta_id}]", false, array( 'tabindex' => '6' ) );
$html.=wp_nonce_field( 'change-meta', '_ajax_nonce', false, false );

echo $html;

} // end wp_custom_attachment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.