Bootstrap 5 Menu Drop Down and Navigate


// The drop down menu needs to allow for navigation to parent as well as drop down.
// This javascript will find all instances of .dropdown-toggle (parent menu items) and if there is
// a .show class (ie; the submenu is showing), it will change to default behaviour (href navigation).
// If no .show class, bootstrap behaviour should be activated. // Target all parent menu items with a dropdown-toggle
const dropdownToggles = document.querySelectorAll('.dropdown-toggle');
dropdownToggles.forEach(function(dropdownToggle) {
const dropdownMenu = dropdownToggle.nextElementSibling; // Get the corresponding dropdown menu
const dropdown = dropdownToggle.closest('.dropdown'); // Get the closest dropdown container // Manually track the dropdown visibility state let isDropdownVisible = false; // Attach the click event to each parent link dropdownToggle.addEventListener('click', function(e) { e.preventDefault(); // Prevent Bootstrap's default behavior of toggling the dropdown if (isDropdownVisible) { // If the dropdown is open, navigate to the link window.location.href = dropdownToggle.href; // Navigate to the href } else { // If the dropdown is not open, show it and prevent navigation const bsDropdown = new bootstrap.Dropdown(dropdownToggle); bsDropdown.show(); // Explicitly show the dropdown without navigating // Update our manually tracked state isDropdownVisible = true; } });

});


Stay logged into WordPress

    function stay_logged_in($expires) {
    return 172800; // default 48 hours
    // return YEAR_IN_SECONDS;
    }
    add_filter(‘auth_cookie_expiration’, ‘stay_logged_in’);


    WordPress Admin Stylesheet

    To include styling for Administrators:

    function admin_style() {
    wp_enqueue_style(‘admin-styles’, get_template_directory_uri().’/admin.css’);
    }
    add_action(‘admin_enqueue_scripts’, ‘admin_style’);


    WordPress Categories Template

    If WordPress category template file is not working, add the following in category.php, before the loop:

    $cat_id = get_query_var(‘cat’);
    query_posts(“post_type=&cat=$cat_id”);


    Page Navigation by Category

    $terms = get_the_terms( $post->ID, ‘mediaitem’ );
    $args = array(
    ‘in_same_term’ => true,
    ‘category’ => $term->slug,
    ‘screen_reader_text’ => ‘Post navigation’
    );
    the_post_navigation($args);


    WordPress: Loop All Categories showing All Posts

    $post_type = ‘category’;
    $args = array(
    ‘true’ => false,
    ‘orderby’ => ‘term_id’,
    ‘order’ => ‘ASC’
    );
    $tax = get_terms( ‘category’ , $args);
    if ( have_posts() ) {
    foreach( $tax as $cat ) {
    echo $cat->name;
    while ( have_posts() ) : the_post();
    the_title();
    the_content();
    endwhile;
    }
    }


    Useful WordPress Development Plugins

    Replace WordPress Media with Enable Media Replace

    Audit WordPress changes using Stream

    Change WordPress administrator menu options with Admin Menu Editor

    Simplify Look and feel for WordPress Editors with White Label CMS

    Easy WordPress Updates Manager with Easy Updates Manager


    WordPress: Easy to remember template hierarchy

    Aide memoir for the WordPress template hiearchy…

    Default for everything – archive pages, single posts and single pages (as well as all other content types):

    1st: index.php

    Select template by page for specific archive displays, like category and taxonomy archives or templates for different page layouts:

    1st: template_{$custom-post-type}.php or template_{NOSIDEBAR}.php
    2nd: index.php

    For all pages that do not exist:

    1st: 404.php
    2nd: index.php

    The user submits a search:

    1st: search.php
    2nd: index.php

    Latest blog posts:

    1st: front-page.php
    2nd: home.php
    3rd: index.php

    For whichever page is assigned as the front page:

    1st: front-page.php
    2nd: home.php
    3rd: index.php

    For a single post page:

    1st: single-{$post-type}-{$slug}.php
    2nd: single-{$post-type}.php
    3rd: single.php
    4th: singular.php (fallback for single.php AND page.php)
    3rd: index.php

    For a single page:

    1st: custom_template.php
    2nd: page-{$slug}.php
    3rd: page-{$id}.php
    4th: page.php
    5th: singular.php (fallback for single.php AND page.php)
    6th: index.php

    For a category page:

    1st: category-{$slug}.php
    2nd: category-{$id}.php
    3rd: category.php
    4th: archive.php
    5th: index.php

    for a tag page:

    1st: tag-{$slug}.php
    2nd: tag-{$id}.php
    3rd: tag.php
    4th: archive.php
    5th: index.php

    For custom taxonomy pages:

    1st: taxonomy-{$taxonomy}-{$term}.php
    2nd: taxonomy-{$taxonomy}.php
    3rd: taxonomy.php
    4th: archive.php
    5th: index.php

    For Custom Post Types:

    1st: archive-{post_type}.php
    2nd: archive.php
    3rd: index.php

    For an author info page:

    1st: author-{$author-nicename}.php
    2nd: author-{$author-id}.php
    3rd: author.php
    4th: archive.php
    5th: index.php

    For a date page archive:

    1st: date.php
    2nd: archive.php
    3rd: index.php

    For an attachment page:

    1st: {$MIME-type}.php (one of: image.php, video.php, pdf.php, application.php, etc.)
    2nd: attachment.php
    3rd: single-attachment-{$slug}.php
    4th: single-attachment.php
    5th: single.php
    6th: singular.php
    7th: index.php

    For embeds:
    1st: embed-{$post-type}-{$post_format}.php
    2nd: embed-{$post-type}.php
    3rd: embed.php
    4th: wp-includes/theme-compat/embed.php


    WordPress: useful additions to functions.php

    /*
    * Useful snippets for functions.php
    /*

    /*
    * Add support for the following:
    */
    add_theme_support( ‘title-tag’ );
    add_theme_support( ‘post-thumbnails’ );
    add_theme_support( ‘html5’, array( ‘search-form’, ‘comment-form’, ‘comment-list’ ) );

    /*
    * Register Menus
    */
    function register_my_menus(){
    register_nav_menus(
    array( ‘primary’ => __(‘Primary Menu’, ‘AD’), ) );
    }
    add_action(‘init’, ‘register_my_menus’);

    /*
    * Allow php in widget
    */
    function howman_php_execute( $html ) {
    if ( strpos($html,”<“.”?php”) !==false ) {
    ob_start();
    eval(“?”.”>”.$html);
    $html=ob_get_contents();
    ob_end_clean();
    }
    return $html;
    }
    add_filter(‘widget_text’,’howman_php_execute’, 100);

    /*
    *Enqueue Datepicker + jQuery UI CSS
    */
    wp_enqueue_script( ‘jquery-ui-datepicker’ );
    wp_enqueue_style( ‘jquery-ui-style’, ‘//ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/smoothness/jquery-ui.css’, true);

    /*
    * Add widgets
    */
    function bootstrapstarter_widgets_init() {

    register_sidebar( array(
    ‘name’ => ‘Footer – Copyright Text’,
    ‘id’ => ‘footer-copyright-text’,
    ‘before_widget’ => ‘<div class=”footer_copyright_text”>’,
    ‘after_widget’ => ‘</div>’,
    ‘before_title’ => ‘<h4>’,
    ‘after_title’ => ‘</h4>’,
    ) );

    register_sidebar( array(
    ‘name’ => ‘Sidebar – Inset’,
    ‘id’ => ‘sidebar-1’,
    ‘before_widget’ => ‘<div class=”sidebar-module sidebar-module-inset”>’,
    ‘after_widget’ => ‘</div>’,
    ‘before_title’ => ‘<h4>’,
    ‘after_title’ => ‘</h4>’,
    ) );

    register_sidebar( array(
    ‘name’ => ‘Sidebar – Default’,
    ‘id’ => ‘sidebar-2’,
    ‘before_widget’ => ‘<div class=”sidebar-module”>’,
    ‘after_widget’ => ‘</div>’,
    ‘before_title’ => ‘<h4>’,
    ‘after_title’ => ‘</h4>’,
    ) );

    }
    add_action( ‘widgets_init’, ‘bootstrapstarter_widgets_init’ );

    /*
    * Register jQuery
    */
    if (!is_admin()) {
    wp_deregister_script(‘jquery’);
    wp_register_script(‘jquery’, (“https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js”), false);
    wp_enqueue_script(‘jquery’);
    }

    /*
    * Custom Excerpt Length
    */
    function custom_excerpt_length($length) {
    return 25;
    }
    add_filter(‘excerpt_length’, ‘custom_excerpt_length’);

    /*
    * Add scripts, stylesheets, fonts, etc.
    */
    function mysite_enqueue_styles() {
    wp_register_style(‘bootstrap’, get_template_directory_uri() . ‘/bootstrap/css/bootstrap.min.css’ );
    $dependencies = array(‘bootstrap’);
    wp_enqueue_style( ‘mysite-style’, get_stylesheet_uri(), $dependencies );
    }
    add_action( ‘wp_enqueue_scripts’, ‘mysite_enqueue_styles’ );

    function startwordpress_scripts() {
    wp_enqueue_script( ‘bootstrap’, get_template_directory_uri() . ‘/js/bootstrap.min.js’, array( ‘jquery’ ), ‘3.3.6’, true );
    wp_enqueue_script( ‘validator’, get_template_directory_uri() . ‘/js/jquery.validate.min.js’, array ( ‘jquery’ ), ‘3.3.6’, true);
    }
    add_action( ‘wp_enqueue_scripts’, ‘startwordpress_scripts’ );

    function startwordpress_google_fonts() {
    wp_register_style(‘Lato’, ‘http://fonts.googleapis.com/css?family=Lato:100,200,400,600,700,800’);
    wp_enqueue_style( ‘Lato’);
    wp_register_style(‘Nunito’, ‘https://fonts.googleapis.com/css?family=Nunito+Sans” rel=”stylesheet”>’);
    wp_enqueue_style( ‘Nunito’);
    // wp_register_style(‘OpenSans’, ‘https://fonts.googleapis.com/css?family=Open+Sans” rel=”stylesheet”>’);
    // wp_enqueue_style( ‘OpenSans’);
    }
    add_action(‘wp_print_styles’, ‘startwordpress_google_fonts’);


    WordPress: Must have plugins, updated

    Must Haves

    All In One SEO Pack
    Out-of-the-box SEO for your WordPress blog.

    WordFence
    THE security plugin.

    WP Optimize
    Clean Up / Optimise your WordPress installation.

    WP Super Cache
    WordPress Caching.

    WP Smush It
    Optimise graphics files.

    WP Migrate DB
    WordPress database migration.

    Used in Specific Circumstances

    Advanced Custom Fields
    Provides simple flexibility.

    Post Types Order
    Provides simple and easy sorting.

    Superceded

    These we still use occassionally, but simpler to build on the fly:

    Contact Form 7

    These we no longer used as they’re all done by All in One SEO:

    Google Analytics for WordPress
    Google XML Sitemaps