This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*** | |
* Trash all post | |
**/ | |
add_action('wp_loaded','my_trash_all_posts'); | |
function my_trash_all_posts() { | |
if ( !current_user_can( 'edit_others_posts' ) ) | |
return; | |
$nonce = isset($_POST['nonce']) ? $_POST['nonce'] : ''; | |
if ( isset($_POST['trash_all']) && wp_verify_nonce($nonce, 'trash_all_posts') ) { | |
global $wpdb; | |
$post_type = isset($_POST['post_type']) ? $_POST['post_type'] : 'post'; | |
$result = $wpdb->query($wpdb->prepare(" | |
update {$wpdb->posts} | |
set post_status = 'trash' | |
where post_type = %s and post_status != 'trash'", | |
$post_type | |
)); | |
$json = json_encode(array( | |
'result' => $result, | |
)); | |
nocache_headers(); | |
header( 'Content-Type: application/json; charset=' . get_bloginfo('charset') ); | |
echo $json; | |
die(); | |
} | |
} | |
add_action('restrict_manage_posts', 'my_restrict_manage_posts_trash_all'); | |
function my_restrict_manage_posts_trash_all() { | |
if ( !current_user_can( 'edit_others_posts' ) ) | |
return; | |
$post_type = isset($_REQUEST['post_type']) ? $_REQUEST['post_type'] : 'post'; | |
$post_status = isset($_REQUEST['post_status']) ? $_REQUEST['post_status'] : ''; | |
if ( $post_status !== 'trash' ) { | |
?> | |
<script type="text/javascript"> | |
jQuery(function($){ | |
var $trash_all = $('<?php submit_button( '全ての投稿をゴミ箱に入れる', 'button-secondary apply', 'trash_all', false ); ?>'); | |
$('#post-query-submit').after($trash_all); | |
$trash_all.click(function(){ | |
$.ajax({ | |
url: '<?php echo admin_url("edit.php?post_type={$post_type}");?>', | |
type: 'POST', | |
data: { | |
trash_all: true, | |
post_type: '<?php echo $post_type; ?>', | |
nonce: '<?php echo wp_create_nonce('trash_all_posts'); ?>', | |
}, | |
success: function(){ | |
location.href = '<?php echo admin_url("edit.php?post_status=trash&post_type={$post_type}");?>'; | |
}, | |
}) | |
return false; | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} |
別名:自爆ボタン
本番運用前にテストデータを全部消し去りたい時とかに使おう。
コメントを残す