---
description: https://www.rafaelcardero.com/media/wordpress-customize-attachment-url-src-dl/
---

# How to Customize the URL of Attachments in WordPress (ok)

### Study

C:\xampp82\htdocs\wp2\wp-content\themes\twentyseventeen\functions.php

```
function ns_add_rewrite_rules($rules){
	$dirname = 'media';
	$custom_rules = array("{$dirname}/(.?.+?)/?$" => 'index.php?attachment=$matches[1]');
	return array_merge($custom_rules, $rules);
}
add_filter('rewrite_rules_array', 'ns_add_rewrite_rules', 10, 1);
/*
* Adds the 'media' directory to the URL of an attachment.
*/
function ns_normalize_attachment_url($url, $post_ID){
	$dirname = 'media';
	$post = get_post($post_ID);
	return $post ? sprintf('%s/%s/%s/', home_url(), $dirname, $post->post_name) : $url;
}
add_filter('attachment_link', 'ns_normalize_attachment_url', 10, 2);
/*
* Adds the '-dl' suffix to the name of an attachment.
*/
function ns_normalize_attachment_name($data, $postarr){
	$suffix = "-" . get_the_ID();
	$has_suffix = substr($data['post_name'], -strlen($suffix)) === $suffix;
	$add_suffix = isset($postarr['ID']) && !$has_suffix;
	if($add_suffix){
			$desired_name = $data['post_name'] . $suffix;
			$data['post_name'] = wp_unique_post_slug($desired_name, $postarr['ID'], $data['post_status'], $data['post_type'], $data['post_parent']);
	}
	return $data; 
}
add_filter('wp_insert_attachment_data', 'ns_normalize_attachment_name', 10, 2);
```

Trước

<figure><img src="https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2F5y95r3VPWzZaf3YQE8TS%2Fimage.png?alt=media&#x26;token=e93f6d30-e15c-49fb-baa9-ee3978827523" alt=""><figcaption></figcaption></figure>

Sau

<figure><img src="https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2FdtLcgaJzSD08G20Z5SW3%2Fimage.png?alt=media&#x26;token=f803bbb7-63f9-4388-925b-b84a23b2f695" alt=""><figcaption></figcaption></figure>

### Example

<figure><img src="https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2FsAHb3K1WhJBLeiBjENat%2Fimage.png?alt=media&#x26;token=b42c66d4-d4e4-46ae-a44f-a8d98e4267e4" alt=""><figcaption></figcaption></figure>

C:\xampp82\htdocs\wp3\wp-content\themes\gutener-child\attachment.php

```php
<?php
/**
 * The template for displaying all attachments
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
 *
 * @package Gutener
 */
get_header();
?>
<div id="content" class="site-content">
	<div class="container">
		<section class="wrap-detail-page">
			<div class="row">
				<div id="primary" class="content-area col-lg-8">
					<main id="main" class="site-main">
						<h1 class="page-title entry-title"><?php single_post_title("Media: "); ?></h1>
						<?php
						while (have_posts()) : the_post();
							get_template_part('template-parts/content', 'attachment');
						endwhile; // End of the loop.
						?>
					</main><!-- #main -->
				</div><!-- #primary -->
				<?php
				if (is_active_sidebar('right-sidebar')) { ?>
					<div id="secondary" class="sidebar right-sidebar col-lg-4">
						<?php dynamic_sidebar('right-sidebar'); ?>
					</div>
				<?php }
				?>
			</div>
		</section>
	</div><!-- #container -->
</div><!-- #content -->
<?php
get_footer();

```

C:\xampp82\htdocs\wp3\wp-content\themes\gutener-child\template-parts\content-attachment.php

```php
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<div class="entry-container">
		<div class="entry-content">
			<?php
			$idi = get_the_ID();
			$attachmenti = wp_get_attachment_image_src($idi, 'full');
			$attachmentm = wp_get_attachment_metadata($idi);
			$attachment = !empty($attachmentm) ? $attachmentm['image_meta']['caption'] || $attachmentm['image_meta']['credit'] : "";
			if (!empty($attachmenti)) echo '<img class="attachment__image" src="' . $attachmenti[0] . '" alt="' . $attachment . '">';
			the_content();
			?>
		</div><!-- .entry-content -->
		<div class="entry-meta footer-meta">
			<?php gutener_entry_footer(); ?>
		</div><!-- .entry-meta -->
	</div><!-- .entry-container -->
</article><!-- #post-<?php the_ID(); ?> -->
```
