Search
 
SCRIPT & CODE EXAMPLE
 

PHP

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' );
Comment

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');
Comment

PREVIOUS NEXT
Code Example
Php :: laravel check if session variable exists 
Php :: php make array to certain length 
Php :: laravel string builder 
Php :: code php ajout heure 
Php :: empty table in laravel 
Php :: concat in laravel 8 
Php :: delete file laravel 8 
Php :: how to get video duration in php 
Php :: laravel remove public from url on shared host 
Php :: wp wc get cart item attribute 
Php :: laravel clear cache 
Php :: php get random value from array 
Php :: laravel php 8 ububtu 
Php :: laravel drop column if exists 
Php :: foreach loop in php 
Php :: seconds to days hours minutes seconds php 
Php :: Check if session exists or not in laravel 
Php :: get only date in laravel 
Php :: how to trim white space array in php 
Php :: datetime iso 8601 php 
Php :: laravel db::query update 
Php :: how to disable register route in laravel 
Php :: php function exists 
Php :: laravel validation max string length 
Php :: php remove charictors from a string 
Php :: while loop php 
Php :: get post by taxonomy 
Php :: blade set variable 
Php :: render vs redirect laravel exception 
Php :: php compare two versions return true or false if version is big 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =