hooks - Custom filename when pasting an image from clipboard

I've used a plugin with the classic editor that enabled me to paste images directly from the clipboard, into the po

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..

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..

Share Improve this question edited Jan 22, 2020 at 20:19 RkdL asked Jan 20, 2020 at 14:46 RkdLRkdL 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744812072a4595116.html

相关推荐

  • hooks - Custom filename when pasting an image from clipboard

    I've used a plugin with the classic editor that enabled me to paste images directly from the clipboard, into the po

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信