By MikeBau

2019-07-12 09:45:10 8 Comments

Let's say I want to add the permalink for the URLs:{postname} and the 'section' matches the name of one post's category (just for one case). After having a moment for study the documentation. I added a function for the rewrite_url. In the 'functions.php' file of my template (which is Astra). For what I add at the end:

add_action( 'init', 'wpa_rewriterules' ); 

function wpa_rewriterules() 
    // The regex to match the incoming URL 
    // The resulting internal URL: `index.php` because we still use WordPress 
    'top' ); 


I have also read that I would have to add a filter to 'the_permalink' in the same way, so that each time a link has to be displayed it will be transformed to the desired URL. So I added the code for that filter also in the same 'functions.php' file:

add_filter('the_permalink', 'post_permalink_w_seccion'); 

function post_permalink_w_seccion( $link ) { 
    global $post; 
    $postcat = get_the_category( $post->ID ); 
    if ( $post->post_type === 'post' && $postcat->slug == 'section') { 

        $link = str_replace( $post->post_name, 'section/' . $post->post_name,  get_permalink( $post ) ); 
    return $link;  

but this is what does not work for me. The urls do not transform or change. Can you help me to see what I'm doing wrong or what I'm missing?

Also, I'm not sure if this is the best way to do what I want. I'm new to the Wordpress world.

P.S. I have always saved changes to the 'permalinks' doing a flush of the rules of URLs.

Thank you.


@nmr 2019-07-12 14:50:43

Function get_the_category() returns array, that is why the condition $postcat->slug == 'section' is always false.

In its current form, your filter can change links to posts that do not have the section category. You should change the function to check the post for which get_permalink() was called, instead of checking the global $post.

add_filter( 'post_link', 'post_permalink_w_section', 10, 2 ); 

function post_permalink_w_section( $link, $post )
    if ( $post->post_type === 'post' && has_category('section', $post) )
        $link = str_replace( $post->post_name, 'section/' . $post->post_name, $link ); 
    return $link;  

@MikeBau 2019-07-12 15:35:17

Yes, it was a typo (edited). But it does not work either. I have added a sentence die('HI!'); at the beginning of the filter function and it only executes in the view article/post action (I saw it at bottom so at the end), but not in a page with blocks of posts, for example. where the links must be well formed for all of those posts.

@nmr 2019-07-12 16:04:45

@MikeBau My oversight. The more appropriate filter will be post_link or pre_post_link applied by get_permalink() function. add_filter( 'post_link', 'post_permalink_w_section', 10, 2 );

@MikeBau 2019-07-12 16:29:50

OK thanks!, now it works fine with add_filter (' post_link ',' post_permalink_w_section ', 10, 2); And finally, before closing the question I would like to know if this is the best way to do this. Thank you.

@nmr 2019-07-12 16:43:09

@MikeBau It's a good way, from what I know.

@MikeBau 2019-07-12 17:10:54

Excellent, thanks. In addition I will add a redirect 301 so that there are no duplicate urls, for the case in which someone tries to access the post without including the category in the url.

Related Questions

Sponsored Content

1 Answered Questions

WP get_permalink Return Wrong URL

2 Answered Questions

the_permalink on the homepage vs posts for sharing links

1 Answered Questions

2 Answered Questions

[SOLVED] the_permalink displays post ID URL for future posts

  • 2015-07-19 04:09:33
  • FredHead
  • 2367 View
  • 4 Score
  • 2 Answer
  • Tags:   permalinks

1 Answered Questions

[SOLVED] Page Permalinks: Custom URLs

1 Answered Questions

[SOLVED] the_permalink() leads back to homepage

1 Answered Questions

[SOLVED] Image Attachment Url Rewrite

1 Answered Questions

Sponsored Content