0

I've used a plugin with the classic editor that enabled me to paste images directly from the clipboard, into the post. But only used this when the filename did not matter seo wise.

With the Gutenberg editor, this 'pasting' is built in and a handy feature at that. However, because of the auto generated filename, it is not a very Seo Friendly approach to add images to a post. But it is alot faster than saving, resizing, renaming, uploading and adding. Especially when alot of images are involved.

The only thing that would make this functionality the to-go way of adding images, is the filename. Replacing this part with a user provided name, would make all the difference in naming and posting images.

The following function should do the trick of renaming the attachment that is being pasted, just before it is saved:

add_action('add_attachment', 'rename_attachment');
function rename_attachment($post_ID){
  $new_attachment_name = array(
    'ID' => $post_ID, 
    'post_title' => $post_ID, // changes what you see 
    'post_name' => $post_ID // changes the slug to 89
  );

  wp_update_post($new_attachment_name);
}

i would like to replace $post_ID with either the current H2 name wp:heading i am pasting under, or have an inputbox in which i can input a filename i want to give the image.

How can i achieve this from here? would it be possible to hook/add_action and continue after user input? I was thinking of php throwing a simple html form and GET the value entered.

<form name="form" action="" method="get">
  <input type="text" name="pastedimagename" id="subject" value="Cat-Food-image">
</form>

<?php echo $_GET['pastedimagename']; ?>

Would this be moldable to a working workaround instead of writing a plugin. Since i don't have the skillset (yet) to achieve that..

-1

You could create a plugin for the editor to achieve that. In JS there's the onpaste event. These would be the basic steps:

  1. listen for onpaste.
  2. Find the closest heading element up the tree from the target element being pasted into.
  3. Default to page/post title if no heading elements are found and then back to the default naming convention if all else fails.
  4. Send the name you created based on that information back to server and trigger renaming the file with the PHP code you made.

creating an input box would be a little bit more indepth to do in gutenberg - but automatically naming them seems like it would be more ideal for you anyways.

3
  • I'm really not proficient enough to achieve that. I was thinking that maybe another php function could intercept the paste, and add user input before returning and finishing its action. Ill dive deeper into it..
    – RkdL
    Jan 22 '20 at 19:33
  • PHP is a server side, paste is client side.
    – Tim Elsass
    Jan 22 '20 at 19:55
  • Then maybe i need to re-phrase, but then i don't understand when the image gets uploaded to the server. Your comment confused me
    – RkdL
    Jan 22 '20 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.