Zakir
Forum Replies Created
Viewing 2 replies - 1 through 2 (of 2 total)
-
Forum: Developing with WordPress
In reply to: 400 Bad Request and ajax path<?php /** Plugin Name: My Script **/ add_action( 'admin_menu', 'my_script' ); function my_script() { $page_title = 'My Script'; $menu_title = 'My Script'; $capability = 'manage_options'; $menu_slug = 'myscript_viewer'; $function = 'my_script_functions'; add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function ); add_submenu_page( $menu_slug, $page_title, 'Panel', 'manage_options', 'myscript-panel-submenu-page', 'my_script_panel_page' ); } function my_script_functions() { } function my_script_panel_page() { require_once dirname( __FILE__ ) . '/includes/class-myscript-settings.php'; $page = new Myscript_Settings( plugin_basename( __FILE__ ) ); ?> <div class="wrap"> <h1><?php esc_html_e( 'My Script Panel', 'myscript-viewer' ); ?></h1> <?php $page->display_panel(); ?> </div> <?php } function myscript_load_scripts() { wp_enqueue_script( 'myscript', plugin_dir_url( __FILE__ ) . 'js/myscript.js', array( 'jquery' ), '1.0.0', true ); wp_localize_script( 'myscript', 'myscript', array( 'myajaxurl' => admin_url( 'admin-ajax.php' ) ) ); } add_action( 'admin_enqueue_scripts', 'myscript_load_scripts' ); class Myscript_Settings { public function __construct( $plugin_basename ) {} public function display_panel() { ?> <div id="run-myscript"><button>Run</button></div> <?php } } // file js/myscript.js jQuery(document).ready(function($) { jQuery("#run-myscript button").click(function(){ console.log('path ' + myscript.myajaxurl); jQuery.post( myscript.myajaxurl, { action: 'my_script_action', status: true, extract: true }, function(response) { console.log(response); }); }); });
Here are the changes that I made:
- The function names were inconsistent with the plugin name. I changed
My_script
tomy_script
throughout the code. - I changed the class method
DisplayPanel()
todisplay_panel()
. In PHP, method names should be written in camelCase. - The
wp_enqueue_script()
function was missing a dependency array. I added'jquery'
to the dependencies array to make sure that jQuery is loaded before themyscript
script. - I added a new parameter
'1.0.0'
to thewp_enqueue_script()
function to specify the version of the script. - I added a new action
'my_script_action'
to thejQuery.post()
function. This will be used to handle the AJAX request. - I added a callback function to the
jQuery.post()
function. This function will be called when the AJAX request is complete. In this case, it simply logs the response to the console.
Forum: Reviews
In reply to: [ZD Pre Loader] GoodThanks. It’s my pleasure. rightmeowmarketing. If you like this plugin please give 5 start rating.
- The function names were inconsistent with the plugin name. I changed
Viewing 2 replies - 1 through 2 (of 2 total)