首先,让我们看看delete_attachment
在堆芯中点火。这是在网上发生的5062
属于wp-includes/post.php
在wp_delete_attachment 作用以下是相关片段:
/**
* Fires before an attachment is deleted, at the start of wp_delete_attachment().
*
* @since 2.0.0
*
* @param int $post_id Attachment ID.
*/
do_action( \'delete_attachment\', $post_id );
wp_delete_object_term_relationships($post_id, array(\'category\', \'post_tag\'));
wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type));
我们可以看到WordPress core正在调用
do_action
- 此方法不关心返回的内容;不像
apply_filters
,
do_action
只需触发一个钩子,然后不管返回什么值,处理都将继续。进一步了解
wp_delete_attachment
, 似乎没有办法“短路”此过程并阻止删除附件。
Except, 从技术上讲,您可以使用PHPdie
或exit
语句以结束脚本的处理,这将有效防止文件被删除。
function action_maybe_delete( $id ) {
echo "Let\'s not delete Attachment id: " . $id ;
// prevent the attachment from actually being deleted
if( 1 == 1 ) { // made up condition
die; // Prevent the script from continuing.
}
};
// add the action
add_action( \'delete_attachment\', \'action_maybe_delete\', 10, 1 ); ?php