Home > Back-end >  Wordpress custom theme with bootstrap not opening modal
Wordpress custom theme with bootstrap not opening modal

Time:07-29

Hello y'all it's the whole day I'm stuck figuring how to open a modal dialog box in my custom theme, I'm using bootstrap 5.2

function load_css()
{
    wp_register_style('bootstrap', get_template_directory_uri().'/assets/css/bootstrap.min.css', array(), false, 'all');
    wp_register_style('sticky_footer', get_template_directory_uri().'/assets/css/sticky-footer.css', array(), false, 'all');

    wp_enqueue_style('bootstrap-reboot');
    wp_enqueue_style('bootstrap');
    wp_enqueue_style('sticky_footer');
}

function load_js()
{
    wp_enqueue_script('jquery');
    wp_register_script('boostrap', get_template_directory_uri().'/assets/js/bootstrap.min.js', 'jquery', false, true);
    wp_enqueue_script('bootstrap');
}

function register_navwalker(){
    require_once get_template_directory() . '/bootstrap-navwalker.php';
}

Thse are my functions to load bootstrap css and js

My header file looks like this

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Document</title>

    <?php wp_head(); ?>
</head>
<body>
    <header>
        <nav >
            <div >
                <a  href="#">
                    <?php 
                    if ( function_exists( 'the_custom_logo' ) ) {
                        $custom_logo_id = get_theme_mod('custom_logo');
                        $logo = wp_get_attachment_image_src( $custom_logo_id , 'full' );

                        if ( has_custom_logo() ) {
                            echo '<img src="' . esc_url( $logo[0] ) . '" alt="' . get_bloginfo( 'name' ) . '" width="150" height"="50">';
                        } else {
                            echo '<h1>' . get_bloginfo('name') . '</h1>';
                        }
                    }
                    ?>
                </a>
                <button  type="button" data-bs-toggle="collapse" data-bs-target="#primary-menu" aria-controls="primary-menu" aria-expanded="false" aria-label="Toggle navigation">
                    <span ></span>
                </button>

                <div  id="primary-menu">
                    <?php
                    wp_nav_menu(array(
                        'theme_location' => 'primary-menu',
                        'container' => false,
                        'menu_class' => '',
                        'fallback_cb' => '__return_false',
                        'items_wrap' => '<ul id="%1$s" >%3$s</ul>',
                        'depth' => 2,
                        'walker' => new bootstrap_5_wp_nav_menu_walker()
                    ));
                    ?>
                </div>
                <button type="button"  data-bs-toggle="modal" data-bs-target="#loginModal">
                    Accedi all'area riservata
                </button>
            </div>
        </nav>
    </header>
    <div  id="loginModal" tabindex="-1" aria-labelledby="loginModalTitle" aria-hidden="true">
        <div >
            <div >
                <div >
                    <h5  id="loginModalTitle">Modal title</h5>
                    <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div >
                    ...
                </div>
                <div >
                    <button type="button"  data-bs-dismiss="modal">Close</button>
                    <button type="button" >Save changes</button>
                </div>
            </div>
        </div>
    </div>
    <main>

The #loginModal is basicly a modal where the user can put in username and password and it'll then make an AJAX call to another webpage to verify it and then automaticly closes and opens a new tab with a given link.

My problem is that the modal doesn't show up in the first place. How can i fix it? Code is take directly from bootstraps examples I've just renamed a few things carefully

CodePudding user response:

It must be that bootstrap.min.js isn't loaded. Maybe it's because the syntax here should be with an array.

wp_register_script('boostrap', get_template_directory_uri().'/assets/js/bootstrap.min.js', ['jquery'], false, true);

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Document</title>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>


</head>

<body>
  <header>
    <nav >
      <div >
        <a  href="#">
                  LOGO
                </a>
        <button  type="button" data-bs-toggle="collapse" data-bs-target="#primary-menu" aria-controls="primary-menu" aria-expanded="false" aria-label="Toggle navigation">
                    <span ></span>
                </button>

        <div  id="primary-menu">

        </div>
        <button type="button"  data-bs-toggle="modal" data-bs-target="#loginModal">
                    Accedi all'area riservata
                </button>
      </div>
    </nav>
  </header>
  <div  id="loginModal" tabindex="-1" aria-labelledby="loginModalTitle" aria-hidden="true">
    <div >
      <div >
        <div >
          <h5  id="loginModalTitle">Modal title</h5>
          <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div >
          ...
        </div>
        <div >
          <button type="button"  data-bs-dismiss="modal">Close</button>
          <button type="button" >Save changes</button>
        </div>
      </div>
    </div>
  </div>
  <main>

  • Related