I have a few function. For both of them need javascript code. I need to add javascript code in header only when function is active. At this time I have all my javascript code in header for all my function and newermind which one of them is active. I tried to make this code but nothing is changed:
All I have in
scripts.php
function my_scripts() {
if (!is_admin()) {
wp_enqueue_script('jquery');
wp_enqueue_script( 'custom',
get_bloginfo('template_directory').'/js/custom.js',
array( 'jquery' ),
'',
true );
if (is_front_page()) {
if (function_exists('function_one'))
wp_enqueue_script( 'jquery1',
get_bloginfo('template_directory').'/js/jquery.plugin.js',
array( 'jquery' ));
else if (function_exists('function_two'))
wp_enqueue_script('jquery2',
get_bloginfo('template_directory').'/js/jquery.plugin2.js',
array( 'jquery' ));
else if (function_exists('function_three'))
wp_enqueue_script( 'jquery3',
get_bloginfo('template_directory').'/js/jquery.plugin3.js',
array( 'jquery' ));
}
}
}
add_action('wp_print_scripts', 'my_scripts');
mycode.php
This code include all my three functions. Looks like:
function function_one() { ?>
...code..
<?php }
function function_two() { ?>
...code..
<?php }
function function_three() { ?>
...code..
<?php }
This two files are defined in functions.php
.
I’m new in php, please help me how should I need to do? Thank you!
UPDATE
Function for choosing slider’s looks like this
function sliders {
$sliders = get_option('src_switcher'); // array in theme options with sliders name
if($sliders == 'First slider') {
function_one();
}
if($sliders == 'Second slider') {
function_two();
}
if($sliders == 'Third slider') {
function_three();
}
}
Hi @Denis Belousov:
Okay, based on your responses I’m going to ask why tie it to the function existing, why not just test the return value of
get_option('src_switcher')
like this?Alternately if you must use the function testing you’ll want the code that defines your functions to look like this:
Basically your approach fails because
function_exist()
simply check for function definition. It doesn’t care if function is ever run or anything like that.In your case I think it makes sense to handle enqueue in same way as your slider choice, something like this:
Check your logic…
You say that you have 3 functions defined in a file.
So remove “else”, use “if”. And what’s the point of using function_exists() if you know you have 3 functions somewhere in your app?