简体   繁体   English

在检查数据库之后,如何在wordpress functions.php文件中添加javascript

[英]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 如何在此代码中的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' );
            }

} }

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? 您可以在代码中看到它,但是没有用。如何在代码中添加两行jquery?

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. custom_script.js文件包含以下代码。

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() 对于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's inline solution didnt work for @Anahit DEV because it should be: @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' );

The function call was not corresponding the action hook name. 该函数调用与动作挂钩名称不对应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用wp_enqueue_script()在Wordpress-functions.php中添加Javascript - Add Javascript in Wordpress - functions.php with wp_enqueue_script() 在function.php中排队脚本后,Javascript无法在wordpress上运行 - Javascript not working on wordpress after enqueue script in functions.php 使用functions.php文件在wordpress主题中包含javascript文件 - include javascript files in wordpress theme using functions.php file WordPress-functions.php将不会注册我的JavaScript文件 - WordPress - functions.php will not register my JavaScript file WordPress 在函数中使用 JavaScript 将 CSS 文件排队。ZE1BFD762321E409CEEZBAC01 - WordPress enqueuing a CSS file using JavaScript inside Functions.php Javascript文件在Wordpress中无法正常运行,而在Function.php中却无法正确入队 - Javascript File Not Functioning in Wordpress w/ Proper Enqueueing in Functions.php 如何在WordPress子主题中的functions.php上添加多个脚本? - How to add multiple script on functions.php in wordpress child theme? 如何在 WordPress 函数.php 上添加 datalayer.push? - How to add datalayer.push on WordPress functions.php? 如何在函数中添加/关闭简码。WordPress 中的 php - How to add/close a shortcode to functions.php in WordPress 如何在Wordpress的functions.php中编写Javascript函数? - How to write a Javascript function inside the functions.php of Wordpress?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM