search ]

Enqueue Scripts Only for Specific Admin Pages

Load scripts and styles only on specific WordPress admin pages to avoid conflicts and keep the dashboard fast. For more on optimizing script loading.

add_action( 'admin_enqueue_scripts', function ( $hook ) {
    // Only load on the post edit screen
    if ( $hook !== 'post.php' && $hook !== 'post-new.php' ) {
        return;
    }

    wp_enqueue_style(
        'savvy-admin-custom',
        get_template_directory_uri() . '/css/admin-custom.css',
        array(),
        '1.0.0'
    );

    wp_enqueue_script(
        'savvy-admin-custom',
        get_template_directory_uri() . '/js/admin-custom.js',
        array( 'jquery' ),
        '1.0.0',
        true
    );
} );

Add to functions.php. The $hook parameter tells you which admin page is loading. Common values: post.php, edit.php, upload.php, options-general.php.

Join the Discussion
0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo