0

I not sure why i failed to load the wordpress site on the laravel site. the following code, i tested on plain php without any framework - it work! but if i call from the file within laravel framework - it failed!!

<?php 
define('WP_USE_THEMES', false);     
require('./wordpress/wp-blog-header.php'); 
wp_nav_menu(array('theme_location' => 'primary',
    'container' => false,
    'menu_class' => 'menu',
    'menu_id' => '',
    'fallback_cb' => false
)); 

i not sure what's the problem and the following is the trace catch from the error:

array_key_exists() expects parameter 2 to be array, null given

function mo_get_theme_option($option_id, $default = null, $single = true) {
    global $mo_theme;
    global $options_cache;

    if (array_key_exists($option_id, $options_cache))
        return $options_cache[$option_id];
  • Where does $options_cache get populated? What does var_dump( $options_cache ); reveal? Clearly this is where your problem lies (if the last snippet is related). | Also, I don't see how this is related to WP. – Johannes Pille Sep 22 '13 at 19:29
  • @JohannesPille, i'm trying to get the nav menu of wordpress into the laravel website. and i downloaded entire live version of wordpress to my local folder to test it out. the array_key_exist is from the template file i think.. – 1myb Sep 22 '13 at 19:35
  • You've defined WP_USE_THEMES to false, but are using theme related code. – s_ha_dum Sep 22 '13 at 19:41
  • @s_ha_dum, but i use only the function wp_nav_menu. tried get_header as well. also failed – 1myb Sep 22 '13 at 19:44
  • 2
    Look at that function call-- 'theme_location' => 'primary',. Notice that part about the theme? And the other function: mo_get_theme_option. Also notice the part about the theme? Try without define('WP_USE_THEMES', false); just for fun. – s_ha_dum Sep 22 '13 at 19:49
5

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...)

  • Exactly!! I immediately use the ob to catch the return and then dump them into a variable, write the content to file (blade.php) immediately then move to the laravel view folder. yea.. it's damn a lot of work to reverse the wordpress into laravel :/ hope someone with strong skill on both framework work it out and i will be one of contributor :P Thx – 1myb Sep 23 '13 at 1:17
  • when I remove the menu, primary-menu.inc is not updated. – lyhong Mar 24 '16 at 4:16
  • @lyhong this is a proof of concept code to betterexplain the answer. Is not intended to be some code you copy and paste on production. – gmazzap Mar 24 '16 at 13:27

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.