1

I have a Wordpress installation with a sub directory /shop where I have my Magento 2 installation. I've included wp-load in Magento's index.php (root) so that I can make use of WP functions within the shop such as get_header() etc.

I have an issue where both Wordpress and Magento are trying to declare the function __() for translations. This was solved by adding the following around the function in the functions.php file of Magento (/app/functions.php).

if (!function_exists ('__')){
    function __() {
        .......
    }
}

This solved my immediate error of trying to redeclare the function but I then ran into my next error:-

Recoverable Error: Argument 1 passed to Magento\Framework\Exception\NoSuchEntityException::__construct() must be an instance of Magento\Framework\Phrase, string given, called in....vendor/magento/module-store/Model/StoreRepository.php on line 60

So, I checked what's on this line and it's a call to the __() function

throw new NoSuchEntityException(__('Requested store is not found'));

The return of the __() is giving a string and not an instance of Magento\Framework\Phrase because it's the WP declared version of the function. If I remove __('Requested store is not found') and pass null (ie don't pass a string) then the error is resolved. Obviously this is not what I want to do since those errors will be useful to me.

So, my question is, is there a way around this problem without having to re-name either the WP and Magento 2 functions as this would mean going through the entire code base and changing all calls to the function.

Thank you in advance.

1

Your problem is attempting to load both frameworks simultaneously in the first place, as you say:

I've included wp-load in Magento's index.php (root) so that I can make use of WP functions within the shop such as get_header() etc.

This is a very bad idea, on account of performance penalty and the inevitable conflicts you will encounter between them (like this __() collision).

In short, no, there is no way to resolve this conflict except changing all references in one system or the other.

The proper way to do this would be to forget your hopes of combining the systems (and themes), and implementing your Magento theme to match your Wordpress site through its native theme system. It will be a lot of work, but you're not hacking apart any system in the process. Long-term, this will save you an immense amount of time and pain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.