Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 ovewrite helper function __() because used in wordpress

I have read several post on stackoverflow but didn't help, so i hope somebody can give the good answer.

I am using Laravel with wordpress. Now there is an error. Is it possible to rename or other method to change this?

Error:

Fatal error: Cannot redeclare __() (previously declared in C:app\laravel\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:821)
like image 831
Bas Avatar asked Jan 21 '26 23:01

Bas


2 Answers

In de file wp-blog-header.php load before wp-load.php the l110n.php

if ( !isset($wp_did_header) ) {

    $wp_did_header = true;

    // Load the WordPress library.
    require_once( dirname(__FILE__).'/wp-includes/l10n.php' );
    require_once( dirname(__FILE__) . '/wp-load.php' );

    // Set up the WordPress query.
    wp();

    // Load the theme template.
    require_once( ABSPATH . WPINC . '/template-loader.php' );

}

And make a own helpers file in your LARAVEL project with this function with ownname:

if (! function_exists('ownname')) {
    /**
     * Translate the given message.
     *
     * @param  string  $key
     * @param  array  $replace
     * @param  string  $locale
     * @return \Illuminate\Contracts\Translation\Translator|string
     */
    function ownname($key = null, $replace = [], $locale = null)
    {
        return app('translator')->getFromJson($key, $replace, $locale);
    }
}
like image 59
Bas Avatar answered Jan 23 '26 17:01

Bas


Laravel 5.4 added a helper function called __() This function has the same name as WordPress's.

Just rename Laravel's __ to ___ and run "composer update".

You can find the file in:

[project folder]\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php

The problematic function is in line 947. Don't forget to pay attention to line 938 as well.

I had this exact same issue. Couldn't do it with Bas's solution, though. Hope this helps someone.

NOTE: You shoud never change files in the Vendor folder, as they would need to be maintained. I do not recommend this solution unless you understand the consequences. Still, it gets the job done.

like image 43
Luís Henriques Avatar answered Jan 23 '26 18:01

Luís Henriques



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!