Excluir meta personalizada

A minha meta checkbox personalizada corretamente guarda meu upload de arquivos. Quando o usuário deseja limpar o campo, eles podem clicar em excluir e eu quero excluir apenas os meta dados do URL do arquivo. Como posso atribuir o meta_id corretamente à variável $meta_id na minha function?

insira a descrição da imagem aqui

E eu preciso atribuir este meta_id , por exemplo, para $meta_id

insira a descrição da imagem aqui

Aqui está a function que cria esta checkbox, bem como a function de salvar.

 function wp_custom_attachment() { wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce'); $html = '

'; $html .= 'Upload your PDF here.'; $html .= '

'; $html .= ''; $pdf = get_post_meta(get_the_ID(), 'wp_custom_attachment', true); if($pdf['url']){ $html .= '

Current file: ' .$pdf['url'] . '

'; } $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');

Solutions Collecting From Web of "Excluir meta personalizada"

Encontrei uma function para obter o Meta ID pela Meta Key . Isso me permitiu atribuir corretamente a minha variável $ meta_ID, e agora o botão ajax de exclusão funciona perfeitamente.

Não é necessário atualizar a function de salvar.

 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 = '

'; $html .= 'Upload your PDF here.'; $html .= '

'; $html .= ''; $pdf = get_post_meta(get_the_ID(), 'wp_custom_attachment', true); if($pdf['url']){ $html .= '

Current file: ' .$pdf['url'] . '

'; } $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