I have a plugin that will allow administrators to perform certain actions when adding or editing a post. I use a stylesheet and a javascript for this plugin that I want to include only when a post is being added or edited. Am I right to use the following action hooks?
add_action('load-post.php', 'call_my_function');
add_action('load-post-new.php', 'call_my_function');
Inside of the function call_my_function I have:
function call_my_function() {
$plugin_directory = "/wp-content/plugins/".dirname(plugin_basename(__FILE__));
$jssrc = $plugin_directory.'/js/my_plugin.js';
wp_enqueue_script("my_plugin_js", $jssrc);
$csssrc = $plugin_directory.'/css/my_plugin.css';
wp_enqueue_style("my_plugin_css", $csssrc);
}
The code above successfully loads the CSS and Javascript files when not called from the add_action hook. It does not work successfully when called from these hooks.
you want to use
admin_print_scripts-(page_hook)
andadmin_print_styles-(page_hook)
, so in your case:I suggest you check the source of the page, those hooks work just fine for enqueues. I copied your code to check anyway, and i see both the stylesheet and script enqueued when using the hooks..
The way you’re loading the enqueues looks wrong though, i’d suggest the following..
this one works for me, including the scripts on a customsubmenu page anyway:
shorthand this could translate for you to:
Don’t forget to load after jQuery if your js code depents on it.
I dit not test this, so do with it as you please 🙂