Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • <?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:

    1. The function names were inconsistent with the plugin name. I changed My_script to my_script throughout the code.
    2. I changed the class method DisplayPanel() to display_panel(). In PHP, method names should be written in camelCase.
    3. 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 the myscript script.
    4. I added a new parameter '1.0.0' to the wp_enqueue_script() function to specify the version of the script.
    5. I added a new action 'my_script_action' to the jQuery.post() function. This will be used to handle the AJAX request.
    6. 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] Good
    Plugin Author Zakir

    (@zakirbd63)

    Thanks. It’s my pleasure. rightmeowmarketing. If you like this plugin please give 5 start rating.

Viewing 2 replies - 1 through 2 (of 2 total)