[英]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.