Answers for "Redirect image attachment pages in Wordpress"

PHP
1

Redirect image attachment pages in Wordpress

function myprefix_redirect_attachment_page() {
	if ( is_attachment() ) {
		global $post;
		if ( $post && $post->post_parent ) {
			wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );
			exit;
		} else {
			wp_redirect( esc_url( home_url( '/' ) ), 301 );
			exit;
		}
	}
}
add_action( 'template_redirect', 'myprefix_redirect_attachment_page' );
Posted by: Guest on May-26-2021

Code answers related to "Redirect image attachment pages in Wordpress"

Browse Popular Code Answers by Language