Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
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 App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Http\Request;
use App\Http\Requests;

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";
like image 553
jeanpier_re Avatar asked Sep 11 '15 17:09

jeanpier_re


1 Answers

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.

like image 144
Gummibeer Avatar answered Sep 23 '22 14:09

Gummibeer