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.


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)

How can i achieve the following result?:


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

3 Answers 3


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:


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 );


  • I was stuck on this for hours, Thank you so much sir, your answer is spot on.
    – Mark
    Commented Feb 18, 2019 at 21:44

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


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 and acknowledge you have read our privacy policy.

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