我正在将上载的附件存储到自定义目录下wp-content/uploads/my-directory/
并将其价值存储在wp_postmeta
桌子当我删除帖子时,并不是从路径中删除附加文件。
所以我试着抓住delete_post
钩子从文件路径中删除文件:
<?php
/**
* Delete Custom Attachment when deleting its post.
*
* @param integer $post_id Post ID.
*
* @return void
*/
function wpse353942_delete_attachment( $post_id ) {
if ( \'my-cpt\' !== get_post_type( $post_id ) ) {
return;
}
$attachment = get_post_meta( $post_id, \'_attachment\', true ); // get the attachment URL.
if ( ! $attachment ) {
return;
}
$upload_dir = wp_get_upload_dir();
unlink( $upload_dir[\'basedir\'] . \'/my-directory/\' . basename( $attachment ) );
}
add_action( \'delete_post\', \'wpse353942_delete_attachment\' );
但遗憾的是,这并不是从目录中删除文件(
我能做什么?