• Hello,

    I have added this function in order to rewrite the permalink structure of my attachments :

    add_filter( 'attachment_link', 'wpd_attachment_link', 20, 2 );
    function wpd_attachment_link( $link, $attachment_id ){
    
        $attachment = get_post( $attachment_id );
    
        // Only for attachments actually attached to a parent post
        if( ! empty( $attachment->post_parent ) ) {
    
            $parent_link = get_permalink( $attachment->post_parent );
            // make the link compatible with permalink settings with or without "/" at the end
            $parent_link = rtrim( $parent_link, "/" );
            $link =  $parent_link . '/gallerie/' . $attachment_id;
    
        }
    
        echo $link;
    
    }
    
    add_action( 'init', function() {
    
        // Tell WordPress how to handle the new structure
        add_rewrite_rule( '(.+)/gallerie/([0-9]{1,})/?$', 'index.php?attachment_id=$matches[2]', 'top' );
    
    } );

    The problem I encounter is that the displayed content is an array, not the image. Since the permalink is working, I wonder why wordpress is unable to display the image.

    Any idea?

    Regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    There’s something odd about how your installation is processing index.php?attachment_id=$matches[2]. It should display the attachment page. You might try index.php?p=$matches[2] instead to see if that makes any difference. It probably won’t help, but is worth a try.

    There’s some sort of conflict somewhere. Please deactivate all of your plugins and switch to a default twenty* theme. Your problem should be corrected. Switch back to your theme, then activate your plugins one by one. After each change, test for the problem. Once it recurs, the last activated entity is the culprit.

    One other thing. This wouldn’t influence what you’re seeing, but you shouldn’t echo the filter result, you should return it for the normal process outside your callback to echo it. With this particular filter it probably makes no difference at all, but it will be a problem with many other filters.

    Thread Starter moxymore

    (@moxymore)

    Thank you again bcworkz for your answer,

    I took some times to figured out what was the issue, but I can’t find it. The complex structure of my website with a lot of custom taxonomies which impacts directly the url structure makes it really hard to set it up correctly.

    I have opted for another solution, just correct me if I have wrong : I have configured my SEO plugin to automatically redirect all attachment post to its parent. I have, per security, put a no-index for all attachment.

    I think it’s the best solution for me, because my website will never use these links. The only benefit they give me for the frontend are for captions and descriptions which can be shown in my lightbox.

    Regards.

    Moderator bcworkz

    (@bcworkz)

    That would be fine if it works for you. Rewrite rules can be very difficult to debug even without a complex site structure. Better to avoid them where possible ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Attachment posts showing an array (issue | url rewriting)’ is closed to new replies.