doukengzi3517 2015-05-25 20:39
浏览 42

WordPress删除附件脚本

I'd like to delete "alternate post" (Apróhirdetések menu) attachment. When I click "Empty trash" I need delete all alternate post and alternate post attachment delete.

I have two half solution.

  1. I have script. It delete "alternate post" (Apróhirdetések menu) but not delete the attachment. (But I need delete attachment.)

When I click this button, then run script.

Button code:

<button data-toggle="tooltip" data-placement="top" title="<?php _e('Delete','theme-name');?>" onclick="javascript:cs_delete_directory_post('<?php echo esc_js(admin_url('admin-ajax.php'));?>','<?php echo esc_js($post->ID);?>')" type="button" class="tolbtn close close-<?php echo intval($post->ID); ?>" data-dismiss="alert"><em class="icon-trash-o"></em></button>

The script:

function cs_delete_directory_post(admin_url, post_id){
if(confirm('Delete Post')){
        "use strict";
         var dataString = 'post_id=' + post_id+'&action=cs_delete_directory_post';
         jQuery(".close-"+post_id).html('<i style="color:#fe9909;" class="icon-spinner8 icon-spin"></i>');
            jQuery.ajax({
                type:"POST",
                url: admin_url,
                data: dataString,
                success:function(response){
                    jQuery(".attachment-thumbnail").remove();
                    jQuery(".post-"+post_id).remove();
                }
        });
}

return false;
}
  1. I hava verry usefull plugin. This plugin delete attachment when I click "Empty Trash", BUT it only works simple (blog) post. Not work "alternate" post. (picture) But I' like to work, when I clcik "alternate" post "Empty Trash".

Plugin:

function wpse_188427_delete_post_media( $post_id ) {
$attachments = get_posts(
    array(
        'post_type'      => 'attachment',
        'posts_per_page' => -1,
        'post_status'    => 'any',
        'post_parent'    => $post_id,
    )
);

foreach ( $attachments as $attachment ) {
    wp_delete_attachment( $attachment->ID );
}
}
add_action( 'delete_post', 'wpse_188427_delete_post_media' );
add_action( 'wp_trash_post', 'wpse_188427_delete_post_media' );

Need any more informations? Big thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 linux驱动,linux应用,多线程