php – understand what code is doing when prepending /blog

Question

I am new to WordPress and php coding.

We are moving from a different container system to wordpress and to keep the links intact I need to prepend /blog to all post articles.

I tried it through the individual permalink using https://www.example.com/blog/%postname%/

It works nicely for the post articles, but I realized that it seems to also impact i.e. custom layouts from Astra, making the following url: https://www.example.com/blog/astra-advanced-hook/12345

Although I am not sure this is an issue, but being an orderly person I really don’t like such a strange URL convention.

So I searched the net and found the following reply from Zeth and Chaoste
Change permalinks for post type ‘post’ only

function wp1482371_custom_post_type_args( $args, $post_type ) {
    if ( $post_type == "post" ) {
        $args['rewrite'] = array(
            'slug' => 'blog'
        );
    }
    return $args;
}

add_filter( 'register_post_type_args', 'wp1482371_custom_post_type_args', 20, 2 );

function my_change_post_link($permalink, $post, $leavename) {   
    if (get_post_type($post) == 'post') {     
        return "/blog" . $permalink;   }   
    return $permalink; 
}

add_filter('pre_post_link', 'my_change_post_link', 10, 3);

I went on and created a child theme, posted the above to function php and it is working (when I posted just the first function and add_filter, there didn’t seem to be any change). But being an inquisitive person, I would really like to know what exactly this is doing?

I guess add_filter is what triggers the function when some action happens. The if checks if it is the post type.

1.) But what does the first function do and what does the second one do?

2.) Why do I need both functions?

3.) Plus somewhere I read something about slug changes and Pods, but honestly didn’t fully understand it. Would this change impact Pods?

Thank you for helping me learn!

0
Pir 6 months 2022-03-04T16:07:30-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse