• Resolved Brit Albritton

    (@brit-albritton)


    Hi everyone,

    I began this discussion in the wrong subforum (here). It’s a more general question than I realized, and the solution seems simpler and possibly of interest to many people.

    Like many people, I’m interested in integrating a menu generated by wordpress into a non-wordpress site. In my case, the awkward dance partner is OpenCart, but people do this for Magento, etc.

    After playing around with a direct query of the WordPress database (which seems inefficient for a menu, and causes quite a few conflicts with OpenCart), it struck me that the best solution was to create a static copy of the navigation menu as a temporary file, and to access the temporary file with a simple include_once from my OpenCart template. Here’s a test version that I wrote:

    // no theme output
    define('WP_USE_THEMES', false);
    require_once('wp-load.php');
    
    //define temporary file for static output
    $file="static/nav.tmp";
    
    //use buffering to capture HTML
    ob_start();
    
    //Wordpress template file with only the HTML I want
    require_once('wp-content/themes/twenty-twelve-child/opencartheader.php');
    
    //Save buffer to variable
    $output = ob_get_contents();
    
    //End buffering
    ob_end_clean();
    
    //Save stored buffer to temp file
    file_put_contents($file, $output);

    Simple, once I realized this was actually what I needed.

    Now I’d like to create a plugin that will do this automatically whenever a custom menu is saved. I’ve downloaded a few caching plugins, and am reading the code for inspiration, but this seems as though it should also be easy. Unfortunately, my Google skills don’t seem up to finding out how.

    Could someone steer me in the right direction?

    Thanks,

    Brit

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Brit Albritton

    (@brit-albritton)

    The action is wp_update_nav_menu.

    Here’s the plugin, in case this soliloquy has been of use to someone. Feel free to chime in if you see anything that might be improved.

    If I’m feeling chivalrous, I may post this at OpenCart forums….

    <?php
    /*
    Plugin Name: Static Nav Menus
    Plugin URI: https://www.camelopardalis.org
    Description: Saves WordPress navigation menu as static file to be loaded in other websites. Fires whenever nav menu is modified.
    Author: William Albritton
    Version: .1
    Author URI: https://www.camelopardalis.org/
    */
    
    add_action('wp_update_nav_menu', 'wa_staticnav');
    
    function wa_staticnav() {
    
    	//get root of site
    	if(!defined('__BASEDIRECTORY__')) {
    	define('__BASEDIRECTORY__', dirname(dirname(dirname(dirname(__FILE__)))));
    	}
    
    	//set file to output static nav menu
    		$file=__BASEDIRECTORY__."/static/nav.tmp";
    
      // echo Nav menu to buffer
    		ob_start();
    
    		?>
    			<nav id="site-navigation" class="main-navigation" role="navigation">
    				<h3 class="menu-toggle"><?php _e( 'Menu', 'twentytwelve' ); ?></h3>
    				<a class="assistive-text" href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentytwelve' ); ?>"><?php _e( 'Skip to content', 'twentytwelve' ); ?></a>
    				<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) ); ?>
    			</nav>
    		<?php
      // set variable $output to buffer
    		$output = ob_get_contents();
    
    	// end buffering
    		ob_end_clean();
    
    	// write $output to file
    		file_force_contents($file, $output);
    }
    
    //Create directory if it doesn't exist. Based on TrentTompkins at https://php.net/manual/en/function.file-put-contents.php. Modified to allow development on local box (i.e. C: is root.)
    function file_force_contents($dir, $contents){
        $parts = explode('/', $dir);
        $file = array_pop($parts);
        $root = array_shift($parts);
        $dir = $root;
        foreach($parts as $part)
        if(!is_dir($dir .= "/$part")) mkdir($dir);
        file_put_contents("$dir/$file", $contents);
    }

    wp_update_nav_menu, doesn’t do the trick for me

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Creating a static copy of a custom menu’ is closed to new replies.