How do I use wordpress outside of wp itself?

I decided to write a small (or large) series of articles with regards to wordpress.
The first thing that was a little difficult to find information on Google and what you had to get to yourself was:
How to use wordpress functions outside of wordpress itself?
For those who are especially quick and do not want to read tons of letters:
You just need to connect the wp-load.php script, which is located in the root of the wordpress.
For the rest, who are interested in more detail, read on.

Let’s say you have some kind of file that is outside of WordPress itself. This can be a separate samopisny site, a forum, or just a regular authorization script on some site – anything.
I’ll try to show using an authorization form as an example somewhere outside the WordPress

Task:
– There is a form with two fields for login and password, a checkbox and a button for entering. It is necessary to authorize the user in the wordpress system

First you need to create the form:

<?php
 //Place for PHP authorization code
?>
<form action="" method="POST">
	<p>Login: <input type="text" name="log" /></p>
	<p>Password: <input type="text" name="pwd" /></p>
	<p><input type="checkbox" id="rememberme" name="rememberme" /><label for="rememberme">Remember me</label></p>
	<input type="submit" value="Log in" />
</form>

 

Names of the fields are just such, because the wp-function wp_signon, which we will use as an example, can pull out the login, password and memorization directly from the post. Of course, you can transfer credentials yourself if you get them from somewhere else. But we won’t do it now 🙂
If interested, then welcome to the spoiler

How to transfer non-post credentials

The wp_signon function can take two parameters. One of them is an array with data, and the second is whether to use protective cookies. Default Falls (false)
Those. you can pass them something like this:

$login = 'super_login';
$pass = 'super_pass';
$remember = true;
$arrgs = array(
    'log'        =&amp;gt; $login,
    'pwd'        =&amp;gt; $pass,
    'rememberme' =&amp;gt; $remember
);
$user = wp_signon($arrgs, false);
//If authorization fails, then:
if ( is_wp_error($user) ) {
	echo $user-&gt;get_error_message();
}

[collapse]

Let’s add authorization to the remaining place for the PHP code and, in case of successful authorization, redirect the user to some page. For example, the main wordpress itself)

So, in place for the PHP code, we need to connect wp-load.php from the root of WordPress. Let’s say the wordpress is in the wp folder, and our authorization script is in the root folder.

It turns out something like this:

if (isset($_POST)) { //Check if $_POST has isset. If yes, then:
	include "wp/wp-load.php";
	$user = wp_signon();
	//If authorization fails, then:
	if ( is_wp_error($user) ) {
	    echo $user->get_error_message();
	} else { //If authorization success, then:
	    header("location: wp/"); //redirect main page wp
	    exit();
	}
}

The final version:

<?php
if (isset($_POST)) { //Check if $_POST has isset. If yes, then:
	include "wp/wp-load.php";
	$user = wp_signon();
	//If authorization fails, then:
	if ( is_wp_error($user) ) {
	    echo $user->get_error_message();
	} else { //If authorization success, then:
	    header("location: wp/"); //redirect main page wp
	    exit();
	}
}
?>
<form action="" method="POST">
	<p>Login: <input type="text" name="log" /></p>
	<p>Password: <input type="text" name="pwd" /></p>
	<p><input type="checkbox" id="rememberme" name="rememberme" /><label for="rememberme">Remember me</label></p>
	<input type="submit" value="Log in" />
</form>

As you can see, everything is very simple. If you have questions, then write in the comments)