echang
echang

Reputation: 3

How to change the attachment url in Wordpress

What I'm trying to do is change the link to the main attachment page in Wordpress. Basically, I'm trying to change the word attachment to media.

I'm trying to change:

example.com/parent-category/child-category/post-slug/attachment/attachment-name/

to:

example.com/parent-category/child-category/post-slug/media/attachment-name/

Thanks in advance for any help on this.

Upvotes: 0

Views: 8183

Answers (1)

TheDeadMedic
TheDeadMedic

Reputation: 9997

I know this is an old question, but I just stumbled on it and thought it was worth a shot;

function __filter_rewrite_rules( $rules )
{
    $_rules = array();
    foreach ( $rules as $rule => $rewrite )
        $_rules[ str_replace( 'attachment/', 'media/', $rule  ) ] = $rewrite;
    return $_rules;
}
add_filter( 'rewrite_rules_array', '__filter_rewrite_rules' );

function __filter_attachment_link( $link )
{
    return preg_replace( '#attachment/(.+)$#', 'media/$1', $link );
}
add_filter( 'attachment_link', '__filter_attachment_link' );

Upvotes: 3

Related Questions