0

To include new functions we can write them in the theme's functions.php. But I have to write my own functions in a new file rather altering the theme's function.php. I can write any custom js action in footer.php, but I need to write it in other file. How can I do this? Is there any correct way to do this?

1

You can simply use require, require_once, include or include_once functions to include custom PHP files in your theme.

require( 'folder/custom.php' );

Usually you should keep your custom PHP files in a folder.

Developers vote against using require_once because it is a little slower than require since it requires the system to keep a log of what's already been included/required.

| improve this answer | |
  • 2
    With technology advances since 2008 when that question was asked and answered, this should not be an issue anymore, when used correctly. All include functions will be slower, but it will nearly be unnoticeble. :-) – Pieter Goosen Jul 23 '15 at 5:24
0

For adding JavaScript code in WordPress, you can create a .js file and include it in following way, in your theme's functions.php

/**
 * Proper way to enqueue scripts
 */
function theme_name_scripts() {

    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

Refer https://codex.wordpress.org/Function_Reference/wp_enqueue_script

| 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.