Loading wordpress stuff on laravel site

You are trying to do that just to output a WP menu on a Lavarel site?

In this case you are loading all WordPress environment (themes, plugins, options…) just for a menu. This not seems to me a great thing.

Probably, the most elegant approach for this, should be in Laravel create the eloquent models for the involved WP tables, a Controller a View to to output the menu.
All using Laravel code, in this way you don’t need to load WP.. Of course you can copy the code from WP core and translate in Laravel…

Laravel is great to semplificate thing, however I understand that this can appear a lot of work compared to just an include…

For this reason, and to not write an off-topic answer, I’ll suggest you another approach.

In the WordPress root folder, create a subfolder named, e.g. 'tmp'.

This folder is a sort of exchange folder from WP to Lavarel. Be sure WordPress can write files in this folder.

After that, in the functions.php of the active theme in WordPress put something like:

add_action('load-nav-menus.php', 'go_cache_menu');

function go_cache_menu() {
  add_action('shutdown', 'cache_menu');
}

function cache_menu() {
    $filename = trailingslashit(ABSPATH) . '/tmp/primary-menu.inc';
    $content = wp_nav_menu( array(
      'theme_location' => 'primary',
      'container' => false,
      'menu_class' => 'menu',
      'menu_id' => '',
      'fallback_cb' => false,
      'echo'  => false
    )); 
    file_put_contents ( $filename , $content );
}

This code, when you visit the nav menu page, create a file in the tmp folder, named primary-menu.inc that contain your menu. When you update the menu, that file is updated.

After that, in Laravel you can retrieve the content of this file, like so:

$path="./wordpress/tmp/primary-menu.inc";
$menu_content = @file_get_contents($path) ? : '';

And then pass the $menu_content to Lavarel views to display it… The performance of your Lavarel site will improve a lot without loading WordPress! (and if you use a Blade template the content of your menu will be also auto-cached…)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)