Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function addActionsAndFilters() {
- // Add options administration page
- // http://plugin.michael-simpson.com/?page_id=47
- add_action('admin_menu', array(&$this, 'addSettingsSubMenuPage'));
- // Example adding a script & style just for the options administration page
- // http://plugin.michael-simpson.com/?page_id=47
- // if (strpos($_SERVER['REQUEST_URI'], $this->getSettingsSlug()) !== false) {
- // wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__));
- // wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__));
- // }
- // Add Actions & Filters
- // http://plugin.michael-simpson.com/?page_id=37
- // Adding scripts & styles to all pages
- // Examples:
- // wp_enqueue_script('jquery');
- // wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__));
- // wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__));
- // Needed for the Settings Page
- if (strpos($_SERVER['REQUEST_URI'], $this->getSettingsSlug()) !== false) {
- wp_enqueue_style('styles', plugins_url('/css/styles.css', __FILE__));
- wp_enqueue_style('jquery-ui', plugins_url('/css/jquery-ui.css', __FILE__));
- wp_enqueue_script('jquery-ui-core');
- wp_enqueue_script('jquery-ui-tabs');
- // enqueue any othere scripts/styles you need to use
- //Ajax shit
- wp_enqueue_script('AjaxHandler', plugins_url() . '/colaren/js/ajax_calls.js', array('jquery'));
- # Here we send PHP values to JS
- wp_localize_script(
- 'AjaxHandler', 'AjaxHandler', array(
- 'ajaxurl' => admin_url('admin-ajax.php'),
- 'ajaxnonce' => wp_create_nonce('my_ajax_validation') // <--- Security!
- )
- );
- }
- // Register short codes
- // http://plugin.michael-simpson.com/?page_id=39
- // Register AJAX hooks
- // http://plugin.michael-simpson.com/?page_id=41
- //More Ajax shit
- add_action('wp_ajax_AjaxAddDev', 'AjaxAddDev');
- add_action('wp_ajax_nopriv_AjaxAddDev', 'AjaxAddDev');
- }
- //Adding a development function for the ajax call.
- public function AjaxAddDev() {
- check_ajax_referer('my_ajax_validation', 'security');
- if (isset($_POST['name']) && isset($_POST['desc'])) {
- $name = $_POST['name'];
- $desc = $_POST['desc'];
- }
- if (!$name && !$desc) {
- wp_send_json_error(array('error' => __('Could not retrieve a post.')));
- } else {
- wp_send_json_success("Name: "+$name+" Description: "+$desc);
- }
- die("wtf");
- }
- //THE JS File
- jQuery(document).ready(function($) {
- jQuery('#add_dev').bind('submit', function(e) {
- e.preventDefault();
- var data = {
- action: 'AjaxAddDev',
- security: AjaxHandler.ajaxnonce,
- name: $('#add_dev').find('input[name="name"]').val(),
- desc: $('#add_dev').find('input[name="desc"]').val()
- };
- $.post(
- AjaxHandler.ajaxurl,
- data,
- function(response) {
- // ERROR HANDLING
- if (!response.success) {
- // No data came back, maybe a security error
- if (!response.data) {
- //$('#my-answer').html('AJAX ERROR: no response');
- alert(response.data);
- } else {
- //$('#my-answer').html(response.data.error);
- alert(response.data);
- }
- } else {
- //$('#my-answer').html(response.data);
- alert(response.data);
- }
- }
- );
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment