9. Load file wpf-themplate_ct.js và phương thức Save (ok)
C:\xampp\htdocs\test\wp-content\plugins\plugin-name\admin\class-wpf-admin_ct.php
<?php
class WPF_Admin_CT {
private $plugin_name;
private $version;
public function __construct($plugin_name, $version) {
$this->plugin_name = $plugin_name;
$this->version = $version;
add_action('admin_menu', array($this, 'add_plugin_admin_menu_ct'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'), 11);
add_action('wp_ajax_wpf_add_ct', array($this, 'add_template'));
add_action('wp_ajax_wpf_ct_ajax_themes_save',array($this,'save_themplate'));
}
public function add_plugin_admin_menu_ct() {
add_menu_page(
__('Product Filters', 'wpf_ct'), __('Lionel Product Filters', 'wpf_ct'), 'manage_options', 'wpf_search_ct', array($this, 'display_search_forms_ct'), 'dashicons-welcome-write-blog', '50'
);
$this->plugin_about_page();
}
public function plugin_about_page() {
add_submenu_page(
'wpf_search_ct',
__('About', 'wpf'),
__('About', 'wpf'),
'manage_options',
'wpf_about',
array($this, 'create_about_page')
);
}
public function display_search_forms_ct() {
include_once 'partials/list_ct.php';
}
public function enqueue_scripts($hook) {
$screen = get_current_screen();
if ($screen->id != 'customize') {
$plugin_dir = plugin_dir_url(__FILE__);
wp_register_script($this->plugin_name, $plugin_dir . 'js/wpf-admin_ct.js', array('jquery'), $this->version, false);
wp_enqueue_script($this->plugin_name);
wp_enqueue_style($this->plugin_name, $plugin_dir . 'css/wpf-themplate.css', array(), $this->version, 'all');
wp_enqueue_script($this->plugin_name.'-template', $plugin_dir . 'js/wpf-themplate_ct.js', array('jquery'), $this->version, false);
}
}
public function add_template() {
check_ajax_referer($this->plugin_name . '_edit', 'nonce', true);
if (current_user_can('manage_options')) {
if ($_REQUEST['action'] === 'wpf_edit' && !empty($_REQUEST['slug'])) {
global $cpt_id;
$cpt_id = sanitize_key($_REQUEST['slug']);
}
include_once 'partials/form_ct.php';
}
wp_die();
}
public function save_themplate(){
check_ajax_referer($this->plugin_name .'_them_ajax', $this->plugin_name .'_nonce', true);
$form = new WPF_Form_CT($this->plugin_name,$this->version);
$result = $form->save_themplate($_POST);
if($result){
echo wp_json_encode($result);
}
wp_die();
}
}
?>
C:\xampp\htdocs\test\wp-content\plugins\plugin-name\admin\js\wpf-themplate_ct.js
var WPF;
(function ($, window, document, undefined) {
// Builder Function
WPF = {
prefix: 'wpf_ct_',
template_type: false,
init: function ($options) {
$options = $.extend({
prefix: this.prefix,
template_type: this.template_type
},$options);
this.prefix = $options.prefix;
this.template_type = $options.template_type;
this.bindEvents();
},
bindEvents: function () {
this.Save();
},
Save: function () {
var self = this;
$('#' + self.prefix + 'submit').click(function (event) {
console.log('aaaaaaaaa');
});
}
}
}(jQuery, window, document));
C:\xampp\htdocs\test\wp-content\plugins\plugin-name\admin\partials\form_ct.php
<?php
global $cpt_id;
?>
<form method="post" action="<?php echo add_query_arg(['action' => $this->plugin_name . '_ajax_themes_save'], admin_url('admin-ajax.php')) ?>">
<input type="hidden" value="<?php echo wp_create_nonce($this->plugin_name . '_them_ajax'); ?>" name="<?php echo $this->plugin_name ?>_nonce"/>
<?php $form = new WPF_Form_CT($this->plugin_name, $this->version, $cpt_id);?>
<?php $form->form();?>
<p class="submit">
<button id="<?php echo $this->plugin_name ?>_submit" class="button button-primary"><?php _e('Save', 'wpf_ct')?></button>
</p>
<div id="<?php echo $this->plugin_name ?>_success_text" class="updated"></div>
<div class="<?php echo $this->plugin_name ?>_wait"></div>
</form>
<script type="text/javascript">
jQuery(function () {
WPF.init({
prefix: '<?php echo $this->plugin_name; ?>_'
});
});
</script>
Last updated
Was this helpful?