Created
November 20, 2019 12:22
-
-
Save Inzman/00983bd2385710767b17e04716195663 to your computer and use it in GitHub Desktop.
This code rewrites the slug of the attachment upon uploading to add a prefix (or suffix) to the slug. In that case you have less chance of getting in the way of page slugs in the future.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('add_attachment', function($postId) { | |
// get the attachment post object | |
$attachment = get_post($postId); | |
// get the slug for the attachment | |
$slug = $attachment->post_name; | |
// update the post data of the attachment with an edited slug | |
wp_update_post(array( | |
'ID' => $postId, | |
'post_name' => 'media-'.$slug, //adds a prefix | |
//'post_name' => $slug.'-photo', //adds a suffix | |
)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment