understand what code is doing when prepending /blog

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.