Skip to content

Commit

Permalink
Update disable attachment pages
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkla committed Nov 8, 2023
1 parent 8fe82d0 commit 3960da1
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions headache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Description: An easy-to-swallow painkiller plugin for WordPress.
* Author: Vincent Klaiber
* Author URI: https://github.com/vinkla
* Version: 3.2.1
* Version: 3.2.2
* Plugin URI: https://github.com/vinkla/headache
* GitHub Plugin URI: vinkla/headache
*/
Expand Down Expand Up @@ -47,9 +47,6 @@ function disable_feeds(): void
add_filter('xmlrpc_enabled', '__return_false');
add_filter('xmlrpc_methods', '__return_false');

// Disable attachment pages.
add_filter('pre_option_wp_attachment_pages_enabled', '__return_zero');

// Remove WordPress version.
remove_action('wp_head', 'wp_generator');

Expand Down Expand Up @@ -87,6 +84,9 @@ function disable_feeds(): void
remove_action('template_redirect', 'rest_output_link_header', 11);

// Remove emojis.
// WordPress 6.4 deprecated the use of print_emoji_styles() function, but it has
// been retained for backward compatibility purposes.
// https://make.wordpress.org/core/2023/10/17/replacing-hard-coded-style-tags-with-wp_add_inline_style/
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles');
Expand Down Expand Up @@ -206,10 +206,49 @@ function remove_roles(): void

add_action('init', __NAMESPACE__ . '\\remove_roles');

// Disable attachment template loading and redirect to 404.
// WordPress 6.4 introduced an update to disable attachment pages, but this
// implementation is not as robust as the current one.
// https://github.com/joppuyo/disable-media-pages/issues/41
// https://make.wordpress.org/core/2023/10/16/changes-to-attachment-pages/
function attachment_redirect_not_found(): void
{
if (is_attachment()) {
global $wp_query;
$wp_query->set_404();
status_header(404);
nocache_headers();
}
}

add_filter('template_redirect', __NAMESPACE__ . '\\attachment_redirect_not_found');

// Disable attachment canonical redirect links.
function disable_attachment_canonical_redirect_url(string $url): string
{
attachment_redirect_not_found();

return $url;
}

add_filter('redirect_canonical', __NAMESPACE__ . '\\disable_attachment_canonical_redirect_url', 0, 1);

// Disable attachment links.
function disable_attachment_link(string $url, int $id): string
{
if ($attachment_url = wp_get_attachment_url($id)) {
return $attachment_url;
}

return $url;
}

add_filter('attachment_link', __NAMESPACE__ . '\\disable_attachment_link', 10, 2);

// Discourage search engines from indexing in non-production environments.
function disable_indexing()
{
return wp_get_environment_type() === 'production' ? true : 0;
return wp_get_environment_type() === 'production' ? 1 : 0;
}

add_action('pre_option_blog_public', __NAMESPACE__ . '\\disable_indexing');

0 comments on commit 3960da1

Please sign in to comment.