Guest User

Untitled

a guest
Nov 1st, 2013
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. public function addActionsAndFilters() {
  2.  
  3. // Add options administration page
  4. // http://plugin.michael-simpson.com/?page_id=47
  5. add_action('admin_menu', array(&$this, 'addSettingsSubMenuPage'));
  6.  
  7. // Example adding a script & style just for the options administration page
  8. // http://plugin.michael-simpson.com/?page_id=47
  9. // if (strpos($_SERVER['REQUEST_URI'], $this->getSettingsSlug()) !== false) {
  10. // wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__));
  11. // wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__));
  12. // }
  13. // Add Actions & Filters
  14. // http://plugin.michael-simpson.com/?page_id=37
  15. // Adding scripts & styles to all pages
  16. // Examples:
  17. // wp_enqueue_script('jquery');
  18. // wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__));
  19. // wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__));
  20. // Needed for the Settings Page
  21. if (strpos($_SERVER['REQUEST_URI'], $this->getSettingsSlug()) !== false) {
  22. wp_enqueue_style('styles', plugins_url('/css/styles.css', __FILE__));
  23. wp_enqueue_style('jquery-ui', plugins_url('/css/jquery-ui.css', __FILE__));
  24. wp_enqueue_script('jquery-ui-core');
  25. wp_enqueue_script('jquery-ui-tabs');
  26. // enqueue any othere scripts/styles you need to use
  27.  
  28.  
  29. //Ajax shit
  30. wp_enqueue_script('AjaxHandler', plugins_url() . '/colaren/js/ajax_calls.js', array('jquery'));
  31. # Here we send PHP values to JS
  32. wp_localize_script(
  33. 'AjaxHandler', 'AjaxHandler', array(
  34. 'ajaxurl' => admin_url('admin-ajax.php'),
  35. 'ajaxnonce' => wp_create_nonce('my_ajax_validation') // <--- Security!
  36. )
  37. );
  38. }
  39. // Register short codes
  40. // http://plugin.michael-simpson.com/?page_id=39
  41. // Register AJAX hooks
  42. // http://plugin.michael-simpson.com/?page_id=41
  43.  
  44. //More Ajax shit
  45. add_action('wp_ajax_AjaxAddDev', 'AjaxAddDev');
  46. add_action('wp_ajax_nopriv_AjaxAddDev', 'AjaxAddDev');
  47. }
  48.  
  49.  
  50. //Adding a development function for the ajax call.
  51. public function AjaxAddDev() {
  52. check_ajax_referer('my_ajax_validation', 'security');
  53. if (isset($_POST['name']) && isset($_POST['desc'])) {
  54. $name = $_POST['name'];
  55. $desc = $_POST['desc'];
  56. }
  57.  
  58. if (!$name && !$desc) {
  59. wp_send_json_error(array('error' => __('Could not retrieve a post.')));
  60. } else {
  61. wp_send_json_success("Name: "+$name+" Description: "+$desc);
  62. }
  63. die("wtf");
  64. }
  65.  
  66.  
  67.  
  68. //THE JS File
  69. jQuery(document).ready(function($) {
  70. jQuery('#add_dev').bind('submit', function(e) {
  71. e.preventDefault();
  72. var data = {
  73. action: 'AjaxAddDev',
  74. security: AjaxHandler.ajaxnonce,
  75. name: $('#add_dev').find('input[name="name"]').val(),
  76. desc: $('#add_dev').find('input[name="desc"]').val()
  77. };
  78. $.post(
  79. AjaxHandler.ajaxurl,
  80. data,
  81. function(response) {
  82. // ERROR HANDLING
  83. if (!response.success) {
  84. // No data came back, maybe a security error
  85. if (!response.data) {
  86. //$('#my-answer').html('AJAX ERROR: no response');
  87. alert(response.data);
  88. } else {
  89. //$('#my-answer').html(response.data.error);
  90. alert(response.data);
  91. }
  92. } else {
  93. //$('#my-answer').html(response.data);
  94. alert(response.data);
  95. }
  96. }
  97. );
  98. });
  99. });
Advertisement
Add Comment
Please, Sign In to add comment