首页 > 解决方案 > 上传到 CPT 时 Wordpress 自动重命名和填充属性

问题描述

如您所见,此代码可以按帖子标题重命名图像,效果很好。即使有多个标题相同的帖子。

它只是输入数字:image、image1、image2、image3 ..等

    /*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中的CPT属性之外,他将奇怪的empty-1.jpg重命名为uploads..结果:-1.jpg,-2.jpg,-3.jpg等.....

我需要帮助来添加一些说明

我必须错过一个告诉他的指令,它应该只在CPT 属性中触发

else --> 如果没有标题并且我们不在 CPT 属性中,则什么也不做。所以当没有标题集和图像首先上传时,他不会将上传重命名为“auto-draft.jpg” .. 非常烦人.. 如果我们在 CPT'property' 之外什么都不做 = 保留原始上传的名称并忽略这段代码。

在此处输入图像描述

在此处输入图像描述

标签: phpwordpressfunctionadd-filter

解决方案


我认为只需在函数中注释(或删除)这两行file_renamer()就可以了:

$my_image_title = $post;

$file['name'] = $my_image_title  . - uniqid() . $ext; // uniqid method

因为if ( get_post_type( $_REQUEST['post_id'] ) === 'property'){是检查是否post_id属于自定义帖子类型的行。根据我从您的解释中了解到的情况,您只想在那里进行重命名。如果您还想做其他事情,则可以在该 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;

  }     
}

顺便说一句,我没有测试代码!


推荐阅读