C
C
Cesare Borgia2014-02-12 18:56:59
PHP
Cesare Borgia, 2014-02-12 18:56:59

How to make CNC for sitename/?attachment_id=1 in Wordpress?

There are pages of attached files, they build links sitename.com/?attachment_id=1 in such a way, where 1 is the id of the attached file.
It is necessary that instead of such a link there should be a link like sitename.com/%name of the attached file%
Do not ask why this is necessary, I myself am at a loss.
For all other links, the NC is configured, but I can’t figure out exactly what to do with the page of the attached file ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cesare Borgia, 2014-02-13
@Borgia

In general, I made a crutch yesterday. In /wp-includes/link-template.php in the last if of the get_attachment_link function added $link = home_url( 'attachment/' . $object->post_name );
Now everything works almost as intended, but instead of sitename/%name of the attached file% there is sitename/attachment/%name of the attached file%
It would be possible to remove /attachment/ through .htaccess, but so that later there would be no conflict if there is a post and a file with the same name, decided to leave attachment.
True, as soon as Wordpress is updated, everything will fly off, if anyone has a more beautiful solution, I would be glad to see it.

M
Max, 2014-02-12
@AloneCoder

This is what you can add to functions.php

add_action('generate_rewrite_rules', 'attachment_rewrite_rule_14924');

function attachment_rewrite_rule_14924($wp_rewrite){
  $new_rules = array();
  $new_rules['attachment/(\d*)$'] = 'index.php?attachment_id=$matches[1]';
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

moped is not mine

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question