1

i want to change the address of the file attachment link to wordpress [example: http://www.sitename.com/category/sub-category/article.html/attachment/image etc.]. i did research on the subject a few days, but could not get any results..

looking at the structure of the connection! after ".html", "/" mark does not look very nice to come. also the "attachment" as the fixing of that section, for WordPress users of different languages are missing a very large. i mean; rewrite.php file links to the author for the "author" from the base. this too, edit with the help of a simple function;

global $wp_rewrite;
$wp_rewrite->author_base = 'example';
$wp_rewrite->flush_rules();

i can not for this "attachment" :/ i tried so hard but i could not deal with this problem...

anyway, i wanted to ask you!

i'm not a coder (i'm a graphic designer). i want to do, so maybe things can make the limits of logic (: image.php file as it is sufficient to open a url more sense to me ...

this link; http://www.sitename.com/category/sub-category/article.html/attachment/image

whether such; http://www.sitename.com/category/sub-category/article.html?file=image

i do not know would not open the image.php file in the form of "?file=image". so i can carry out your suggestions.

thank you in advance.

regards, Metin METE

4
  • I asked a similar question, where the new URLs are of the form /media/[attachment name]/. Is that also good for you?
    – Jan Fabry
    Dec 8, 2010 at 7:46
  • @Jan: i saw, i tried but "page not found" error was giving.. i want to be relevant in article a certain attachments to the fact that. i want to change the above example, as the most suitable.
    – Metin METE
    Dec 8, 2010 at 8:47
  • Did you flush the rewrite rules after you added that code? You can do that by visiting the Permalinks page.
    – Jan Fabry
    Dec 8, 2010 at 12:52
  • @Jan: yes, but i do not work..
    – Metin METE
    Dec 8, 2010 at 16:28

1 Answer 1

2

Actually, you can always just use a query parameter, even if you have the "pretty" permalinks enabled. So instead of /category/sub-category/article.html/attachment/image you can go to /?attachment=image. The only case when this "breaks down" is when you go to /category/sub-category/article.html?attachment=image, because WordPress gets confused: it tries to query for a post and an attachment at the same time. However, this is very simple to handle: check for the request, and if it is an attachment remove the other parameters.

add_action( 'parse_request', 'wpse5015_parse_request' );
function wpse5015_parse_request( $wp )
{
    if ( array_key_exists( 'attachment', $wp->query_vars ) ) {
        unset( $wp->query_vars['name'] );
        unset( $wp->query_vars['year'] );
        unset( $wp->query_vars['monthnum'] );
    }
}

Now you only have to change the generated attachment URLs. Since they are almost in the correct format, we can just so some search and replace:

add_filter( 'attachment_link', 'wpse5015_attachment_link', 10, 2 );
function wpse5015_attachment_link( $link, $id )
{
    // If the attachment name is numeric, this is added to avoid page number conflicts
    $link = str_replace( 'attachment/', '', $link );
    $attachment = get_post( $id );
    $link = str_replace( '/' . $attachment->post_name . '/', '?attachment=' . $attachment->post_name, $link );
    return $link;
}
5
  • i want to also not give such results (article.html?file=image etc.) but, thank you so so so much for the explanation and sample code..
    – Metin METE
    Dec 8, 2010 at 22:46
  • @Metin: I'm sorry, I don't think I understand you. What form do you want the attachment links to have? Almost anything is possible, but it should include the name or ID of the attachment. You can also replace the ?attachment= part with ?file=, but it takes (a little bit) more code.
    – Jan Fabry
    Dec 9, 2010 at 7:37
  • now; the output of the code you gave (post.html/image) is going this way. in this code ($link = str_replace('/' . $attachment->post_name . '/'...), we want to find at the end of the attachment file name "/" sign by deleting, i want to change the way this has become (post.html?attachment=image) but image.php file in this way does not open. just change the link to the article, the article opens up again.
    – Metin METE
    Dec 9, 2010 at 10:38
  • @Metin: It seems you use a non-standard permalink structure, because your post links end in .html. This probably confuses my str_replace(). The first thing you must get to work is the parsing of the new url, by checking the parse_request hook. If you then have a structure that you like, edit the attachment_link hook to re-format your URLs like that. Since this depends on your specific local configuration, you will have to figure it out yourself.
    – Jan Fabry
    Dec 9, 2010 at 11:28
  • i'm so sorry. i accidentally deleted the database. i installed the new Wordpress and worked flawlessly. but, i do not understand how it works :D anyway, thank you again Jan.
    – Metin METE
    Dec 9, 2010 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.