Forum Replies Created

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

    (@brit-albritton)

    I continued this soliloquy here, where interested readers can find the exciting solution.

    Unbate your breath…

    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);
    }
    Thread Starter Brit Albritton

    (@brit-albritton)

    OK. I played around with a few caching plugins (Really Static seemed promising, though the code is poorly commented and what comments there are are often in German) as well as SimpleHTMLDOM, until I realized I was being an idiot. This is really easy.

    So I created a rough draft:

    // no theme output
    define('WP_USE_THEMES', false);
    require_once('wp-load.php');
    
    //output nav menu
    $file="static/nav.tmp";
    
    // load menu to variable
    
    ob_start();
    require_once('wp-content/themes/twenty-twelve-child/opencartheader.php');
    $output = ob_get_contents();
    ob_end_clean();
    
    file_put_contents($file, $output);

    Where “opencartheader.php” is simply the portion of the WordPress header that contains my nav menu.

    My OpenCart template simply loads the file static/nav.tmp. I’ll copy the CSS & javascript straight from WordPress.

    I’ll strip away the unnecessary includes and create a plugin that will be triggered by menu saves.

    Feel free to chime in with any suggestions. Meanwhile, I hope this helps someone.

    Thread Starter Brit Albritton

    (@brit-albritton)

    As a more general question, would it be insane to use a screen-scraping technique to grab said menu? Alternately, can you think of any security risks that might arise from a plugin to output an html version of the menu whenever it is saved?

    Sorry if this is too general. It strikes me as something that could really help us poor benighted Opencart folks, and might also have general utility in WordPress integration. I can’t seem to find much on it, though.

Viewing 4 replies - 1 through 4 (of 4 total)