In this answer, we have covered what is the preferred way to add custom javascript files to the site? Use the following PHP code to add custom javascript files to the site.
1 2 3 4 5 6 7 | <?php add_action( 'wp_enqueue_scripts', 'mysite_enqueue' ); function mysite_enqueue() { $ss_url = get_stylesheet_directory_uri(); wp_enqueue_script( 'mysite-scripts', "{$ss_url}/js/mysite-scripts.js" ); } |
1 2 3 4 5 6 7 | <?php define( 'MY_PLUGIN_VERSION', '2.0.1' ); add_action( 'admin_enqueue_scripts', 'my_plugin_admin_enqueue' ); function my_plugin_admin_enqueue() { wp_enqueue_script( 'my-script', plugins_url( '/js/my-script.js', __FILE__ ), array( 'jquery', 'jquery-ui-sortable' ), MY_PLUGIN_VERSION ); } |
1 2 3 4 5 6 7 | <?php define( 'MY_PLUGIN_VERSION', '2.0.1' ); add_action( 'wp_footer', 'my_plugin_admin_enqueue' ); function my_plugin_admin_enqueue() { wp_enqueue_script( 'my-script', plugins_url( '/js/my-script.js', __FILE__ ), array( 'jquery', 'jquery-ui-sortable' ), MY_PLUGIN_VERSION, true ); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.