Skip to content

Instantly share code, notes, and snippets.

@wolfcoder
Created June 4, 2022 12:43
Show Gist options
  • Select an option

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

Select an option

Save wolfcoder/49bd1fe07eb02b500754eabcdb62b050 to your computer and use it in GitHub Desktop.
call function wp admin plugin through ajax jquery
<?php
/*
Plugin Name: VIN Articles Post
Plugin URI: https://www.vinpractice.com/
Description: Sync / import CDP articles from VIN Api
Version: 1.0.1
Author: VIN Practice
Author URI: https://www.vinpractice.com/
License: GPLv2 or later
*/
if ( ! class_exists( 'VinArticlePost' ) ) {
// plugin definitions
define( 'VAP_PLUGIN', '/vin-article-post/');
// directory define
define( 'VAP_PLUGIN_DIR', WP_PLUGIN_DIR.VAP_PLUGIN);
define( 'VAP_INCLUDES_DIR', VAP_PLUGIN_DIR.'includes/' );
define( 'VAP_ASSETS_DIR', VAP_PLUGIN_DIR.'assets/' );
define( 'VAP_CSS_DIR', VAP_ASSETS_DIR.'css/' );
define( 'VAP_JS_DIR', VAP_ASSETS_DIR.'js/' );
define( 'VAP_IMAGES_DIR', VAP_ASSETS_DIR.'images/' );
// URL define
define( 'VAP_PLUGIN_URL', WP_PLUGIN_URL.VAP_PLUGIN);
define( 'VAP_ASSETS_URL', VAP_PLUGIN_URL.'assets/');
define( 'VAP_IMAGES_URL', VAP_ASSETS_URL.'images/');
define( 'VAP_CSS_URL', VAP_ASSETS_URL.'css/');
define( 'VAP_JS_URL', VAP_ASSETS_URL.'js/');
global $vap_version;
$vap_version = '1.0.1';
class VinArticlePost {
var $vap_setting = '';
function __construct() {
global $wpdb;
$this->vap_sync = 'vap_sync';
$this->vap_setting = 'vap_settings';
register_activation_hook( __FILE__, array( &$this, 'vap_install' ) );
register_deactivation_hook( __FILE__, array( &$this, 'vap_deactivation' ) );
add_action( 'admin_menu', array( $this, 'vap_add_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'vap_enqueue_scripts' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'vap_action_links' ) );
}
static function vap_install() {
global $wpdb, $vap, $vap_version;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
update_option( "vap_plugin", true );
update_option( "vap_version", $vap_version );
}
static function vap_deactivation() {
}
function vap_get_sub_menu() {
$vap_admin_menu = array(
array(
'name' => __( 'Sync / Import' ),
'cap' => 'manage_options',
'slug' => $this->vap_sync,
),
array(
'name' => __( 'Settings' ),
'cap' => 'manage_options',
'slug' => $this->vap_setting,
),
);
return $vap_admin_menu;
}
function vap_add_menu() {
$vap_main_page_name = __( 'Vin Articles' );
$vap_main_page_capa = 'manage_options';
$vap_main_page_slug = $this->vap_sync;
$vap_get_sub_menu = $this->vap_get_sub_menu();
add_menu_page($vap_main_page_name, $vap_main_page_name, $vap_main_page_capa, $vap_main_page_slug, array( &$this, 'vap_route' ), 'dashicons-update-alt', 11 );
foreach ($vap_get_sub_menu as $vap_menu_key => $vap_menu_value) {
add_submenu_page(
$vap_main_page_slug,
$vap_menu_value['name'],
$vap_menu_value['name'],
$vap_menu_value['cap'],
$vap_menu_value['slug'],
array( $this, 'vap_route')
);
}
}
function vap_route() {
global $vap_sync, $vap_settings;
if( isset($_REQUEST['page']) && $_REQUEST['page'] != '' ){
switch ( $_REQUEST['page'] ) {
case $this->vap_setting:
$vap_settings->vap_display_settings();
break;
default:
$vap_sync->vap_display_settings();
break;
}
}
}
function vap_admin_slugs() {
$vap_pages_slug = array(
$this->vap_sync,
$this->vap_setting,
);
return $vap_pages_slug;
}
function vap_is_page() {
if( isset( $_REQUEST['page'] ) && in_array( $_REQUEST['page'], $this->vap_admin_slugs() ) ) {
return true;
} else {
return false;
}
}
function vap_enqueue_scripts() {
global $vap_version;
if( $this->vap_is_page() ) {
wp_register_script( 'vap_admin_js', VAP_JS_URL . 'vap_admin_js.js', 'jQuery', $vap_version, true );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'vap_admin_js' );
wp_localize_script( 'vap_admin_js', 'vap_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
}
function vap_action_links( $links ) {
$row_meta = array(
'settings' => '<a href="'. menu_page_url( 'vap_settings', false ) .'">Settings</a>',
);
return array_merge( $links, $row_meta );
}
}
global $vap;
$vap = new VinArticlePost();
include_once( VAP_INCLUDES_DIR . "vap_settings.class.php" );
include_once( VAP_INCLUDES_DIR . "vap_sync.class.php" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment