Instead of calling a script from a file, I put a number of my javascripts in a template file called codes.php.

I then call codes.php in the footer by pasting <?php include (TEMPLATEPATH . '/codes.php'); ?> into footer.php

Should I continue with this method, or should I be saving each script in its own file, upload it to my directory, and then call the scripts individually (rather than having the entire code pasted in codes.php)?

If I go with this second option, should I do away with codes.php altogether or should I put the script tags in codes.php for easier organization and access? Would there be any real drawback to doing this?

  • 1
    You are loading your Javascript by includeing a PHP file? What is in that PHP file? How does this system work? – s_ha_dum Jan 15 '13 at 19:53
  • I edited the question and made it as specific as I possibly could. – AndrettiMilas Jan 15 '13 at 20:10
  • You didn't answer my question. What is in that PHP file? Post the code. I don't know what you are doing so I can't really comment on whether it is 'proper'. – s_ha_dum Jan 15 '13 at 20:15
  • The PHP file (codes.php) is simply a template file added to the theme. I am posting javascript in it, then calling it into footer.php. The only thing in the file is javascripts. – AndrettiMilas Jan 15 '13 at 20:18
  • If you won't answer my question I can't help you. Good luck. – s_ha_dum Jan 15 '13 at 20:19

If I interpret the question correctly you:

  • Don't have any .js javascript files
  • Output any javascript you have from an .php file (presumably code)
  • Include this javascript on the bottom of the page by including the .php file.

So you are actually doing:

  • Correct
    • Combining all the javascript in one file
    • Having the javascript in the footer
    • Minimized the number of javascript files
  • Incorrect
    • Native file type should be .js
    • If you don't add actual php code in the php file, what's the point because:
      • you loose any manipulation by plugins/hooks
      • javascript should be in a javascript file
      • you can not exclude any of it on any page
      • you can not check dependencies

Move all the javascript to a .js file and then enqueue it with

wp_enqueue_script( $handle, $src = false, $deps = array(), 
    $ver = false, $in_footer = false );

So you can use @Furqan is method:

function my_scripts_method() {
        get_template_directory_uri() . '/js/custom_script.js',
add_action('wp_enqueue_scripts', 'my_scripts_method');
  • $handle should be a unique name (so don't use custom-script).
  • $src the path to the file (http:// uri not file path)
  • $deps an array of dependencies. He added jquery for you (handle names)
  • $ver is the version number
  • $in_footer should be true for all non-execute-as-quick-as-possible scripts
| improve this answer | |

I am unable to understand your Question but WordPress provide a good way to use java script files wp_enqueue_script()

function my_scripts_method() {
        get_template_directory_uri() . '/js/custom_script.js',
add_action('wp_enqueue_scripts', 'my_scripts_method');
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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