上载到CPT时的Wordpress自动重命名和填充属性 [英] Wordpress Auto Rename and Fill Attributes on upload to CPT
问题描述
如您所见,该代码可以按帖子标题重命名图像,效果很好.甚至有几个标题相同的帖子.
as you can see this code to rename images by post title works fine. even with several posts with the same title.
它只放置数字:image,image1,image2,image3等.
it just puts the numbers: image, image1, image2, image3 .. etc
/*Renaming attachment files to the post title*/
function file_renamer( $filename ) {
$info = pathinfo( $filename );
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$name = basename( $filename, $ext );
if ( get_post_type( $_REQUEST['post_id'] ) === 'property'){
if( $post_id = array_key_exists("post_id", $_POST) ? $_POST["post_id"] : null) {
if($post = get_post($post_id)) {
return $post->post_title . $ext;
}
}
$my_image_title = $post;
$file['name'] = $my_image_title . - uniqid() . $ext; // uniqid method
// $file['name'] = md5($name) . $ext; // md5 method
// $file['name'] = base64_encode($name) . $ext; // base64 method
return $filename;
}
}
add_filter( 'sanitize_file_name', 'file_renamer', 10, 1 );
/* Automatically set the image Title, Alt-Text, Caption & Description upon upload*/
add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
function my_set_image_meta_upon_image_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID ) ) {
// Get the parent post ID, if there is one
if( isset($_REQUEST['post_id']) ) {
$post_id = $_REQUEST['post_id'];
} else {
$post_id = false;
}
if ($post_id != false) {
$my_image_title = get_the_title($post_id);
} else {
$my_image_title = get_post( $post_ID )->post_title;
}
// Sanitize the title: remove hyphens, underscores & extra spaces:
$my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ', $my_image_title );
// Sanitize the title: capitalize first letter of every word (other letters lower case):
$my_image_title = ucwords( strtolower( $my_image_title ) );
// Create an array with the image meta (Title, Caption, Description) to be updated
// Note: comment out the Excerpt/Caption or Content/Description lines if not needed
$my_image_meta = array(
'ID' => $post_ID, // Specify the image (ID) to be updated
'post_title' => $my_image_title, // Set image Title to sanitized title
'post_excerpt' => $my_image_title, // Set image Caption (Excerpt) to sanitized title
'post_content' => $my_image_title, // Set image Description (Content) to sanitized title
);
// Set the image Alt-Text
update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );
// Set the image meta (e.g. Title, Excerpt, Content)
wp_update_post( $my_image_meta );
}
}
我相信此代码有效,因为内部在CPT属性中,他将我的上传重命名为 post-title.jpg
结果:post-title.jpg,post-title-1.jpg,post-title-2.jpg,post-title-3.jpg等.
I believe this code works since inside in CPT property he renames my upload to
post-title.jpg
result : post-title.jpg , post-title-1.jpg, post-title-2.jpg , post-title-3.jpg, etc....
和外部 CPT属性在wordpress中,他将奇怪的 empty-1.jpg 重命名为要上传的文件.结果:-1.jpg,-2.jpg,-3.jpg等.....
and outside CPT property in wordpress he rename strange empty-1.jpg to uploads.. result : -1.jpg , -2.jpg , -3.jpg etc .....
我将需要帮助以添加一些说明
I will need help to add some instructions
我必须错过一条告诉他的指令,它只能在 CPT属性中触发
I must miss an instruction to tell him ,it should only fire in CPT property only
else->如果没有标题并且我们不在CPT属性中,则不执行任何操作.因此,当未设置标题和图片先上传时,他不会将上传内容重命名为'auto-draft.jpg' ..非常烦人..如果我们不在CPT'property'范围内,则不执行任何操作=离开原始上传文件的名称,请忽略此代码.
else --> do nothing if there is no title and if we are not in CPT property. so he does not rename the uploads to 'auto-draft.jpg' when no title set and image uploaded first .. very annoying.. and if we are outside CPT'property' do nothing = leave the names of the original uploads and ignore this code.
推荐答案
我认为只需在您的 file_renamer()
函数中注释(或删除)这两行即可:
I think simply commenting (or removing) these two lines in your file_renamer()
function will do the trick:
$my_image_title = $post;
$file['name'] = $my_image_title . - uniqid() . $ext; // uniqid method
因为 if(get_post_type($ _REQUEST ['post_id'])==='属性'){
是检查 post_id
是否属于自定义行的行帖子类型.根据我从您的解释中了解的内容,您只想在此处重命名.如果您还想做其他事情,则可以在该IF条件下编写.
Because if ( get_post_type( $_REQUEST['post_id'] ) === 'property'){
is the line that checks whether the post_id
belong to a Custom Post Type. Based on what I understood from your explanation, you only want do the renaming there. If you want to do something else too, you could write inside that IF condition.
因此您的代码应如下所示:
So your code would look like this:
/*Renaming attachment files to the post title*/
function file_renamer( $filename ) {
$info = pathinfo( $filename );
$ext = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
$name = basename( $filename, $ext );
if ( get_post_type( $_REQUEST['post_id'] ) === 'property'){
if( $post_id = array_key_exists("post_id", $_POST) ? $_POST["post_id"] : null) {
if($post = get_post($post_id)) {
return $post->post_title . $ext;
}
}
//$my_image_title = $post;
//$file['name'] = $my_image_title . - uniqid() . $ext; // uniqid method
// $file['name'] = md5($name) . $ext; // md5 method
// $file['name'] = base64_encode($name) . $ext; // base64 method
return $filename;
}
}
顺便说一句,我没有测试代码!
Btw, I didn't tested the code!
这篇关于上载到CPT时的Wordpress自动重命名和填充属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!