7

If my permalink structure is set to /blog/%postname%/, how can I retrieve a URL for my site which retrieves domain.com/blog/?

With the permalink structure mentioned above, when using site_url();, it displays domain.com/ when I'm looking to easily get the site's URL including the front base.

Maybe I'm unclear about what the "front base" actually is.

I'm under the impression that the front base is your "Permalink Structure". (Stored in the option permalink_structure)

I've tried home_url(); and site_url(); ... Both output just domain.com/

I'm thinking about just using site_url(); then retrieving the option permalink_structure, parsing the contents of the option myself, then appending it to the site_url(); function.

$permalink_structure = get_option('permalink_structure');
$front_base = preg_replace('#(\%)(.*)(\%/)#si', '', $permalink_structure);
echo site_url($front_base, 'http').PHP_EOL;

Is there an easier way to get the site's URL including the front base?

3 Answers 3

12

You can get the value of front in the global $wp_rewrite:

global $wp_rewrite;
echo $wp_rewrite->front;
// or
echo home_url( $wp_rewrite->front );

Though that is probably of limited use, as the front base isn't necessarily an existing page, and may 404 in many cases. If you're using that value to prepend to other URLs, you're likely doing it wrong.

If you want the page designated as the posts page, that value is stored in the page_for_posts option:

echo get_permalink( get_option( 'page_for_posts' ) );
4

I was about to suggest something like the function bellow, but lets face it @Milo's answer is just a bit smarter, so just for completeness reason and because I already wrote it down.

function wpse163163_site_url_with_fix_permalink_base() {
    return 
        get_site_url()
        . '/'
        . preg_replace(
            '/\%([a-z0-9]+?)\%/i',
            '',
            str_replace(
                '/',
                '',
                get_option('permalink_structure')
            )
        )
        . '/';
}
3
  • Had basically the same solution in mind, but could not formulate it :-) Oct 1, 2014 at 16:36
  • TBH I pretty much had it in store from a different but similar use case. @PieterGoosen Oct 1, 2014 at 16:38
  • Little bit of cheating never hurt anyone :-) Enjoy Oct 1, 2014 at 16:39
0

You will have to get link like this.

<?php echo home_url( '/blog' ); ?>

or

<?php echo home_url( '/' ) . 'blog'; ?>
4
  • Well, yeah. Ideally that would be nice if you knew what the front base was. What if you don't know what the front base is? AKA working in a plugin, which the front base could be different on several sites using the plugin. That's why I was going to retrieve the contents of permalink_structure option, and parse it myself, then append to home_url(); or site_url();. I was just curious if there was an easier way. Oct 1, 2014 at 15:39
  • Then there is not way. Either install WordPress in domain.com/blog folder.
    – Robert hue
    Oct 1, 2014 at 15:42
  • 1
    Also why do you need to get front base? Most of the time, if you need to include files then you can use ABSPATH.
    – Robert hue
    Oct 1, 2014 at 15:54
  • I have a plugin which needs to know the post type URLs and taxonomy URLs. As you know, post types and taxonomies can have the front base appended. Oct 1, 2014 at 16:42

Your Answer

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

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