• Resolved Priyanka

    (@dev_205)


    I have two setup – Single WP & Multisite WP Network

    I want to use the multisite’s wpmu_create_user function on the one custom file which is located on the root of single wp setup. I have tried below code but it throws Error establishing a database connection.

    <?php
    $wp_load_loc = “/opt/lampp/htdocs/mulsite/wp-load.php”;
    require_once($wp_load_loc);
    echo $wpdb->prefix;
    $user = wpmu_create_user(‘test@test.com’, ‘123456’, ‘test@test.com’);
    add_user_to_blog( 1, $newuser, ‘subscriber’ );
    add_user_to_blog( 2, $newuser, ‘subscriber’ );
    echo ‘Userid-‘ . $user;
    ?>

    I have also tried including ms-settings.php and ms-load.php but doesn’t work.

    After a lot of debugging, I have checked to include another single wp’s wp-load.php and it works (Below code)! So the issue seems to the multisite configuration which defined in the wp-config file.

    <?php
    ob_start();
    $wp_load_loc = “/opt/lampp/htdocs/singlewp/wp-load.php”;
    require_once($wp_load_loc);
    echo $wpdb->prefix;
    ?>

    I have spent many hours on such an issue. Kindly look into and please suggest the solution as soon as possible.

    Thanks in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    If you are trying to add a new user to the multisite installation from another site, don’t do it that way. Instead, use the multisite’s REST API or a custom Ajax or admin-post.php handler installed in multisite. You are required to be logged into the multisite, or use proper alternative authorization like OAuth.

    Thread Starter Priyanka

    (@dev_205)

    Hi @bcworkz

    Thanks for your reply!

    I have checked for the REST API and tried to create user using “/wp-json/wp/v2/users” but it throws “rest_cannot_create_user” – “Sorry, you are not allowed to create new users”

    After searching for such issue, have modified .htaccess on mulsite setup and tried with HTTPS but still, it can not create a user.

    I have followed below reference:

    REST API » Create a User
    REST API » Authentication

    Can you please help as I have not used WP REST API before and have no idea to use it in the non-wp project.

    Thanks!

    Moderator bcworkz

    (@bcworkz)

    POSTing an array of user data is straight forward enough. Your problem is almost certainly related to authentication. Another site’s code adding users cannot use the default cookie authentication, so you need an authentication plugin of some sort, as discussed on the authentication page.

    Once an authentication plugin is properly setup and you authenticate as a user with adequate capability to add users, POST the user data along with the required security token to the API and the user will be added.

    Thread Starter Priyanka

    (@dev_205)

    It has been fixed.
    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to use Multisite WordPress functions in a different Single WordPress’ is closed to new replies.