1

My goal is to be able to call a plugin function per site and of course it has different result based on the site data.

e.g.

I have a plugin called: sample-plugin.php

Inside it has a function called:

function sp_echo_site()
{
   echo get_site_url();
}

I have a multisite inside has 3 sites: e.g. animals.com, fruits.com and people.com

And in a network level, I wanted to call the sp_echo_site() function.

I wanted to do the following loop, however of course it doesn't work, How can I make this work?

foreach (get_sites() as $site)
{
   $site->sp_echo_site();
}

How can i achieve the following result?:

animals.com
fruits.com
people.com

Is this possible? Or do I have to go to database? Or any other alternative methods?

1

You're almost there...

If you have this in your plugin:

function sp_echo_site() {
   echo get_site_url();
}

Then you call this function as this:

sp_echo_site();

And this line will run this function in the context of current site.

So you'll have to do something like this:

if ( function_exists( 'get_sites' ) ) {
    foreach ( get_sites() as $site ) {
        switch_to_blog( $site->blog_id );

        sp_echo_site();

        restore_current_blog();
    }
}
  • I was stuck on this for hours, Thank you so much sir, your answer is spot on. – Mark Feb 18 at 21:44
0

Mark, to achieve the following result, you have to change your foreach loop. Try this code:

foreach (get_sites() as $site) {
  echo $site->__get('siteurl');
}

EDIT: Wordpress reference

-1

Yes, that's an option OR you can do this exmaple.com/?trigger_my_func sub1.exmaple.com/?trigger_my_func or exmaple.com/sub1/?trigger_my_func by triggering a method when accessing a subsite within a network it should run in the context of that site and output site specific information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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