I have a javascript file and a corresponding css file. Both files should be embedded at a single wordpress page. I managed to load the javascript file, but how can a css file be loaded from/at a page?
2 Answers
You should be doing JS and CSS loading with wp_enequeue_script
and wp_enqueue_style
. Register the script at the init
hook (or later) and then enquque it at the wp_print_scripts
hook. Using this, you can say something like if( is_page( 42 ) ) : wp_enqueue_script( 'my_script' ); endif;
and just call it a day.
Additional Functions: wp_register_script()
, wp_register_style
-
Thanks for the hint! Would it be save/advisable to call wp_enqueue_... within php code that is placed in the content of the page and executed by Exec-PHP plugin? <wordpress.org/extend/plugins/exec-php> Commented Jan 22, 2012 at 20:38
-
You should avoid modifying plugins and core files as this will cause your modifications to be removed and potentially break should the file you modified be updated. The correct way to do this is to register and enqueue the files in your theme's function.php file (or your own custom plugin if you're using a theme that's not yours).– mor7iferCommented Jan 22, 2012 at 20:46
How about this? have a new page template, then put the Header in this page, then you can load a different CSS file.
it's not a smart way to do it,i know :(
wp_register_script()
function andwp_enqueue_script
action hook as mentioned before. wordpress.stackexchange.com/questions/39130/…