检查媒体库中是否已存在文件
我正在插件中创建自定义文件,并使用Wordpress Codex中针对wp_insert_attachment的代码将其添加到媒体库中。但是,我的插件有时会覆盖这些文件。我需要确保文件不会再次添加到媒体库中。这是当前代码: $wp_filetype = wp_check_filetype(basename($filename), null ); $wp_upload_dir = wp_upload_dir(); $attachment = array( 'guid' => $wp_upload_dir['baseurl'] . '/' . _wp_relative_upload_path( $filename ), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $filename); // you must first include the image.php file …