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 :: send email to no register user in laravel 
Php :: rebuild joomla menu 
Php :: union type php does not work 
Php :: laravel validate form data unique array 
Php :: php default argument 
Php :: PHP quoted_printable_decode — Convert a quoted-printable string to an 8 bit string 
Php :: Laravel 9 localization not working on live server 
Php :: WooCommerce quantity field to ajax add to cart button archive page 
Php :: how to use db more than 1 codeigiter 3 
Php :: symfony dump request headers 
Php :: php array to query string using array map 
Php :: Detecting specifically the My account "Dashboard" page 
Php :: how to make custom sub menu admin wordpress dev 
Php :: install tinymce php 
Php :: escape class wordpress 
Php :: php async curl request 
Php :: jobs laravel 
Php :: woocommerce subscriptions custom user rolde 
Php :: debugger not installed phpstorm 
Php :: php years 
Php :: getting key from env returns null laravel 
Php :: php array_walk_recursive 
Php :: oneliner if php 
Php :: ring create an RSA key from PEM encoded string 
Php :: Dropzone Attachment Required is not working 
Php :: How to Validate an Email Duplicate Entry in Codeigniter 
Php :: data showing in single option instead of multiple option from json array 
Php :: php edit user profile 
Php :: php exponential equation 
Php :: Add Payment Type to WooCommerce Admin Email 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =