6

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";
3
  • 1
    I am not familiar with this topic, so this may be a stupid question, but I'll ask it just in case. In your working first example, you import Corcel at the top. Isn't that what allows you to use the WordPress functionality, and if so, shouldn't you important that in the Artisan command as well? Commented Sep 11, 2015 at 20:06
  • Not a dumb questions at all, I literally just tried that earlier and it wasn't working but I didn't import the plugin properly. Once I started importing each class individually it works now. so use Corcel becomes use Corcel\Posts etc... Commented Sep 11, 2015 at 20:59
  • More to the point this does offer an interface for accessing WordPress but it doesn't give me access to WordPress's methods directly, which is what I'm really looking for. Commented Sep 11, 2015 at 21:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, but there may be a strategy there if I can manage to define the constant before autoloading wp-blog-header.php. I'll experiment with that then report back.
Thanks for accepting my answer - which one was the right way? Including this part in the bootstrap/app.php or?
Placing it in bootstrap/app.php worked great with the above fixes. The autoloading created so many errors that I didn't bother trying to make it work. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.