1

In my plugin, in init action I use get_sites() function... however it throws:

Call to undefined function get_sites() in XYZ on line 154 whats solution?

p.s.
1) The same happens with wp_get_sites()...
2) no help when I use require_once(ABSPATH . 'wp-includes/ms-blogs.php');
3) I use wordpress version 4.6

  • 1
    What version of WordPress is being used? Where is get_sites() being called (which hook)? – Dave Romsey Nov 17 '16 at 8:11
  • in init as i said ( version 4.6). I doubt problem is that the function is called within non MULTI-SITE site.... – T.Todua Nov 17 '16 at 8:13
1

Solution seems to be:

if(function_exists('get_sites'))         {$sites= get_sites();} 
elseif(function_exists('wp_get_sites'))  {$sites= wp_get_sites();} 
| improve this answer | |
  • 1
    This will not work properly, as the deprecated function still exists in WP 4.6+. Better to test for the WP version (also see my answer below). – Rick Hellewell Jan 4 '18 at 19:57
  • If the deprecated function still exists, then it's usable. When it is not available any more, then it will stop working and the alternative will be chosen. – Mike Kormendy Mar 28 '19 at 14:10
0

The wp_get_sites function was deprecated in WP 4.6. The full answer is the first answer here: Sorting list of sites from multisite network using wp_get_sites .

Also, the function provided by Lance Cleveland (in the answers in the above link) provides a test for the proper function to use. But it is incomplete, as the old function is deprecated so the old function still exists if pre-4.6. The test should look for the WP version, not the existence of the function. (See my answer to the same question.)

I have a plugin for multisites that looped through the sites. The pre-4.6 code was causing problems on 4.6+ site, so changed the plugin's requirements to require WP 4.6 or later. The plugin automatically deactivates with a message if pre-4.6 is found.

That said, keeping WP updated is strongly recommended. There are security risks (and compatibility issues) with not doing updates.

| improve this answer | |
  • Testing for the version is generally the wrong way to go about detecting which function to use. A simple search on the web will show that the concept of object detection is superior over version detection for many different frameworks, programming languages, and implementations. Proven many times over. – Mike Kormendy Mar 28 '19 at 14:08
  • But I didn't recommend checking the version of the browser or JS, as your links discuss. I said to check the WP version, which will tell you which method to use. Checking for the existence of the function will not fully work in all instances, as the function will still be there in 4.6+, but won't work right. So checking for the WP version is the best way, IMHO. – Rick Hellewell Mar 28 '19 at 17:40
  • Again, I was referring to the concept not the languages or the frameworks. – Mike Kormendy Mar 28 '19 at 20:51
0

The get_sites() function (replacing the wp_get_sites() function, deprecated) only works in a context of a network of sites.

You can create a network by using the multisite feature, following those instructions in the Codex.

| improve this answer | |

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.