0

How to get current logged in user in Wordpress Multisite outside of it?

I tried the method for Wordpress Singlesite and it give the error Call to a member function set_prefix() on a non-object on code line $wpdb->set_prefix( $table_prefix, false ); location wp-includes/ms-settings.php:196.

I think there should be a way to do it properly without exporting a bunch of functions from Wordpress.

Piece of code ($root = ABSPATH . '/sf/app'):

    $root = $this->container->getParameter('kernel.root_dir');
    require_once(realpath($root . '/../../wp-load.php'));
6
  • I don't know what you mean by 'outside' of it? Are you trying to implement an AJAX endpoint? If so you should never use a standalone file, use the WP AJAX api instead and this problem vanishes. Standalone form and ajax files are major security holes – Tom J Nowell Feb 16 '15 at 13:32
  • I have to use wordpress login system in Symfony framework. The login should be made on Wordpress pages. – Marin Bînzari Feb 16 '15 at 13:34
  • That sounds cumbersome, is there not a WP auth bundle? Your question essentially boils down to "How do I bootstrap/load WordPress in an external script?" – Tom J Nowell Feb 16 '15 at 14:16
  • @TomJNowell, yes, it is. – Marin Bînzari Feb 16 '15 at 14:22
  • Can you provide a more comprehensive example of what you've done so far? Not everybody here knows Symfony and incomplete snippets don't provide a lot of context, it might help – Tom J Nowell Feb 16 '15 at 14:24
0

Have you tried including the wp-load.php file in your external script ?

require_once("/path/to/wordpress/wp-load.php");
7
  • Yes I tried this. And it gives the error Call to a member function set_prefix() on a non-object. – Marin Bînzari Feb 16 '15 at 13:34
  • Could you please paste here the code you're using to include the wp-load.php file ? Or any other relevant code, it's hard to help you without some samples of what you've done so far :) – cristian.raiber Feb 16 '15 at 13:39
  • I updated my question. $root . '/../../' is my wordpress folder. – Marin Bînzari Feb 16 '15 at 14:06
  • 1
    You can't use the $wpdb object without declaring global $wpdb – Tom J Nowell Feb 16 '15 at 14:23
  • @TomJNowell, strange, but it helped. You can write an anwer and I'll accept it. – Marin Bînzari Feb 16 '15 at 15:13

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.