how to add script in WordPress admin page
To add a script in a WordPress admin page, follow these steps:
Access the functions.php file: Log in to your WordPress admin dashboard. Go to "Appearance" and then click on "Theme Editor." On the right-hand side, find and click on "Theme Functions (functions.php)".
Insert PHP code: Scroll to the bottom of the functions.php file and add the following PHP code:
php
function custom_admin_script() {
wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-admin-script.js', array('jquery'), '1.0', true);
}
add_action('admin_enqueue_scripts', 'custom_admin_script');
This code creates a function custom_admin_script()
that enqueues a custom JavaScript file named custom-admin-script.js
. Ensure to replace /js/custom-admin-script.js
with the correct path to your script file.
Create your JavaScript file: In your theme folder, create a folder named
js
if it doesn't exist. Inside thejs
folder, create a JavaScript file namedcustom-admin-script.js
and add your script content.Customize the script: Write your JavaScript code within the
custom-admin-script.js
file. This script will now be loaded on all admin pages of your WordPress site.Save Changes: After adding the code to the functions.php file and creating the JavaScript file, click the "Update File" button in the Theme Editor to save your changes.
This process will ensure that your custom script is properly added and executed on WordPress admin pages.