How to Access WordPress functions from Artisan Command

I followed this tutorial on using WordPress with Laravel and I was able to access WordPress functions from my Laravel controllers.

Basic Example

Read More
<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppHttpRequests;
use AppHttpControllersController;
use Corcel;
class WordPressController extends Controller
{
    public function getIndex ()
    {
        return redirect('/');
         $posts = get_posts([
             'posts_per_page' => 20,
             'order' => 'ASC',
             'orderby' => 'post_title',
             ]);

        return $posts;
    }

That works and I’ve been able to access all the WordPress methods that I’ve tried so far.

The Issue

Where I get stuck is when I create and register a new artisan command and attempt to access those same methods from there.

<?php

namespace AppConsoleCommands;

use IlluminateConsoleCommand;
use IlluminateHttpRequest;
use AppHttpRequests;

class WPTags extends Command
{
    protected $signature = 'wp:tags';
    protected $description = 'Output tags from WordPress';

    public function __construct()
    {
        parent::__construct();
    }
    public function handle()
    {
        $tags = get_tags([
          'number'=>20,
          'offset' => 10,
          'hide_empty' => true,
        ]);
        return $tags;
    }

From what I can tell there is an issue with the way Laravel 5 imports the WordPress methods via the index.php file. I think I need to do something with autoloading but I’m lost. I’ve tried repeating the steps taken in the index.php file inside of my artisan commands file constructor.

The only other (hacky) thing I could think of was importing the controller into my artisan command but I’d really rather not do it that way.

Update

The accepted answer is the way to go. There are a few things you’ll run into. You’ll get a few errors related to the $_SERVER variable not being set in the client. Here is the code I used to suppress/deal with these errors.

It’s not perfect but for local development this should at least get you productive.

//assumes you're using localhost as your base url
$_SERVER['HTTP_HOST'] = "localhost";
$_SERVER['SERVER_PROTOCOL'] = "HTTP/1.1";

if (!isset($_SERVER['REQUEST_METHOD'])) {
  $_SERVER['REQUEST_METHOD'] = "GET";
}
if (!isset($_POST['action'])) {
  $_POST['action'] = "undefined";
}

define('WP_USE_THEMES', false);

require __DIR__."/../public/wordpress/wp-blog-header.php";

Related posts

1 comment

  1. I think that this part

    Connect Laravel to WordPress

    define('WP_USE_THEMES', false);
    require __DIR__."/../public/wordpress/wp-blog-header.php";
    

    Is much better placed in the app.php because this file gets opened on every call to laravel: web, console and so on. This is not tested, but I think that should work.

    The other and I think a lot of better way is to include this file into the composer autoloader but there you can’t define the constant.

Comments are closed.