Add word to permalinks does not transform the urls

by user171729   Last Updated July 12, 2019 10:08 AM - source

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.

Related Questions

How Do I get the author's page id?

Updated May 27, 2015 01:03 AM

Add custom post type name in term link?

Updated February 26, 2017 23:08 PM

Dynamically populate permalinks

Updated July 11, 2015 15:03 PM