slurpage



Wordpress Example Code - how to include a stylesheet and javascript file from your plugin file

If you are trying to add a stylesheet or javascript file from a wordpress plugin, to the best of my knowledge this is how you do it the right way, although perhaps not the best way… see How a Jedi Master Loads Scripts

To add a stylesheet:

function soompi_itunes_load_stylesheet() {  $url = plugins_url('/path/to/file.css', __FILE__);  wp_register_style('pick_a_unique_name', $url); wp_enqueue_style( 'pick_a_unique_name');  } add_action('wp_print_styles', 'soompi_itunes_load_stylesheet');

You can also get the url like this:

$url = WP_PLUGIN_URL . '/soompi-itunes/_inc/css/itunes.css';

But this may cause problems if connecting over ssl. Don’t ask why, I just read it somewhere.

To add a script:

function soompi_itunes_load_script() {  $url = plugins_url('/path/to/file.js', __FILE__);  wp_register_script('pick_a_unique_name', $url, array('jquery'), 1.0); wp_enqueue_script( 'pick_a_unique_name');  } add_action('init', 'soompi_itunes_load_script');

You will notice this is basically the same code. The only real difference is that you are using [wp_register_script] instead of [wp_enqueue_script].

For more a more detailed explanation see the wp codex

Posted via email from adlatitude | Comment »

Notes

  1. drebabels posted this