Skip to content

Instantly share code, notes, and snippets.

@wolfcoder
Created June 4, 2022 02:36
Show Gist options
  • Select an option

  • Save wolfcoder/e97c86810ce3c3ac5757f4ad785a4967 to your computer and use it in GitHub Desktop.

Select an option

Save wolfcoder/e97c86810ce3c3ac5757f4ad785a4967 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Rentalist
Description: Create a plugin with sub menu and script.js jquery included with ajax admin
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// plugin definitions
define( 'RENTALIST_URL', plugin_dir_url( __FILE__ ) );
class Rentalist {
function __construct() {
$this->rentalist_setting = 'rentalist_settings';
$this->rentalist_sync = 'rentalist_sync';
add_action( 'admin_menu', array( $this, 'rentalist_add_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'rentalist_enqueue_scripts' ) );
}
function rentalist_add_menu(): void {
$rentalist_main_page_slug = $this->rentalist_sync;
add_menu_page( "Rentalist", "Rentalist", "manage_options", $rentalist_main_page_slug, array(
$this,
'rentalist_sync'
), 'dashicons-smiley', 7 );
add_submenu_page( $rentalist_main_page_slug, "Sync / Import", "Sync / Import", "manage_options", $this->rentalist_sync, array( $this, "rentalist_sync"), 7 );
add_submenu_page( $rentalist_main_page_slug, "Setting", "Setting", "manage_options", $this->rentalist_setting, array($this,"rentalist_setting"), 7 );
}
function rentalist_enqueue_scripts() {
if ( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], [$this->rentalist_setting,$this->rentalist_sync] ) ) {
wp_enqueue_script( 'rentalist_admin_js', RENTALIST_URL . 'js/rentalist.js', 'jQuery', 1, true );
wp_localize_script( 'rentalist_admin_js', 'rentalist_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
}
function rentalist_sync() {
echo "<div class='wrap'>rentalist_sync</div>";
}
function rentalist_setting() {
echo "<div class='wrap'>rentalist_setting</div>";
}
}
$rentalist = new Rentalist();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment