class-custom-nav-menu-items-in-new-tab.php
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class for Allow Custom Menu Links to Open in New Tab module |
| 7 | * |
| 8 | * @since 6.9.5 |
| 9 | */ |
| 10 | class Custom_Nav_Menu_Items_In_New_Tab { |
| 11 | |
| 12 | /** |
| 13 | * Add "open in new tab" checkbox in custom nav menu item settings |
| 14 | * |
| 15 | * @since 5.4.0 |
| 16 | */ |
| 17 | public function add_custom_nav_menu_open_in_new_tab_field( $item_id, $menu_item, $depth, $args ) { |
| 18 | |
| 19 | $target_blank = get_post_meta( $item_id, '_menu_item_target_blank', true ); |
| 20 | |
| 21 | if ( 'custom' == $menu_item->object ) { |
| 22 | ?> |
| 23 | <p class="field-target_blank description-wide"> |
| 24 | <label for="edit-menu-item-target-blank-<?php echo esc_attr( $item_id ); ?>"> |
| 25 | <input type="checkbox" id="edit-menu-item-target-blank-<?php echo esc_attr( $item_id ); ?>" name="menu-item-target-blank[<?php echo esc_attr( $item_id ); ?>]" value="1" <?php checked( $target_blank, '1' ); ?> /> |
| 26 | Open link in new tab and add rel="noopener noreferrer nofollow" attribute. |
| 27 | </label> |
| 28 | </p> |
| 29 | <?php |
| 30 | } |
| 31 | |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Save status of "open in new tab" checkbox in custom nav menu item settings |
| 36 | * |
| 37 | * @since 5.4.0 |
| 38 | */ |
| 39 | public function save_custom_nav_menu_open_in_new_tab_status( $menu_id, $menu_item_db_id, $args ) { |
| 40 | |
| 41 | if ( isset( $_POST['menu-item-target-blank'][$menu_item_db_id] ) ) { |
| 42 | update_post_meta( $menu_item_db_id, '_menu_item_target_blank', '1' ); |
| 43 | } else { |
| 44 | delete_post_meta( $menu_item_db_id, '_menu_item_target_blank' ); |
| 45 | } |
| 46 | |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Add attributes to custom nav menu item on the frontend |
| 51 | * |
| 52 | * @since 5.4.0 |
| 53 | */ |
| 54 | public function add_attributes_to_custom_nav_menu_item( $atts, $menu_item, $args ) { |
| 55 | |
| 56 | $target_blank = get_post_meta( $menu_item->ID, '_menu_item_target_blank', true ); |
| 57 | |
| 58 | if ( $target_blank ) { |
| 59 | $atts['target'] = '_blank'; |
| 60 | $atts['rel'] = 'noopener noreferrer nofollow'; |
| 61 | } |
| 62 | |
| 63 | return $atts; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | } |