Where to next with this plugin?
-
Dear Gagan
wonderful work, looking at some of the questions on the forums, I was thinking that it might be an idea to include some default shortcode logic in your plugin, this would save time for the most common usage of this plugin.
I posted an example in this thread, and for example you could look at coding this out of the box with the following default shortcode:
[shortcodeMenu isLocal true=”/new-project/custom-post” false=”/custom-post”]
we could build a list of such default cases and make some useful shortcodes to use with your plugin.
kind regards
-
Aurovrata,
Sorry I missed this thread of yours, I actually only rely on emails for support requests, and sometimes, I don’t get emails for support threads from WordPress(happening quite often).
About your suggestion, I think we can code a different plugin altogether with a bunch of useful ShortCodes.
You can provide me with a list of ShortCode suggestions or even code, to help making that new plugin.
Let me know your views on this.
Dear Gagan
nice to read you. Yes, by all means I would be happy to suggest a list.
At this point I have come across 2 real-life problems that I see the ‘Shortcodes in Menu’ plugin being the answer:
1. Folder based WordPress installation vs Domain root installation
This the more general case of the localhost issue. WordPress is either installed as a folder based installation or in the root folder as a domain, which is usually the live deployment.
For example: as localhost or a WPMU (multisite) installation, the installation comes under its own folder, which means that the root of the URL is always pre-poned with the folder name,
https://<domain-name>/beta/<slug of post/pages/taxonomy>
https://localhost/test/<slug of post/pages/taxonomy>So when we create a custom link in the Dashboard Appearance->Menus section, this is set as:
/[folder installation]/<slug of post/pages/taxonomy>
eg: /beta/<slug of post/pages/taxonomy>
or. /test/<slug of post/pages/taxonomy>This is compatible with the redeployment/export of this site to a domain-based installation,
Hence, a default shortcode can be created such as:
[SiM_isFolder relative_path=”/my-latest-post”]
(SiM= Shortcode in Menu)
Here is the code to do the above:// [SiM_isFolder] add_shortcode( 'SiM_isFolder', 'SiM_isFolder_menu_link' ); function SiM_isFolder_menu_link( $atts ) { $path = shortcode_atts( array( 'relative_path' => '/', ), $atts ); $url = '/'=== $path['relative_path'][0] ? home_url() : home_url().'/'; $url .= $path['relative_path']; return $url; }
2. Inserting a registered menu as a sub-menu
This is another problem which has cropped up in several projects. We want to dynamically add a series of sub-menus to the main menu.For example, we define a set of hierarchical taxonomies called
taxonomy_locations
, with items such as:- Mumbai
- Chennai
- Delhi
This taxonomy is common to 2 custom post_types, names
post_restaurant
andpost_hotel
. In order to differentiate which post_type to display in thearchive.php
/taxonomy.php
templates, we pass a url parameter,?postType=post_restaurant
for example.The wordpress menu dhasboard (Appearance->Menu) does not allow for the configuration of url parameters in the taxonomy items. Hence, I propose the following solution.
- 1. Register & setup a separate menu for the custom taxonomy
- 2. user Shorcodes in Menu to ensure the above menu is inserged as a sub-menu tree into the relevant item of the main menu
So we set up a custom menu item in the main menu called ‘Restaurants’ with the following shortcode
[SiM_custom_menu]
. This shortcode is created dynamically in the Settings page of your new plugin, the following parameters will need to be captured in the settings page,- menu_name – set this to say ‘restaurants’
- url_parameters (optional) – set this to say
‘postType=post_restaurant’ - relative_path (optional) – this can also be used to configure additional paths if need be, I can’t image such a use at this point, but it might be an interesting additional option
The plugin needs to execute the following code:
add_action( 'init', 'SiM_register_custom_menu' ); // register additional menu function SiM_register_custom_menu() { register_nav_menu('restaurants_menu','Restaurants Menu'); }
This will allow the user to see a new menu in his dashboard. They can then add of the Taxonomy
taxonomy_locations
which they have defined above.The plugin then needs to create the following shortcode,
/* * CUSTOM SiM Menu */ $SiM_constom_menu = array('menu_id'=>'', 'url_param'=>'', 'relative_path'=>''); // [SiM_custom_menu menu="restaurants-menu" relative_path="" url_parameters="postType=post_restaurants"] add_shortcode( 'SiM_custom_menu', 'SiM_insert_custom_menu_tree' ); function SiM_insert_custom_menu_tree( $atts ) { $path = shortcode_atts( array( 'url_parameters' => '', 'relative_path' => '', 'menu'=>'' ), $atts ); $menu = $path['menu']; $menu_class ='nav, nav-'.$menu; $locations = get_nav_menu_locations(); $menu_id = $locations[$menu]; /* *Let set some global values to be used in our menu hook *let-s build each custom menu using the menu_name in the key nomenclature to ensure multi menu configuration possibilities */ global $SiM_constom_menu; //echo "Menu=".$menu_id; $SiM_constom_menu['menu_id']=$menu_id; $SiM_constom_menu['url_param_'.$menu_id]=$path['url_parameters']; $SiM_constom_menu['relative_path_'.$menu_id]=$path['relative_path']; //next we call the actuall menu, and this will trigger our call to the hooked function SiM_nav_items $menu = wp_nav_menu( array( 'theme_location' => $menu, 'container' => '', 'fallback_cb' => '', 'menu_class' => $menu_class, 'menu_id' => $menu_id, 'echo' => false ) ); return $menu; } add_filter( 'wp_get_nav_menu_items','SiM_nav_items', 11, 3 ); function SiM_nav_items( $items, $menu, $args ) { global $SiM_constom_menu; $menu_id = $SiM_constom_menu['menu_id']; $url_params=$SiM_constom_menu['url_param_'.$menu_id]; $url_path=$SiM_constom_menu['relative_path_'.$menu_id]; if($menu_id == $menu->term_id){ foreach( $items as $item ) { $item->url .= $url_path.'?'.$url_params; } } return $items; }
PLEASE NOTE: Currently the function
SiM_nav_items( $items, $menu, $args )
isn’t checking if the default url of links is using post IDs instead of permalinks as in (.../?post=
or.../?cat=
) which should be done in order to ensure all cases of wordpress configuration, else the URLs produced here will be mal-formed. We would then use a&
rather than a?
to form the corrent URL, we also need to place therelative_path
content before the?
.Please note also that the SiM plugin needs to test if the return value form the shortcode is an array of items then it needs to build the resulting menu correctly.
let me know if you need help with this implementation…
small error in the function
function SiM_nav_items()
, it should read,function SiM_nav_items( $items, $menu, $args ) { global $SiM_constom_menu; $menu_id = $SiM_constom_menu['menu_id']; if($menu_id == $menu->term_id){ $url_params=$SiM_constom_menu['url_param_'.$menu_id]; $url_path=$SiM_constom_menu['relative_path_'.$menu_id]; foreach( $items as $item ) { $item->url .= $url_path.'?'.$url_params; } } return $items; }
- The topic ‘Where to next with this plugin?’ is closed to new replies.