我的自定义元框正确保存了我的文件上载。当用户想要清除字段时,他们可以单击delete,我只想删除文件URL的元数据。如何将meta\\u id正确分配给$meta_id
函数中的变量?
我需要分配这个
meta_id
, 例如,到
$meta_id
下面是创建此框的函数以及保存函数。
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\');
SO网友:NotAnotherCliche
我发现a function to get the Meta ID by Meta Key. 这允许我正确地分配$meta\\u ID变量,现在删除ajax按钮工作得很好。
根本不需要更新save函数。
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