Delete custom meta

时间:2016-01-17 作者:NotAnotherCliche

我的自定义元框正确保存了我的文件上载。当用户想要清除字段时,他们可以单击delete,我只想删除文件URL的元数据。如何将meta\\u id正确分配给$meta_id 函数中的变量?

enter image description here

我需要分配这个meta_id, 例如,到$meta_id

enter image description here

下面是创建此框的函数以及保存函数。

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\');

1 个回复
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

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在