Skip to content

Instantly share code, notes, and snippets.

@humayunahmed8
Created October 14, 2023 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humayunahmed8/303ec1010abbf33f9e35afd38e6b7467 to your computer and use it in GitHub Desktop.
Save humayunahmed8/303ec1010abbf33f9e35afd38e6b7467 to your computer and use it in GitHub Desktop.
WP Nav Menu Custom WIdget JSON format
[
{"label": "Products", "url": "#"},
{"label": "Learn", "url": "#"}
]
<?php
class Custom_Nav_Menu_Widget_Column_2 extends WP_Widget {
public function __construct() {
parent::__construct(
'custom_nav_menu_widget_column_2',
esc_html__('Custom Navigation Menu (Column 2)', 'text_domain'),
array('description' => esc_html__('Display a custom navigation menu for Column 2.', 'text_domain'))
);
}
public function widget($args, $instance) {
echo $args['before_widget'];
// Display the menu
echo '<ul class="glue-footer__site-links-list glue-no-bullet" id="column-2-content">';
foreach ($instance['menu_items'] as $item) {
echo '<li class="glue-footer__site-links-list-item">';
echo '<a class="glue-footer__link" href="' . esc_url($item['url']) . '" aria-label="' . esc_attr($item['label']) . '">' . esc_html($item['label']) . '</a>';
echo '</li>';
}
echo '</ul>';
echo $args['after_widget'];
}
public function form($instance) {
$menu_items = isset($instance['menu_items']) ? $instance['menu_items'] : array();
?>
<p>
<label for="<?php echo $this->get_field_id('menu_items'); ?>"><?php esc_html_e('Menu Items:', 'text_domain'); ?></label>
<textarea class="widefat" rows="4" id="<?php echo $this->get_field_id('menu_items'); ?>" name="<?php echo $this->get_field_name('menu_items'); ?>"><?php echo esc_textarea(json_encode($menu_items)); ?></textarea>
<small><?php esc_html_e('Enter menu items in JSON format. Example: [{"label": "Products", "url": "#"}, {"label": "Learn", "url": "#"}]', 'text_domain'); ?></small>
</p>
<?php
}
public function update($new_instance, $old_instance) {
$instance = array();
$instance['menu_items'] = json_decode($new_instance['menu_items'], true);
return $instance;
}
}
function register_custom_nav_menu_widget_column_2() {
register_widget('Custom_Nav_Menu_Widget_Column_2');
}
add_action('widgets_init', 'register_custom_nav_menu_widget_column_2');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment