Php Wordpress在上传到CPT时自动重命名和填充属性
正如您所看到的,这段代码可以通过post title重命名图像。 即使有几篇文章标题相同 它只放了数字:图像,图像1,图像2,图像3。。等Php Wordpress在上传到CPT时自动重命名和填充属性,php,wordpress,function,add-filter,Php,Wordpress,Function,Add Filter,正如您所看到的,这段代码可以通过post title重命名图像。 即使有几篇文章标题相同 它只放了数字:图像,图像1,图像2,图像3。。等 /*Renaming attachment files to the post title*/ function file_renamer( $filename ) { $info = pathinfo( $filename ); $ext = empty( $info['extension'] ) ? '' : '.' . $in
/*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等 在wordpress中,他将奇怪的empty-1.jpg重命名为上传。。 结果:-1.jpg、-2.jpg、-3.jpg等 我需要帮助来添加一些说明 我必须错过一个指示告诉他,它只能在CPT属性中开火 否则-->如果没有标题且我们不在CPT属性中,则不执行任何操作。 因此,当没有标题集和图片先上传时,他不会将上传的内容重命名为“auto-draft.jpg”。。很烦人。。如果我们在CPT'property'之外,则什么也不做=保留原始上传的名称并忽略此代码 我认为只要在
文件\u renamer()
函数中注释(或删除)这两行就可以了:
$my_image_title = $post;
$file['name'] = $my_image_title . - uniqid() . $ext; // uniqid method
因为if(get_post_type($\u REQUEST['post_id'])=='property'){
是检查post_id
是否属于自定义post类型的行。根据我从您的解释中了解的情况,您只想在那里重命名。如果您还想做其他事情,您可以在该if条件中写入
因此,您的代码如下所示:
/*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;
}
}
顺便说一句,我没有测试代码!我认为只需在文件中注释(或删除)这两行即可:
$my_image_title = $post;
$file['name'] = $my_image_title . - uniqid() . $ext; // uniqid method
因为if(get_post_type($\u REQUEST['post_id'])=='property'){
是检查post_id
是否属于自定义post类型的行。根据我从您的解释中了解的情况,您只想在那里重命名。如果您还想做其他事情,您可以在该if条件中写入
因此,您的代码如下所示:
/*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;
}
}
顺便说一句,我没有测试代码!谢谢Akhilesh给你的消息,我刚刚测试过,我仍然有同样的问题,我更新了帖子以便你能看到我的错误谢谢Akhilesh给你的消息,我刚刚测试过,我仍然有同样的问题,我更新了帖子以便你能看到我的错误