-1

I have a multisite installation and I want to have a counter of the number of sites in the homepage of the main site, this counter must increase or decrease every time a new site is open or closed.

Not asking for the full code, I would love to do it by myself.

Just need some suggestions to understand where I have to start to do it.

Thank you in advance

Maxime
  • 8,645
  • 5
  • 50
  • 53
Michele Petraroli
  • 89
  • 1
  • 1
  • 11
  • 2
    I am voting to **reopen** this question. I understand that the similarity of the titles may lead people to think this is a duplicate, but, the other question and its answers are all about getting the list of sites which pull in the entire object of a site with its entire data, while this question is about a specific argument that can be set to grab only the count number and on top of that getting it through a hook. Therefore this is not a duplicate question. The titles sound similar but like the old saying goes "the devil is in the details"! Thanks for reviewing this! – Ruvee Oct 31 '21 at 15:47

1 Answers1

2

You could use get_sites function which takes many arguments one of which happens to be count argument which allows get_sites function to return a site count.

According to the official documentation, get_sites function returns the number of sites when 'count' is passed.

"this counter must increase or decrease every time a new site is open or closed."

Also in order to get a real-time counter you could use wp_head action hook! Which means every time that wp_head fires, which is on every page load, you get an update. Depending on how you would want to set this up, you could use ajax too which allows you to get an update without even refreshing/reloading the page.

So to start with, we could combine get_sites function, count argument and wp_head hook to get what you're looking for! Like this:

This code goes into the functions.php of your active theme.

add_action('wp_head', 'your_mutisite_counter', 99);

function your_mutisite_counter(){
    
    $args = array(
        'count' => true
    );
    
    $number_of_sites = get_sites( $args );

    echo $number_of_sites;
}

If you want to know more, I strongly suggest to go and read the documentation pages, specially when you scroll down the page, there are very good examples from community members and developers which could be very helpful.

get_sitesDocs

Also if you want to know what kind of arguments get_sites function accepts, in addition to count, here's the official documentation:

argumentsDocs

Hopefully all of this would give you a place to start with!!!

Ruvee
  • 8,611
  • 4
  • 18
  • 44