We have refactored the sidebar menu code to dynamically insert each <li> menu item using WordPress action hooks. This approach leverages the do_action('marketking_extend_menu') function to dynamically generate menu items based on conditions, instead of hardcoding each item directly into the template.
Key Changes:
Modular Functions: Each menu item is now encapsulated in a function that checks the relevant conditions before rendering the item. These functions are hooked into marketking_extend_menu.
Action Hooks: Used add_action('marketking_extend_menu', 'function_name', $priority) to hook each menu item function into the menu generation process.
Dynamic Rendering: Replaced static <li> elements with do_action('marketking_extend_menu') in the sidebar template to call all the hooked functions.
Benefits:
Enhanced Maintainability: By modularizing the menu items into individual functions, the code becomes easier to maintain and update. Each menu item's logic is self-contained and can be modified without affecting others.
Extensibility: This approach allows for easier addition of new menu items or modification of existing ones. Developers can simply create a new function and hook it into marketking_extend_menu without altering the core template.
Improved Readability: Separating the logic for each menu item into distinct functions enhances the readability of the template file, making it clear where and how each item is added.
Customization: It becomes easier for theme developers or users to customize the menu by adding or removing hooks without directly modifying the template file.
Consistency: Ensures consistent application of conditional logic and permissions for displaying menu items across the application.
// Example function to add a menu item function marketking_add_dashboard_menu_item() { if(marketking()->vendor_has_panel('dashboard')) { ?> <li class="nk-menu-item"> <a href="<?php echo esc_attr(trailingslashit(get_page_link(apply_filters('wpml_object_id', get_option('marketking_vendordash_page_setting', 'disabled'), 'post', true)))); ?>" class="nk-menu-link"> <span class="nk-menu-icon"><em class="icon ni ni-dashboard-fill"></em></span> <span class="nk-menu-text"><?php esc_html_e('Dashboard', 'marketking-multivendor-marketplace-for-woocommerce'); ?></span> </a> </li> <?php } } add_action('marketking_extend_menu', 'marketking_add_dashboard_menu_item', 20);
This method ensures that menu items are added in a structured and controlled manner, allowing for more straightforward debugging and updates.
Conclusion:
Implementing this dynamic insertion method for menu items will make the marketking sidebar more flexible, easier to maintain, and extendable for future enhancements. This approach aligns with best practices in WordPress development by utilizing hooks and actions to manage functionality modularly and dynamically.
If you have a github Im more than happy to share improvements.
Thank you very much for your suggestion and sharing the improved code,
I looked over it and it does seem like a nice way to make this work with hooks, without need to edit the sidebar.php template.
We will review it further and consider to replace our template in the next update.
In the meantime, I want to let you know if you are not aware, that you can copy the sidebar.php template to your child theme to replace the default template ( https://woocommerce-multivendor.com/docs/how-to-edit-marketking-pages-templates/ ). This way you can use your updated template without the concern of losing the change if the plugin is updated. It works similar to WooCommerce templates,
We have refactored the sidebar menu code to dynamically insert each <li> menu item using WordPress action hooks. This approach leverages the do_action('marketking_extend_menu') function to dynamically generate menu items based on conditions, instead of hardcoding each item directly into the template.
Key Changes:- Modular Functions: Each menu item is now encapsulated in a function that checks the relevant conditions before rendering the item. These functions are hooked into marketking_extend_menu.
- Action Hooks: Used add_action('marketking_extend_menu', 'function_name', $priority) to hook each menu item function into the menu generation process.
- Dynamic Rendering: Replaced static <li> elements with do_action('marketking_extend_menu') in the sidebar template to call all the hooked functions.
Benefits:// Example function to add a menu item
function marketking_add_dashboard_menu_item() {
if(marketking()->vendor_has_panel('dashboard')) {
?>
<li class="nk-menu-item">
<a href="<?php echo esc_attr(trailingslashit(get_page_link(apply_filters('wpml_object_id', get_option('marketking_vendordash_page_setting', 'disabled'), 'post', true)))); ?>" class="nk-menu-link">
<span class="nk-menu-icon"><em class="icon ni ni-dashboard-fill"></em></span>
<span class="nk-menu-text"><?php esc_html_e('Dashboard', 'marketking-multivendor-marketplace-for-woocommerce'); ?></span>
</a>
</li>
<?php
}
}
add_action('marketking_extend_menu', 'marketking_add_dashboard_menu_item', 20);
This method ensures that menu items are added in a structured and controlled manner, allowing for more straightforward debugging and updates.
Conclusion:Implementing this dynamic insertion method for menu items will make the marketking sidebar more flexible, easier to maintain, and extendable for future enhancements. This approach aligns with best practices in WordPress development by utilizing hooks and actions to manage functionality modularly and dynamically.
If you have a github Im more than happy to share improvements.
Attached files: sidebar.php.zip
Hello David,
Thank you very much for your suggestion and sharing the improved code,
I looked over it and it does seem like a nice way to make this work with hooks, without need to edit the sidebar.php template.
We will review it further and consider to replace our template in the next update.
In the meantime, I want to let you know if you are not aware, that you can copy the sidebar.php template to your child theme to replace the default template ( https://woocommerce-multivendor.com/docs/how-to-edit-marketking-pages-templates/ ). This way you can use your updated template without the concern of losing the change if the plugin is updated. It works similar to WooCommerce templates,
Kind regards,
Stefan