Skip to content

Instantly share code, notes, and snippets.

@Inzman
Created November 20, 2019 12:22
Show Gist options
  • Save Inzman/00983bd2385710767b17e04716195663 to your computer and use it in GitHub Desktop.
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.
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