0

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?

Thank you for helping me learn!

1
  • you would need to contact Pods support to get the answer to your 3rd question, 3rd party plugin support questions are offtopic here. I've edited it out so that your question doesn't get close votes as a 3rd party support question
    – Tom J Nowell
    Commented Mar 8, 2022 at 1:32

1 Answer 1

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

When you register a post type, this filter is called, and wp1482371_custom_post_type_args is using this opportunity to modify the parameters for the post post type to change the slug

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

Just because your posts are now available at a new URL, doesn't mean WordPress knows this and will change the permalinks! It's not psychic, it has to be told explicitly or the URLs shown in the user interface won't match and will still be missing the /blog part. pre_post_link gives you an opportunity to filter them, and if it's a post it add /blog to the beginning.

2.) Why do I need both functions?

Because if you just change the posts URL that doesn't mean WP knows what to do with /blog URLs.

And if you only add /blog URLs that doesn't mean WP knows to put /blog in front of every posts URL.


As an aside, just because you found this particular solution, does not mean it is the best solution. There are solutions that don't involve WordPress at all and sit deeper down at the Apache/Nginx level, you can also 301 redirect to versions that don't have /blog, as well as many other options.

13
  • Thanks Tom. I am probably still missing part of the understanding. How come WP does not know where the post is with the first function? It is telling it, to add the slug "blog". Is it not stating: hey WP, you post is now under blog/somepostname? WP just needs to check, if the slug is empty or not, no?
    – Pir
    Commented Mar 8, 2022 at 11:19
  • And what if I wrote in the first function slug=blog and in the second prepended "/somethingelsethanblog"? What would the result be? Telling the system twice about the "same" just seems like a potential place for typos and errors. (I do not know the system, I am just trying to learn. Thank you for supporting me.)
    – Pir
    Commented Mar 8, 2022 at 11:21
  • where can I read about the other options please? Is there some kind of a comparison of the different possibilities (pro/con) and how to execute them?
    – Pir
    Commented Mar 8, 2022 at 11:24
  • There is no comprehensive list of other options, and it's highly unlikely you will see a full list with comparisons and a pros/cons list. I think you're assuming that there is an interconnected system that tells WP when this changes, that too changes, and that's not the case. Remember WP is not a sentient AI, you're asking why it doesn't do X Y Z, and the answer is because it does not do X Y Z, somebody has to build that and nobody has built that.
    – Tom J Nowell
    Commented Mar 8, 2022 at 14:09
  • URL becomes query vars, query vars go into WP_Query, WP_Query fetches posts, WP takes posts and query vars and picks a template, template loads and displays the posts. That's how you get from a URL to a page in the browser. Notice how deciding what the URL of a post is does not feature in that list, they are separate systems. Just because you've enabled a post to be visited at the URL /bananas does not mean the canonical URL of that post is now /bananas, that's a leap of logic, an expectation, these things need to be implemented, and it has not been implemented
    – Tom J Nowell
    Commented Mar 8, 2022 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.