Plugin Directory

Changeset 822824


Ignore:
Timestamp:
12/16/2013 02:04:02 AM (12 years ago)
Author:
fillup17
Message:

Adding support for multiple menu locations

Location:
menu-override/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • menu-override/trunk/MenuOverride.php

    r769237 r822824  
    44Plugin URI: http://phillipshipley.com/wordpress/menu-override
    55Description: Override page level menus with this plugin. On a page by page basis you can leave your navigation menu the default, choose different menus for each page, or have a page inherit from its parent. This plugin is particularly useful when you want to have section level navigation menus but your theme does not support it.
    6 Version: 0.1
     6Version: 0.2
    77Author: fillup17
    88Author URI: http://phillipshipley.com/
     
    3131class MenuOverride
    3232{
     33    const formFieldPrefix = 'mo_menu_location_';
     34   
    3335    public static function filterMenu($args = '')
    3436    {
    3537        global $post;
    36         $current = get_post_meta($post->ID,'selectedMenu',true);
    37         if($current == "PARENT"){
    38             $levels = 0;
    39             while($current == "PARENT" && $levels < 5){
    40                 $post = get_post($post->post_parent);
    41                 $current = get_post_meta($post->ID,'selectedMenu',true);
    42                 $levels++;
    43             }
    44         }
    45        
    46         if($current == 'DEFAULT'){
     38        if($args['theme_location'] == ''){
    4739            return $args;
    4840        }
     41        $menuOverrideSelection = get_post_meta($post->ID,'menuOverrideSelection',true);
    4942       
    50         $args['theme_location'] = '';
    51         $args['menu'] = $current;
     43        if(in_array($args['theme_location'],array_keys($menuOverrideSelection))){
     44            $current = $menuOverrideSelection[$args['theme_location']];
     45            if($current == "PARENT"){
     46                $levels = 0;
     47                while($current == "PARENT" && $levels < 5){
     48                    $post = get_post($post->post_parent);
     49                    $menuOverrideSelection = get_post_meta($post->ID,'menuOverrideSelection',true);
     50                    if(in_array($args['theme_location'],array_keys($menuOverrideSelection))){
     51                        $current = $menuOverrideSelection[$args['theme_location']];
     52                    }
     53                    $levels++;
     54                }
     55            }
     56
     57            if($current == 'DEFAULT'){
     58                return $args;
     59            }
     60           
     61            //$args['theme_location'] = '';
     62            $args['menu'] = $current;
     63        }
    5264       
    5365        return $args;
     
    6880    {
    6981        global $post;
    70         $current = get_post_meta($post->ID,'selectedMenu',true);
     82        $currentMenu = get_post_meta($post->ID,'menuOverrideSelection',true);
    7183        $menus = self::getMenus();
     84        $locations = get_registered_nav_menus();
     85       
    7286        wp_nonce_field('overridemenu_nonce', 'overridemenu_nonce' );
    7387        ?>
    74         <strong>You may override the menu used on this page by changing this dropdown:</strong><br />
    75         <select name="selectedMenu" id="selectedMenu">
     88        <strong>You may override any of the menus used on this page by selecting the location and the menu you wish to be displayed there:</strong><br />
     89       
     90        <?php
     91            foreach($locations as $location => $temp){
     92                $field = self::formFieldPrefix . preg_replace('/ /','___',$location);
     93        ?>
     94        <br /><br />Location: <i><?php echo $location; ?></i><br />
     95        <select name="<?php echo $field; ?>" id="menuLocation<?php echo $location; ?>">
    7696            <option value="DEFAULT">Use Default Menu</option>
    77             <option value="PARENT" <?php if($current == 'PARENT'){ echo "selected='selected'"; }?>>Use Parent Page Menu</option>
     97            <option value="PARENT" <?php if($currentMenu[$location] == 'PARENT'){ echo "selected='selected'"; }?>>Use Parent Page Menu</option>
    7898            <?php
    7999                foreach($menus as $menu){
    80100            ?>
    81101            <option value="<?php echo $menu->slug; ?>"
    82                     <?php if($menu->slug == $current){ echo " selected='selected' ";} ?>><?php echo esc_attr($menu->name);?></option>
     102                    <?php if($menu->slug == $currentMenu[$location]){ echo " selected='selected' ";} ?>><?php echo esc_attr($menu->name);?></option>
    83103            <?php
    84104                }
     
    86106        </select>
    87107        <?php
     108            }
    88109    }
    89110   
     
    96117        // if our current user can't edit this post, bail
    97118        if( !current_user_can( 'edit_post' ) ) return;
     119       
     120        $menuOverrideSelection = array();
     121        foreach($_POST as $key => $value){
     122            if(preg_match('/'.self::formFieldPrefix.'(.*)/', $key, $match)){
     123                $locName = preg_replace('/___/', ' ', $match[1]);
     124                $menuOverrideSelection[$locName] = $value;
     125            }
     126        }
    98127
    99         update_post_meta($post_id,'selectedMenu',$_POST['selectedMenu']);
     128        update_post_meta($post_id,'menuOverrideSelection',$menuOverrideSelection);
    100129    }
    101130   
  • menu-override/trunk/readme.txt

    r769237 r822824  
    33Tags: menu override, custom menu, custom navigation
    44Requires at least: 3.5.1
    5 Tested up to: 3.6
     5Tested up to: 3.8
    66Stable tag: trunk
    77License: GPLv2 or later
     
    2222== Changelog ==
    2323
     24= 0.2 =
     25* Added ability to override multiple menu locations on a single page
     26
    2427= 0.1 =
    2528* Initial Release
     
    2730== Upgrade Notice ==
    2831
     32= 0.2 =
     33* When upgrading, due to data structure changes, your current overrides will be lost, sorry.
     34
    2935= 0.1 =
    3036* Initial Release
Note: See TracChangeset for help on using the changeset viewer.