我创建了一个插件,它利用了wp_ajax_no_priv_action,以便在注销时允许访问。但是,当您登录时,插件只会将数据拉入下拉框。
我在这里俯瞰什么?
<?php
/**
* Plugin Name: Gyms
* Plugin URI: http://www.gyms.com
* Description: This plugin performs dynamic region updates into select boxes in WordPress
* Version: 1.0.0
* Author: Me
* Author Email:
* License: GPL2
*/
function getregions_scripts() {
wp_enqueue_script(
'getregions-script',
plugin_dir_url(__FILE__) . "assets/getregions.js",
array('jquery'),
'1.0',
true
);
wp_localize_script(
'getregions-script', // this needs to match the name of our enqueued script
'gymRegions', // the name of the object
array('ajaxurl' => admin_url('admin-ajax.php')) // the property/value
);
}
add_action( 'wp_enqueue_scripts', 'getregions_scripts' );
add_action( 'wp_ajax_showcountries', 'showcountries_callback' );
add_action( 'wp_ajax_no_priv_showcountries', 'showcountries_callback' );
function showcountries_callback() {
include_once("pdo_mysql.php");
pdo_connect("localhost","user","password");
pdo_select_db("wpdb");
$makeselection=$_POST["makeselection"];
if($makeselection=="getCountryList"){
$showcountry = pdo_query("Select data from usertbl");
if (!$showcountry) {
$message = 'Invalid query: ' . pdo_error() . "\n";
$message .= 'Whole query: ' . $showcountry;
die($message);
}else{
$html = "<option value=''>Select Country</option>";
foreach($showcountry as $row){
$html .= "<option value='{$row[meta_value]}'>{$row[COUNTRY_NAME]}</option>";
}
echo json_encode($html, JSON_PRETTY_PRINT);
}
}
..........
wp_die();
}
function showcountries_frontend() {
$the_html = '
<form id="MyForm">
<div style="float: left">
<select id="CountryList" onchange="getCities()" style="width: 150px !important; min-width: 150px; max-width: 150px;"></select>
<select id="CityList" onchange="getTowns()" style="width: 150px !important; min-width: 150px; max-width: 150px;"></select>
<select id="TownList" style="width: 150px !important; min-width: 150px; max-width: 150px;"></select>
<select id="CategoriesList" style="width: 170px !important; min-width: 170px; max-width: 170px;"></select>
</div>
</form>';
return $the_html;
}
add_shortcode("sc_frontend", "showcountries_frontend");
?>
插件/资产/ getregions.js
function initialize($) {
feedData($);
categoriesData($);
}
jQuery(document).ready(function ($) { initialize($); });
function feedData($) {
jQuery(document).ready(function ($) {
console.log("feedData is successfully hit.");
var serialized = $('#MyForm').serialize();
$.ajax({
cache: false,
type: "POST",
async: false,
url: gymRegions.ajaxurl,
data:{action: "showcountries", makeselection: "getCountryList", serialized}, //In wordpress the action: value must match the name of the callback function in the php file. In this case, showcountries_callback. Otherwise 0 is returned in success:
dataType: "json",
success: function (data, status, error) {
//$('#CountryList').append(data);
console.log(data);
console.log(status);
console.log(error);
},
error: function (data, status, error) {
console.log(data);
console.log(status);
console.log(error);
}
});
});
}
function categoriesData($){
jQuery(document).ready(function ($) {
console.log("categoriesData is successfully hit.");
var serialized = $('#MyForm').serialize();
$.ajax({
cache: false,
type: "POST",
async: false,
url: gymRegions.ajaxurl,
data:{action: "showcountries", makeselection: "categoriesList", serialized},
dataType: "json",
success: function (data) {
//console.log(data);
$('#CategoriesList').append(data);
},
error: function (data, status, error) {
console.log(data);
console.log(status);
console.log(error);
}
});
});
}
.......
我从调试成功获得的读数:函数feedData()的一部分,内容如下:
0
success
Object { readyState: 4, getResponseHeader: .ajax/w.getResponseHeader(), getAllResponseHeaders: .ajax/w.getAllResponseHeaders(), setRequestHeader: .ajax/w.setRequestHeader(), overrideMimeType: .ajax/w.overrideMimeType(), statusCode: .ajax/w.statusCode(), abort: .ajax/w.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 12 more… }