How to add custom javascript to backend(admin) and frontend of wordpress cms?

Here I'm going to show how to add custom javascript to your backend(admin panel) and frontend of your wordpress website inside <head> tag. I am not talking about wordpress.com(pre hosted) blogging system. Here I'm demonstrating how to do it in self-hosted blog/website.

First you need to go to Theme Editor(Appearance -> Editor). And find functions.php file from right panel. To find it easily click Ctrl + F (In Windows machine) and type functions.php. After you found it click on it and it will open up online editor.

You will see something like following. Coding may differ in your system.


And add following code to the end of functions.php file.

function custom_js() {
    echo '<script type="text/javascript">YOUR CODE HERE</script>';
}

// Add hook for admin <head></head>
add_action('admin_head', 'custom_js');
// Add hook for front-end <head></head>
add_action('wp_head', 'custom_js');

Please remember to edit the my_custom_js function as you needed. And save the file by clicking update button. Please remember you will not see update button if your functions.php file is not writable.

Please comment and let me know if you find any problems.

Comments

Popular posts from this blog

Offers on Friday, April 24, 2020

Fatal: LoadModule: error loading module 'mod_sql_mysql.c'