I am loading some scripts conditionally for the admin section with :
if (is_admin()) {
wp_register_style('admin_js', get_bloginfo('template_directory') . '/admin.js');
wp_enqueue_script('admin_js');
}
The script gets elements with:
theID = document.getElementById('bar').value;
The missing element exists on custom meta boxes
<?php function my_meta() {
global $post;
$custom = get_post_custom($post->ID);
$uniqueInput = $custom["uniqueInput "][0];
?>
<p><label for="foo">Input:</label><br />
<input id="bar" name="bar" value="<?php echo $uniqueInput; ?>"></input></p>
<?php } ?>
I am generating the custom meta boxes only in my custom post type :
register_post_type( 'customType' , $args );
I tried to add another condition in the enqueue :
if (is_admin()) {
if(get_post_meta($post->ID, 'uniqueInput', true)):
wp_register_style('admin_js', get_bloginfo('template_directory') . '/admin.js');
wp_enqueue_script('admin_js');
endif;
and this :
if (is_admin()) {
$foo = get_post_meta($post->ID, 'uniqueInput', true);
if ($foo) {
wp_register_style('admin_js', get_bloginfo('template_directory') . '/admin.js');
wp_enqueue_script('admin_js');
}
But this doesnt work for me. The script is suppressed so I no longer get a missing element error... however this condition is never met, the js never loaded. Now I realize it is because I was making the wrong checks, looking to see if that input has a value entered, not for the input field itself which is what I need. What would be the best way to do this check? Would I look for the custom post type?