Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stanhook's avatar

WordPress and Laravel Running Together

Hi,

I have a project that has an existing, very custom, WordPress site and in the site there is a directory that as a ton of old ASP pages that queries a different database that shows a ton of content. It made sense to convert all of the ASP content using Laravel and keep the custom WordPress site as it is. Also by doing this we can retain the links and SEO. We can also include the WordPress header, navigation and footer into the new section creating a great user interface. The issue is Laravel using __() and that conflicts with WordPress because it has the same thing.

Cannot redeclare __() (previously declared.....

Just to try, I changed the Laravel code in the helper file to use ___() and everything works great. I ran through a bunch of testing without any hiccups. I get the WordPress header, navigation and footer just as I wanted as well as all of the converted content. So obviously changing that file is an awful idea so I am looking for a solution. Laravel cannot be in the root folder because of all the URL's and SEO and I cannot move away from WordPress. The two need to live together with WordPress being the main site in the root directory and Laravel in a sub-folder. Does anyone have any suggestions on how to accomplish this?

Thanks, Stan

0 likes
5 replies
hamzaaslam's avatar

In my opinion solution to this problem is to namespace the Laravel code. Laravel supports namespaces, which allow you to encapsulate your code within a namespace to avoid conflicts with other code. By doing this, you can ensure that the "__()" function in Laravel does not conflict with the one in WordPress.

To namespace your Laravel code, you can add a namespace declaration at the top of each PHP file. For example:

namespace MyProject\Laravel;

function __($string) {
  // Laravel translation function code here
}

This would declare a namespace called "MyProject\Laravel" and the "__()" function would be contained within that namespace. To use the function, you would need to reference it with the full namespace, like this:

MyProject\Laravel\__($string);

Another option would be to use a package like "wp-laravel-bridge" which provides a set of tools and functionality to help integrate WordPress and Laravel, including the ability to use Laravel within a WordPress site.

I hope this helps!

1 like
stanhook's avatar

@hamzaaslam Thanks for the response! But would this work? The calls to __() are located in the vendor files.

hamzaaslam's avatar

@stanhook If the calls to __() are located in the vendor files, then you cannot modify those files directly, as modifying vendor files is generally not a good idea and can cause issues with updates or maintenance of your application.

In this case, you can try to override the __() function in your Laravel application. To do this, you can create a new file in your Laravel project, such as helpers.php, and define your own implementation of the __() function in that file. For example:

if (!function_exists('__')) {
    function __($text, $domain = 'default') {
        // Your custom implementation of the __() function here
    }
}

The function_exists() check will ensure that you are only defining the function if it hasn't already been defined in the vendor files or elsewhere.

Then, you can include this file in your Laravel application by adding it to the autoload section of your composer.json file:

"autoload": {
    "files": [
        "app/helpers.php"
    ]
},

This will load your helpers.php file and make your custom implementation of the __() function available to the rest of your Laravel application.

Keep in mind that this approach could potentially cause issues if the vendor code depends on the original implementation of __(). You should thoroughly test your application after making these changes to ensure that everything still works as expected.

1 like
Sinnbeck's avatar

@hamzaaslam you cannot overwrite it sadly as laravel is loaded before your own file. Trust me i have tried :)

stanhook's avatar

So the last post in this thread helped:

https://core.trac.wordpress.org/ticket/41498

Essentially adding this code in the Public/index.php file before /../vendor/autoload.php

Pull in support for WP

require_once '/path/to/your/wp-includes/l10n.php';
require_once '/path/to/your/wp-load.php';
wp_enqueue_style('app', '/yourapp/css/app.css');
wp_enqueue_script('app', '/yourapp/js/app.js');

I now no longer had to modify the vendor files and it worked with WordPress. I did have an issue with pagination but I created a new template for that and extended LengthAwarePaginator with my own code to help with __().

Anyone with input about using this method would be greatly appreciated. Laravel is new to me and I appreciate the help!

Please or to participate in this conversation.