PHP’s cURL library can be used to connect and login to your WordPress dashboard. This can be used for automation purposes. To connect from an external location into your WordPress dashboard, use the code below.
You just need to amend the variables:
- $login_user
- $login_pass
- $login_url
- $visit_url
[php]
<?php
function curl_get_wp_login( $login_user, $login_pass, $login_url, $visit_url, $http_agent, $cookie_file ){
if( !function_exists( ‘curl_init’ ) || ! function_exists( ‘curl_exec’ )){
$m = "cUrl is not vailable in you PHP server.";
echo $m;
}
// Preparing postdata for wordpress login
$data = "log=". $login_user ."&pwd=" . $login_pass . "&wp-submit=Log%20In&redirect_to=" . $visit_url;
// Intialize cURL
$ch = curl_init();
// Url to use
curl_setopt( $ch, CURLOPT_URL, $login_url );
// Set the cookies for the login in a cookie file.
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie_file );
// Set SSL to false
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
// User agent
curl_setopt( $ch, CURLOPT_USERAGENT, $http_agent );
// Maximum time cURL will wait for get response. in seconds
curl_setopt( $ch, CURLOPT_TIMEOUT, 60 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
// Return or echo the execution
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
// Set Http referer.
curl_setopt( $ch, CURLOPT_REFERER, $login_url );
// Post fields to the login url
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt( $ch, CURLOPT_POST, 1);
// Save the return in a variable
$content = curl_exec ($ch);
/*
** if you need to visit another url, you can do it here.
** curl_setopt( $ch, CURLOPT_URL, ‘a new url address or a file download url’ );
** $content = curl_exec ($ch);
*/
// Close the cURL.
curl_close( $ch );
// You can echo or return the page data here.
echo $content;
}
// Username for login
$login_user = "admin";
/*
** Password for this username
*/
$login_pass = "admin";
/*
** Login url address.
*/
$login_url = "http://localhost/wp/wp30/wp-login.php";
/*
** Which page you want to visit after login.
** WordPress redirect their user automatically after login to this page
** if you do not assign a visit page,
** then the result for this login will return ‘1’.
** That means you have logged in successfully.
** Visit url is ipmportant to get the content.
*/
$visit_url = urlencode( ‘http://localhost/wp/wp30/wp-admin’ );
/*
** Cookie vaiable
*/
$cookie_file = "/cookie.txt";
/*
** Set HTTP user agent.
*/
$http_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
// Test the call
curl_get_wp_login( $login_user, $login_pass, $login_url, $visit_url, $http_agent, $cookie_file );
?>
[/php]
If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed.
11 Responses
When i run cURL code from site A, Then open Site B in same browser in second tab, User cannot seem logged in in site B.
What is the issue?
Hello my family member! I want to say that this post is awesome, great written and come with
approximately all vital infos. I would like to look extra posts like this .
Actually this wasn’t working on localhost, but now this now logs me in but takes me to the blog’s landing page rather than to the http://www.blog.com/wp-admin/ page. Could it be because the wordpress install is on a subdomain?
This gets me onto the login page and fills in the username and password fields but it doesn’t submit the form, please help.
Thanks man. It works!
$ckfile = dirname(__FILE__) . “/cookie.txt”;
$cookie_file = fopen($ckfile, ‘w’) or die(‘Derp…open…nooooooooo!’);
Nice! ^^ Yeah an example of how to post without using XMLRPC would be great!!
Sweet! Can you also make an example of how to submit a post with cURL without using XMLRPC?
Hi
Can you give me more description the `COOKIEJAR`, i don’t know the way to do..
give me example clearly…
Sr for my english 🙂
Surender, first make sure your `COOKIEJAR` exists and is writable (the way it’s provided in the example `$cookie_file = “/cookie.txt”;` makes it unusable on *nix systems where / is the root directory which should not be writable by your PHP process), or createable. You will then have to always load the cookie jar for subsequent requests otherwise WordPress loses track of you without those cookies.
Hi Jean,
Thanks for sharing your code.
Its works fine for me and I am able to login to my wordpress dashboard but after that none of the link works. when I click on any of the link, like “edit profile”, it redirects me to the wordpress login page.
Can you shed some light here?
Kind Regards
Surender