Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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();
  }
}
share|improve this question
    
@Denis Belousov - Where are those functions declared? –  MikeSchinkel Dec 24 '10 at 0:33
    
@MikeSchinkel - This function is declared in another file, not in this. This file is just for scripts only. Bit of code that I show is a part of function that register all my javascript. With this function all is fine, problem with this bit of code. I hope I answered your question. –  Denis Belousov Dec 24 '10 at 0:42
    
@Denis Belousov - Yes, but how do I know from this code whether or not the functions you refer to have been required/included or not? I don't, which is why I asked because it affects the return value of function_exists (). (You did way you were new to PHP, right? Sometimes we ask questions because we know PHP and thus know we need to ask. :) –  MikeSchinkel Dec 24 '10 at 0:46
    
@MikeSchinkel- I'm not sure if I understand your question, but I update my post with more info. –  Denis Belousov Dec 24 '10 at 1:11
    
@Denis Belousov - Thanks. Your update gave me the info I needed. Given what you have you've defined all functions so your function_exists() calls will always return true. Can you update your example to explain your use-case instead of using abstract names like 'function_one' so I can understand what you are actually trying to do? (Often people ask the wrong question and understanding your goal helps us understand how to answer.) –  MikeSchinkel Dec 24 '10 at 2:02
show 3 more comments

3 Answers

up vote 2 down vote accepted

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?

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()) {
      $sliders = get_option('src_switcher'); 
      if ($sliders == 'First slider') 
        wp_enqueue_script( 'jquery1',
          get_bloginfo('template_directory').'/js/jquery.plugin.js', 
          array( 'jquery' ));
      else if ($sliders == 'Second slider') 
        wp_enqueue_script('jquery2',          
          get_bloginfo('template_directory').'/js/jquery.plugin2.js', 
          array( 'jquery' ));
      else if ($sliders == 'Third slider') 
        wp_enqueue_script( 'jquery3',
          get_bloginfo('template_directory').'/js/jquery.plugin3.js', 
          array( 'jquery' )); 
    }
  }
}
add_action('wp_print_scripts', 'my_scripts');

Alternately if you must use the function testing you'll want the code that defines your functions to look like this:

$sliders = get_option('src_switcher'); 
if ($sliders == 'First slider') 
  function function_one() {
    ...code.. 
  }
else if($sliders == 'Second slider') 
  function function_two() {
    ...code.. 
  }
else if($sliders == 'Third slider') 
  function function_three() {
    ...code.. 
  }
share|improve this answer
    
Ohhh, it's a good idea. I tried this method few days ago but instead $sliders = get_option('src_switcher') I used global $sliders and nothing. Thank you, this work. –  Denis Belousov Dec 24 '10 at 8:44
    
@Denis Belousov - You are welcome. Please remember to vote up all good answers here on WordPress Answers and also to select the best answer when you've gotten a good answer to your question(s). FYI, doing so will motivate the people here to continue to answer your questions. –  MikeSchinkel Dec 24 '10 at 9:08
add comment

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:

$sliders = get_option('src_switcher');

if($sliders == 'First slider')
    $plugin = 'jquery.plugin.js';

elseif($sliders == 'Second slider')
    $plugin = 'jquery.plugin2.js';

elseif($sliders == 'Third slider')
    $plugin = 'jquery.plugin3.js';

wp_enqueue_script( 'jquery-slider', get_bloginfo('template_directory').'/js/'.$plugin , array( 'jquery' ));
share|improve this answer
    
Another good idea. Thank you. –  Denis Belousov Dec 24 '10 at 8:49
add comment

Check your logic...

You say that you have 3 functions defined in a file.

if(condition_1_true)      // in your case it is
  execute_function_1

elseif(condition_2_true)  // this one is true also, but never gets executed because first one was true too
  execute_function_2

elseif(condition_3_true)  // same here
  execute_function_3

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?

share|improve this answer
    
At first I've use only "if", but on another forum someone tell me that I should use "elseif". In both case nothing. What's your advice instead function_exists()? –  Denis Belousov Dec 24 '10 at 1:53
    
what are you trying to do exactly? –  onetrickpony Dec 24 '10 at 2:01
    
Amoeba - see my comment above. –  Denis Belousov Dec 24 '10 at 2:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.