Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Load Javascript & CSS Only on Add New Post & Add New Page Screen? WordPress

  • SOLVED

Hi,

I'm looking for a functions.php code snippet that will allow me to load a javascript and or css file <strong>only for the page-new.php and the post-new.php page</strong> in the admin area.

Thanks!

Answers (4)

2011-03-23

Ivaylo Draganov answers:

Hello,

Use something like this:

<?php
// hook to admin pages
add_action( "admin_print_scripts-post-new.php", 'custom_admin_head' );
add_action( "admin_print_scripts-page-new.php", 'custom_admin_head' );
 
function custom_admin_head() {

// register scripts
wp_register_script('myscript', '/path/to/script.js');
// enqueue scripts
wp_enqueue_script('myscript');

// register styles
wp_register_style('mystyle', '/path/to/style.css');
// enqueue styles
wp_enqueue_style('mystyle');

}

2011-03-23

AdamGold answers:

if(strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php') || strstr($_SERVER['REQUEST_URI'], 'wp-admin/page-new.php')) {
wp_enqueue_script('myscript', 'myscriptpath');
wp_enqueue_style('mycss', 'mycsspath');
}

2011-03-23

Peter Michael answers:


function mytheme_header()
{
if(is_admin()){return;}

if(is_page('new'))
{
wp_enqueue_script('myscript', get_stylesheet_directory_uri().'/js/myscript.js', array('jquery'), '1.0.0');
}

if(is_single('new'))
{
wp_enqueue_style( 'mystyle', get_stylesheet_directory_uri().'/css/mystyle.css', '', '1.0.0', 'screen, projection' );
}
}
add_action('wp','mytheme_header');


Peter Michael comments:

Gosh, didn't see that 'admin area' bit, forget my previous answer.

2011-03-23

Daniele Raimondi answers:

function js_on_new_pages_and_posts() {
global $pagenow;
if(is_admin() && ($pagenow=='post-new.php' || $pagenow=='page-new.php ){
wp_enqueue_script('jsscript', get_stylesheet_directory_uri().'/js/jsscript.js', array('jquery'), '1.0.0');
wp_enqueue_style( 'mystyle', get_stylesheet_directory_uri().'/css/mystyle.css', '', '1.0');
}else{
return;
}
}

add_action('init','js_on_new_pages_and_posts');