Advertisement
Guest User

Wordpress Custom Plug (how to call multiple javascript files)

a guest
Feb 12th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Plugin Name: Tab Header Code
  5. Plugin URI: [insert the plugin uri here]
  6. Description: Inserts appropriate javascript and css libraries for tabs
  7. Author: Jesse Wallace
  8. Version: 0.1
  9. Author URI: http://www.enticent.com
  10. Generated At: www.wp-fun.co.uk;
  11. */
  12.  
  13. if (!class_exists('tabstyles')) {
  14. class tabstyles {
  15.  
  16. /**
  17. * PHP 4 Compatible Constructor
  18. */
  19.  
  20. function tabstyles(){$this->__construct();}
  21.  
  22. /**
  23. * PHP 5 Constructor
  24. */
  25.  
  26. function __construct(){
  27. add_action("init", array(&$this,"add_script1"));
  28. add_action("init", array(&$this,"add_script2"));
  29. add_action("init", array(&$this,"add_script3"));
  30. add_action("wp_head", array(&$this,"add_css"));
  31. }
  32.  
  33. /**
  34. * Tells WordPress to load the scripts
  35. */
  36.  
  37. function add_script1(){
  38. wp_enqueue_script('tab_header_code_script1', '/wp-content/plugins/tab-header-code/js/tabview-min.js', NULL , 0.1);
  39. }
  40.  
  41. function add_script2(){
  42. wp_enqueue_script('tab_header_code_script2', '/wp-content/plugins/tab-header-code/js/element-min.js', NULL , 0.1);
  43. }
  44.  
  45. function add_script3(){
  46. wp_enqueue_script('tab_header_code_script3', '/wp-content/plugins/tab-header-code/js/yahoo-dom-event.js', NULL , 0.1);
  47. }
  48.  
  49. /**
  50. * Adds a link to the stylesheet to the header
  51. */
  52.  
  53. function add_css(){
  54. echo '<link rel="stylesheet" href="'.get_bloginfo('wpurl').'/wp-content/plugins/tab-header-code/css/tabstyle.css" type="text/css" media="screen" />';
  55. }
  56. }
  57. }
  58.  
  59. //instantiate the class
  60. if (class_exists('tabstyles')) {
  61. $tabstyles = new tabstyles();
  62. }
  63.  
  64. ?>
  65.  
  66. add_action('admin_menu', 'add_pages');
  67.  
  68. function add_pages() {
  69. $my_plugin_page = add_menu_page('Plugin Name', 'Plugin Name', 8,__FILE__, 'invoice_overview',"images/plugin_icon.png");
  70.  
  71. // This is the trick, notice the "$my_plugin_page" being tacked on the end of admin_print_scripts
  72. add_action( "admin_print_scripts-$my_plugin_page", 'admin_head');
  73.  
  74. }
  75.  
  76. function admin_head() {
  77. global $path_to_your_files;
  78.  
  79. // You may notice array('jquery') at the end, that means jQuery is required to make that particular file function, and WP will include it automatically
  80. wp_enqueue_script('jquery.whatever',$path_to_your_files . "/js/jquery.whatever.js", array('jquery'));
  81. wp_enqueue_script('jquery.autocomplete',$path_to_your_files . "/js/jquery.autocomplete.js", array('jquery'));
  82. wp_enqueue_script('your_custom_file',$path_to_your_files . "/js/your_custom_file.js", array('jquery'));
  83.  
  84. }
  85.  
  86. add_action('wp_head', 'insert_head');
  87.  
  88. function insert_head() {
  89. ?>
  90. <script type="text/javascript" src="<?php bloginfo('wpurl') ?>/wp-content/plugins/tab-header-code/js/tabview-min.js"></script>
  91. <script type="text/javascript" src="<?php bloginfo('wpurl') ?>/wp-content/plugins/tab-header-code/js/element-min.js"></script>
  92. <script type="text/javascript" src="<?php bloginfo('wpurl') ?>/wp-content/plugins/tab-header-code/js/yahoo-dom-event.js"></script>
  93. <link type="text/css" src="<?php bloginfo('wpurl') ?>/wp-content/plugins/tab-header-code/css/tabstyle.csss"></link>
  94. <?php
  95. }
  96.  
  97. <?php
  98.  
  99. if (!class_exists('tabstyles')) {
  100. class tabstyles {
  101.  
  102. /**
  103. * PHP 4 Compatible Constructor
  104. */
  105.  
  106. function tabstyles(){$this->__construct();}
  107.  
  108. /**
  109. * PHP 5 Constructor
  110. */
  111.  
  112. function __construct(){
  113. add_action("init", array(&$this,"on_init"));
  114. }
  115.  
  116. /**
  117. * Tells WordPress to load the scripts
  118. */
  119.  
  120. function on_init(){
  121. // scripts
  122. wp_enqueue_script('tab-view-min', get_bloginfo('wpurl').'/wp-content/plugins/tab-header-code/js/tabview-min.js');
  123. wp_enqueue_script('element-min', get_bloginfo('wpurl').'/wp-content/plugins/tab-header-code/js/element-min.js');
  124. wp_enqueue_script('yahoo-dom-event', get_bloginfo('wpurl').'/wp-content/plugins/tab-header-code/js/yahoo-dom-event.js');
  125.  
  126. //css
  127. wp_enqueue_style('tabstyle', get_bloginfo('wpurl').'/wp-content/plugins/tab-header-code/css/tabstyle.css');
  128.  
  129. }
  130.  
  131. }
  132. }
  133.  
  134. //instantiate the class
  135. if (class_exists('tabstyles')) {
  136. $tabstyles = new tabstyles();
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement