我试图编写以下规则:
附件上传到wp admin/post时。php页面,在上传时,将其重命名为{post-slug}-{n}-[WxH].{ext}
{site-name}-{n}-[WxH].{ext}其中{n}
是数字顺序,[WxH]
WordPress和{ext}
其扩展。
项目1我得到了add_attachment
钩如何实现项目2?如何在上传行为中更改媒体页面上传文件的名称?
我试图编写以下规则:
附件上传到wp admin/post时。php页面,在上传时,将其重命名为{post-slug}-{n}-[WxH].{ext}
{site-name}-{n}-[WxH].{ext}其中{n}
是数字顺序,[WxH]
WordPress和{ext}
其扩展。
项目1我得到了add_attachment
钩如何实现项目2?如何在上传行为中更改媒体页面上传文件的名称?
最好的方法可能是在每个帖子上附加一个自定义字段,以保持增量{n}。
大意是:
$attach_inc = 1;
if ( is_single() ) {
if ( $attach_inc = get_post_meta( $post_id, \'attachment_inc\', true ) ) {
$attach_inc++;
update_post_meta( $post_id, \'attachment_inc\', $attach_inc );
} else {
add_post_meta( $post_id, \'attachment_inc\', $attach_inc );
}
} else {
if ( $attach_inc = get_option( \'attachment_inc\' ) ) {
$attach_inc++;
set_option( \'attachment_inc\', $attach_inc );
} else {
add_option( \'attachment_inc\', $attach_inc );
}
}
// DO YOUR ADD ATTACHMENT STUFF HERE
我想你可以更改文件名wp_handle_upload_prefilter. 虽然我不知道你将如何确定这是一个帖子上传还是媒体上传,也许你可以查看$post global。
如何从媒体库中删除已从上载文件夹中删除的图像?