How can I add javascript in wordpress functions.php file after check on Database

How can I add inline javascript in wordpress functions.php file within this code

function jointheclubfbconnect(){
$message = '
    <div style="width:600px;background:white;padding:25px;">
    <div style="width:100%;font-size:25px;"></div>
    <br/> 
    $message .='<div style="width:100%;display:block;color:#666666;">Kind regards</div><br/><div style="width:100%;display:block;color:#666666;">Team</div><br/><img src="'.get_bloginfo('url') .'/wp-content/themes/teenvoice/images/smalllogoteenvoice.png" alt="Logo"><br/>';
            $header = 'From: Teen | info@teen.com' . "rn"; 
            $header .= "MIME-Version: 1.0" . "rn";
            $header .= "Content-type:text/html;charset=utf-8" . "rn";
            $from  = 'info@teen.com';
            $subject =  'Thank You for Joining Teen';
            $fbemail = $_POST['fbemail'];
            global $wpdb;   
             $checkUserEmail = $wpdb->get_results("SELECT * FROM club_members WHERE Email = '".$fbemail."'"); 
            // var_dump($checkUserEmail);
            $ip = $_POST['ip'];
            $to = '';
    $to .= $fbemail; 
            if(empty($checkUserEmail)){
                    if(mail($to, $subject, $message, $header)) {
                        echo 'ok'; 
                        global $wpdb;
                        $wpdb->insert(
                                'club_members',
                                array(
                                    'Email' => $fbemail,
                                    'IP' => $ip, 
                                    'Date' => current_time('d M Y H:i:s')
                                ),
                                array( 
                                        '%s',
                                        '%s',
                                        '%s'
                                )
                        );
                    }
            }else{
                echo 'You already Registered';
                function print_my_inline_script() {
                    if ( wp_script_is( 'jquery', 'done' ) ) {
                  ?>
                  <script type="text/javascript">
                  alert('Yesss');
                  </script>
                  <?php
                    }
                  }
                  add_action( 'wp_footer', 'print_my_inline_script' );
            }

}

Read More

I have tried doing the following

function print_my_inline_script() {
                    if ( wp_script_is( 'jquery', 'done' ) ) {
                  ?>
                  <script type="text/javascript">
                  alert('Yesss');
                  </script>
                  <?php
                    }
                  }
                  add_action( 'wp_footer', 'print_my_inline_script' );

You can see this in the code but it didnt work.How I can add 2 lines of jquery within my code?

Related posts

2 comments

  1. @noman’s inline solution didnt work for @Anahit DEV because it should be:

        function print_my_inline_script_custom() {
        echo '<script type="text/javascript">
                // Document ready
                jQuery(document).ready(function(){
                    alert("call");
                });
                </script>';
    }
    
    add_action( 'wp_footer', 'print_my_inline_script_custom' );
    

    The function call was not corresponding the action hook name.

  2. Proper way to enqueue your script.

    <?php
    function _adding_scripts() {
    wp_register_script('my_custom_script', get_template_directory_uri() . '/js/custom_script.js', array('jquery'),'1.1', true);
    wp_enqueue_script('my_custom_script');
    }
    
    add_action( 'wp_enqueue_scripts', '_adding_scripts' );  
    ?>
    

    custom_script.js file contains below code.

    jQuery(document).ready(function(){
        alert('call');
    });
    

    For inline script in functions.php you need to call your script/code when document ready or jQuery(document).ready() load jQuery(window).load()

    <?php 
    function print_my_inline_script_custom() {
        echo '<script type="text/javascript">
                // Document ready
                jQuery(document).ready(function(){
                    alert("call");
                });
                // OR window load
                jQuery(window).load(function(){
                    alert("call");
                });
                </script>';
    }
    
    add_action( 'wp_footer', 'print_my_inline_script' );
    

Comments are closed.