Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

wordpress redirect attachment page to file

function redirectAttachmentsToAssociatedFile()
{
    if (is_attachment()) {
        global $post;

        if ($post && $post->post_parent) {
            wp_redirect(esc_url(home_url(wp_get_attachment_url($post->ID))), 301);
            exit;
        } else {
            wp_redirect(esc_url(home_url('/')), 301);
            exit;
        }
    }
}

add_action('template_redirect', 'redirectAttachmentsToAssociatedFile');
Source by www.wpexplorer.com #
 
PREVIOUS NEXT
Tagged: #wordpress #redirect #attachment #page #file
ADD COMMENT
Topic
Name
5+9 =