WordPress Image Slugs Conflict
Prevent conflicts between page and image permalinks
Use a plugin: https://wordpress.org/plugins/disable-media-pages/ Based on this text: https://gschoppe.com/wordpress/disable-attachment-pages/
https://wordpress.stackexchange.com/questions/179162/stop-wordpress-from-reserving-slugs-for-media-items https://www.google.com/search?client=firefox-b-d&q=prevent+attachments+from+reserving+url+paths https://theseoframework.com/extensions/origin/ https://www.wpexplorer.com/disable-image-page/
Disable image URL’s
Explain: Disable image attachment links, stop WordPress from reserving slugs for media items
First of all, there is an option in Yoast SEO that tackles this problem. By turning it on here: SEO > Advanced > Permalinks > Redirect attachment URL’s to parent post URL every image URL attached to the post will redirect to the parent post. This will surely stop indexing WordPress attachment pages.
But I’m trying something different. I simply don’t want any kind of links or permalinks to exist, ever.
Don’t mix this with Insert Media
There is a option that tackles something else that can easily confused with what we want to achieve.
Whenever we insert new image into the post, there are options in Insert Media dialog box named Attachment Display Settings with values on Alignment, Link To and Size.
All three of these can be set as an option, and it will be in effect for all newly inserted images into posts.
You change these values using hidden /wp-admin/options.php page or by using code, as explained in this article.
One way is to change template file
Templates used in rendering images are, in this order: image.php, attachment.php, etc, as explained in Codex, and as seen in this example.
More flexible is inside plugin
More flexible solution would be to do the similar task, right from code.
Solution: http://wordpress.stackexchange.com/a/25147/74876
add_action( 'template_redirect', function () {
global $wp_query;
if ( is_attachment() ) {
/* Generate 404 */
$wp_query->set_404();
// wp_redirect( get_permalink( $wp_query->post->post_parent ), 301 );
}
});
I analyzed possibility to somehow modify permalink structure for attachments, but coudn’t find a way to solve it; maybe it’s even impossible.
I assumed it is possible as on custom post types, something like that should be possible, by playing with rewrite
array.
filter: attachment_link php - Remove attachment slug from url in wordpress - Stack Overflow
filter: post_type_link permalinks - How to rewrite URI of custom post type? - WordPress Development Stack Exchange Remove The Slugs from Custom Post Type URL
Disable media pages slug collisions
Slug collision occurs when the slug of an attachment or media accidentally clashes with that of a page. For some reason, WordPress now behaves as if it does not have slug-collision, but it does not matter because I do not like attachment pages to exist at all. Therefore, I must install one of these plugins in any case.
Disable Media Pages also disables attachment pages, but unlike other plugins, it displays media directly, which I prefer. It also has a convenient feature to “mangle” the media slug, just in case this plugin is ever disabled so that attachment URLs don’t interfere with posts. This is only in case the plugin is deactivated. Repo at joppuyo/disable-media-pages: WordPress plugin to disable “attachment” pages automatically created for WordPress media
Smart Attachment Page Remove is straightforward – when attempting to access an attachment page, it gives a 404 or other status.
Disable Media Pages kaže sledeće - Note for WordPress 6.4
WordPress 6.4 includes a new feature that allows you to disable attachment pages. However, this feature redirects attachment pages to the file URL instead of returning a 404 error. To completely disable attachment pages, you should use this plugin instead. The WP 6.4 feature also does not fix the issue where attachment pages reserve slugs for pages.