簡體   English   中英

在檢查數據庫之后,如何在wordpress functions.php文件中添加javascript

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

如何在此代碼中的wordpress functions.php文件中添加內聯JavaScript

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' . "\r\n"; 
            $header .= "MIME-Version: 1.0" . "\r\n";
            $header .= "Content-type:text/html;charset=utf-8" . "\r\n";
            $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' );
            }

}

我嘗試了以下操作

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' );

您可以在代碼中看到它,但是沒有用。如何在代碼中添加兩行jquery?

正確排隊腳本的正確方法。

<?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文件包含以下代碼。

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

對於functions.php內聯腳本,您需要在文檔就緒或jQuery(document).ready()加載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' );

@noman的內聯解決方案不適用於@Anahit DEV,因為它應該是:

    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' );

該函數調用與動作掛鈎名稱不對應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM