$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How to call plugin function per site in a multisite?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How to call plugin function per site in a multisite?

matteradmin7PV0评论

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, fruits and people

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
fruits
people

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

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, fruits and people

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
fruits
people

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

Share Improve this question edited Feb 18, 2019 at 6:31 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 18, 2019 at 5:17 MarkMark 132 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

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

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/?trigger_my_func sub1.exmaple/?trigger_my_func or exmaple/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.

Post a comment

comment list (0)

  1. No comments so far