E
E
Evij212021-08-10 16:25:06
WordPress
Evij21, 2021-08-10 16:25:06

I need to bind a function to the button to change the main page and menu, how to do it better?

There is a default switcher (like on ios), it is necessary that when you click on it, the main page changes, and if in more detail there are two pages of the same type, one is winter, the other is summer, Now the main page is summer, but when you click on the switcher, it should change to winter. The switcher will be placed as in the screenshot61127e2b61458699964597.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-08-10
@artzolin

I hope you understand that this admin bar is only visible to authorized users, it is not very suitable for what you want. Buttons for changing the season or a dark / light theme are more appropriate to place on the front in the header and / or in the footer
. However, here is an example of how you can add a button to the admin bar

add_action('admin_bar_menu', 'add_season_switcher_item', 100);
function add_season_switcher_item( $admin_bar ){
  global $pagenow;
  $admin_bar->add_menu( array( 'id'=>'season-switcher','title'=>'Season Switcher','href'=>'#' ) );
}

And this is an example of how you can display a handler for this button
add_action( 'admin_footer', 'season_switcher_js' );
function season_switcher_js() { ?>
  <script type="text/javascript" >
    jQuery("li#wp-admin-bar-season-switcher .ab-item").on( "click", function() {

      // код тут

    });
  </script> <?php
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question