• Resolved Raul P.

    (@alignak)


    Hi,

    As the title says, I am creating an advanced-cache.php drop-in and I would like to detect the current blog id with get_current_blog_id(), which will always return 1 because it’s to early.

    I see that is_multisite() works, but this doesn’t tell me which one is it.
    So I was wondering, if I can perhaps include a few files and detect which blog ID am I running based on the url?

    I also see that require_wp_db() can be used to connect to the database earlier, but how do I find the current blog id from advanced-cache.php

    Or is it possible to perhaps define a constant after is_multisite() and then postpone serving a cache file until later?

    Where is the earliest hook I can get the current site id, send the cache file and end execution? Any example or snippet to print the site id as string and end execution right there without loading anything else?

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Raul P.

    (@alignak)

    Currently, what I am thinking about is to do something like this:

    – check if it’s a subdomain or subdirectory install
    – connect to database and query the wp_blogs table for the blog_id, domain and path.
    – for subdirectories, get the current uri path, get the first directory and compare it to the domain and path, thus returning the blog_id.
    – for subdomains, comparing it to the domain and return the blog_id

    Just wondering if there is a better way.
    Thanks again

    Moderator bcworkz

    (@bcworkz)

    WP should be considered unstable prior to the “init” action. There might be an earlier hook where the current blog has been determined. Look through wp-settings.php and try a few of the actions you can see applied there. One of them might be late enough. But if you can wait for “init”, all the better.

    While you could indirectly discern the blog ID from the requested URL, it’s better to wait until WP does this for you if you can.

    Thread Starter Raul P.

    (@alignak)

    Thanks, I’ll stick to the url way, as the init is too late for a cache plugin.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘get_current_blog_id() on advanced-cache.php’ is closed to new replies.