上载到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 );
    
        } 
    }

enter image description here

我相信这段代码行之有效,因为他将其重命名为属性发布类型 post-title.jpg,而在普通文章中,则将-1.jpg上载。.

我将需要帮助以添加一些说明

我必须错过告诉他的指示

else->如果没有标题并且不是post_type属性,则不执行任何操作。 因此他不会很烦人地将上传文件重命名为“ auto-draft.jpg”。如果不是CPT“属性”,则不执行任何操作=保留原始上传文件的名称。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)