Login to WordPress Dashboard via cURL

Written by Jean Galea
Written by Jean Galea
PHP's cURL library can be used to connect and login to your WordPress dashboard. This can be used for automation purposes. Let's find out how to do this in today's code hacking session.

Partner Sponsors


Full disclosure: If you purchase through a link on our site, we may earn a commission. Learn more.

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

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: Gecko/20070725 Firefox/";

// Test the call
curl_get_wp_login( $login_user, $login_pass, $login_url, $visit_url, $http_agent, $cookie_file );

If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed.

This article was filed in our archives.
Written by Jean Galea
Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

In this article

Discover More

11 Responses

  1. 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?

  2. 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 .

  3. This gets me onto the login page and fills in the username and password fields but it doesn’t submit the form, please help.

  4. $ckfile = dirname(__FILE__) . “/cookie.txt”;
    $cookie_file = fopen($ckfile, ‘w’) or die(‘Derp…open…nooooooooo!’);

  5. 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 🙂

  6. 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.

  7. 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

Share Your Thoughts

Your email address will not be published. Required fields are marked *

New discoveries, every week.
Join thousands of designers, developers, and builders that come to WP Mayor to find the best guides, tools, and services for their next website. One email, once a week.
WP Mayor Newsletter

Claim Your Free Website Tip 👇

Leave your name, email and website URL below to receive one actionable improvement tip tailored just for your website within the next 24 hours.

"They identified areas for improvement that we had not previously considered." - Elliot

By providing your information, you'll also be subscribing to our weekly newsletter packed with exclusive content and insights. You can unsubscribe at any time with just one click.

What's missing?