Rename the images in wordpress on upload for a specific post type

99封情书 提交于 2020-12-13 03:13:01

问题


as you can see this code to rename images by post title works fine. even with several posts with the same title.

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( $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 );
    
        } 
    }

the only problem, I have to specify in which post type it should run.

in my case it's post type 'property' so he does not do anything anywhere..

how to add that?

someone told me that $ requests are not secure ...

since your code is not secure (gets $ _REQUEST directly with no sanitizing).

I think he was talking about that right?

and also it will be nice if I have the possibility to add to the file name a date or a suffix [height] x [width] to personalize it (optional choices)

ex: wordpress new office 2048x2048.jpg or wordpress new office 12-11-2020.jpg

someone could help me to improve this piece of code ?

update 13.11.20 after a long search I tried this

if (is_singular ('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;
}
}

}

no luck it doesn't work, I will try again and again to fix this code until someone understands what is going on here

update 13.11.20

according to this post the thing is get_post_type so i tried it doesn't work i can't understand the logic for the moment

/*Renaming attachment files to the post title*/
function rename_attacment($post_ID){
    
   if ( get_post_type( $_REQUEST['post_id'] ) === 'property') {

    $info = pathinfo( $filename );
    $ext  = empty( $info['extension'] ) ? '' : '.' . $info['extension'];
    $name = basename( $filename, $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 );

来源:https://stackoverflow.com/questions/64811111/rename-the-images-in-wordpress-on-upload-for-a-specific-post-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!