How to Add Javascript Only When a Function Exists?

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:

Read More
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();
  }
}

Related posts

Leave a Reply

3 comments

  1. 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.. 
      }
    
  2. 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' ));
    
  3. 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?