The add_attachment
action is called after the attachment has been named. If you only want to change the name of the attachment (and the slug), you don’t need to change the name of the file and can just replace the code inside rename_attachment
with a call to wp_update_post
.
add_action('add_attachment', 'rename_attacment');
function rename_attacment($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);
}
Related Posts:
- upload featured image from front end using wordpress add media button
- Too many connections to server
- Removal of Media Gallery pictures
- How to get the last selected media alignment setting?
- unable to write to the database while uploading images
- Disable Media uploader to users
- WordPress Media URLs?
- Inserting Media on New Post But Hides Previous Uploads for Editor/Contributor
- What is the proper use of guid for images in wp_posts?
- ZIP up all images displayed in a [gallery] and offer as download link
- Changing the post date and time with function
- Get current post id in functions.php
- Dynamically add id to heading tags
- media_handle_upload for local files?
- How to upload post thumbnail while wp_insert_post?
- Getting the post_id in wp_ajax function
- How do I detach images from posts?
- How to publish a post with empty title and empty content?
- Replacing all attachment links in post with media file link
- How to call function within a page/post, to dynamically generate content?
- How do I use element instead of tags in WordPress post content having webP support?
- Guest Author – How to modify my custom function code if the guest author URL will follow a particular pattern/format?
- Human Time Diff, change mins to minutes
- How to get new post URL?
- Force update all posts in custom post type, no content changes
- Disable REST API for a user ROLE
- How to display post tags
- How to Display Post View Count
- Remove double space after a period
- Add confirmation popup on “Move to Trash”
- $post object is null
- post_parent is 0 when uploading new files
- How to change the page break URL from ../post/2 to …/post/page2/
- Easy way to delete 70k posts and attached media?
- How to get future ID for post which haven’t been created yet?
- Run a function when post is deleted?
- Getting current post ID in functions.php
- Add icon/badge next to posttitle of specific category
- Can I hide certain upload folders in media library [duplicate]
- Load post attached images on a single page site with fancybox
- Exclude category from
- Restrict media upload size by format
- Setting posts_per_page for taxonomy term template
- Change post order random through out the entire WordPress
- Prevent Delete Attachment by URL or When Submit
- Can’t get buddypress notifications in front-end; why do I get this error?
- How do I display some posts fully on the homepage, and some as an excerpt only?
- Automatically attach image to post if they have the same title
- Send post id through ajax and get the post content back
- Force authors to Preview a post before publishing
- Change all author links in Blog roll
- disable Tab post on nav-menus page (Admin)
- Adding bootstrap classes to video shortcodes
- Every second post different class in blog view
- Format latest/newest post differently
- is_page Funtion for Posts ?
- Get post meta retrieving wrong value
- The_excerpt() doesn’t parse – how to change that?
- How to only publish posts with image in it
- Display a mixed list of posts and media
- Using system date format
- Is it advisable to use $post->comment_count instead of get_comments_number( $post_id )
- saving/reading custom field value does not work – no value gets POSTed
- Inserting Media to WordPress Posts
- Internal linking to posts permalink fail because of spaces and stripe at postname
- Help on conditional statement to accompany wp_insert_post function please?
- How to implement a WordPress comments function?
- How to add content above footer in posts from specific category
- Delete Post but retain image of post [WP REST API]
- Site/Post preview is not working on all social media
- how to show comments only author which send own posts in wordpress
- shortcode // get posts by ids
- Alt text attributes not showing over portfolio images
- How to add HTML into error message
- Getting image data from phone camera (MediaDevices.getUserMedia) into user post
- Find most used words in post titles
- $post->post_content empty while all other properties are correct
- Replace Tag Keyword With Link Within Post Content
- Why does media_handle_sideload upload to last month’s folder?
- AJAX load more posts not using correct category and repeating the same few posts
- How to correctly escape data
- Zip all (original) images from media gallery
- how to create other sizes of original image in one go wordpress uploads
- How to change number of posts shown on homepage vs other pages?
- Embed button for post screen
- Get post title by Alphabet
- Inserting image into post content – Front End
- Bulk Attach Or Insert Media To Post Without Opening Post Editor
- How to add audio media in a post?
- How can I create a menu items from meta box based on users input
- How to use wp_editor(); in functions.php then retrieve content in the template
- List direct children of page
- save_post not working
- Display a custom field rating system in the front end
- wp trim function not working
- Ajax calls in wordpress
- Custom Post-Rename Function Does Not Function in WordPress 6.x
- Show Next/Previous without Link
- Filters do not work when there are multiple (one works)
- Optimize blog that serves hundred of images and videos