M
M
mercmarcus2021-09-05 15:55:45
WordPress
mercmarcus, 2021-09-05 15:55:45

How to add a plugin button to the menu?

I have a tiny transliterator plugin. By default it is a floating button. Can I fix it in the menu? I wanted to do it with a shortcode, though in the header. But the button is displayed differently on desktop and mobile. I have a stick menu, that is, a fixed one. I want to add a button there. Here is my shortcode:

add_shortcode( 'head_bar' , 'мой скрипт' );
add_action('wp_enqueue_scripts', 'мой скрипт style');


<?php echo do_shortcode('[head_bar]'); ?>


Are there other ways?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aricus, 2021-09-06
@Aricus

The easiest way is to move the button outside the menu: right before or after the menu itself. If necessary, you can get confused with the custom menu. WP allows you to make a menu of any kind, but it's not easy to figure it out. I will give an example from my project.
This is in layout:

<div class="wrapper">
            <?php wp_nav_menu( array(
              'sort_column' => 'menu_order',
              'theme_location' => 'primary-menu',
              'container' => false,
              'menu_class' => 'navigation_menu',
              'walker' => new My_Walker_Nav_Menu()
            ) ); ?>
          </div>

This is in functions.php, or a file included in it:
// Главное меню
class My_Walker_Nav_Menu extends Walker_Nav_Menu {
  function start_lvl(&$output, $depth = 1, $args = Array()) {
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent<ul class=\"navigation_submenu-".$depth."\">\n";
  }
  public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
    if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
      $t = '';
      $n = '';
    } else {
      $t = "\t";
      $n = "\n";
    }
    $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';

    $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    $classes[] = 'menu-item-' . $item->ID;

    $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );

    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
    $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

    $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
    $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

    $output .= $indent . '<li' . $id . $class_names .'>';

    $atts = array();
    $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
    $atts['target'] = ! empty( $item->target )     ? $item->target     : '';
    $atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
    $atts['href']   = ! empty( $item->url )        ? $item->url        : '';

    $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );

    $attributes = '';
    foreach ( $atts as $attr => $value ) {
      if ( ! empty( $value ) ) {
        $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
        $attributes .= ' ' . $attr . '="' . $value . '"';
      }
    }

    $title = apply_filters( 'the_title', writeTitle($item->title), $item->ID );

    $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
    
    
    if ($depth == 1) {
      $writeIcon = '<div class="submenu__icon">
        <div class="submenu__icon__img" style="background-image: url('.get_illImg($item->object_id)['url'].');"></div>
      </div>&nbsp;';
    } else {
      $writeIcon = '';
    }
    $item_output = $args->before;
    if (($depth == 1) && (get_field('activity', 'category_' .$item->object_id) == 'inactive')) {
      $item_output .= '<div' .addTooltip($item->object_id). '>';
      $item_output .= $writeIcon;
      $item_output .= $title;
      $item_output .= '</div>';
    } else {
      $item_output .= '<a'. $attributes .'' .addTooltip($item->object_id). '>';
      $item_output .= $writeIcon;
      $item_output .= $args->link_before . $title . $args->link_after;
      $item_output .= '</a>';
    }
    $item_output .= $args->after;

    $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  }
  
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question