File Renaming on Upload

neosavedme

New member
Sep 19, 2019
20
7
3
hello, is there any hope that someday someone will offer this


I really need to rename my images by post title on upload and automatically Autofill ALT tag. I've been looking for a while without giving up.
I even ended up trying to devlop my own snippet code by copying some codes I found on the web. it works well

but a few lines are missing to make it customizable
like specify in which post type, in my case i'ts (property)

in the two cases I will end up succeeding .. one day? ...

in the meantime I put all my work here

Code:
 /*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 );

        }
    }

as you can see in the picture it works but it is not complete

chaotic upload.png


I tried to specify that I want it to run in post type == property

without success

Code:
/*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 );



if someone knows php, or knows someone, or has an idea or even does not know anything about php, suggest anything, who can advance or create the same thing
or describe your experience with this problem, something that really works and tested by you.

the idea is to secure and personalize the code
ps: I even went to dig into the plugins files to try to understand some php functions without success..

thank you for your understanding and your time
 
Last edited:
  • Love
Reactions: OnlyJesusSaves

wikivb

New member
Banned User
Oct 30, 2019
15
3
3
Hi, you have reached the conclusion, let me know
Sugar
I am also looking for the same plugin and code
 
  • Wow
Reactions: OnlyJesusSaves

wikivb

New member
Banned User
Oct 30, 2019
15
3
3
  • Wow
Reactions: OnlyJesusSaves

neosavedme

New member
Sep 19, 2019
20
7
3
Thank you everyone
I have just discovered your messages, the latest update 1.2.0 has been released and not available yet here.

So you have to paste this code to be able to use the CPT rule while waiting for the ultimate version 1.2.0 Which includes this option requested from the author 😀

https://wpfactory.com/item/file-renaming-on-upload-wordpress-plugin/
1.2.0 - 2021/01/18
  • Add option to restrict file renaming by user role.
  • Add option to restrict renaming by parent post type.
  • Replace random string by wp_unique_filename() in case a file already exists.
  • Tested up to: 5.6


Code:
// Allows renaming only for 'property' post type

add_filter( 'frou_filename_allowed', function ( $validation ) {

    if (        empty( $allowed_cpts = array( 'property' ) )

        || empty( $post_id = \FROU\WordPress\Post::get_parent_post_id() )

        || in_array( get_post_type( $post_id ), $allowed_cpts )

    ) {

        return $validation;

    }

    $validation = false;

    return $validation;

} );
 
  • Wow
Reactions: OnlyJesusSaves

Forum statistics

Threads
69,246
Messages
908,560
Members
237,309
Latest member
gthghj

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu