我有一个 [hook] 与我的测试环境中的永久链接 /test/ 一起使用,我想提取某些数据。这放置在我正在开发的插件中。这是我正在做的一个例子:
当你按下按钮时我想要它
<a href="#ajaxthing" class="myajax" data-id="600">
AJAX 调用专门转到一个类。这样我的插件就不会与其他插件发生冲突。
如果还有更好的方法来放置 JavaScript/JQUERY POST,请告诉我。
这是我到目前为止所尝试的:
class step9
{
public function __construct()
{
add_action('init', array($this, 'init'));
add_shortcode('custom_form', array($this, 'shortcode_handler'));
add_action('wp_ajax_custom_action', array($this,'custom_action_callback_wp'));
add_action( 'wp_ajax_nopriv_custom_action', array($this,'custom_action_callback_wp' ));
}
public function init()
{
if (!empty($_POST['step']))
{
function custom_action_callback_wp() {
global $wpdb; // this is how you get access to the database
$getid = 'ID=> '. $_POST['id'];
echo $getid;
exit(); // this is required to return a proper result
}
}
}
function shortcode_handler($atts)
{
echo '<a href="#ajaxthing" class="myajax" data-id="600">Click On this</a>
<div id="wpajaxdisplay">Ajax Result will display here</div>';
}
}
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url_path = parse_url( $url, PHP_URL_PATH );
$slug = pathinfo( $url_path, PATHINFO_BASENAME );
if($slug == "test"){
if(isset($_GET['step'])&& ($_GET['step'] ==9)|| (isset($_POST['step']==9) &&($_POST['step']==9))){
$step = new step9();
}
}
// javascript ajax call with click event
add_action('wp_head', 'ajx_action_javascript_wp');
function ajx_action_javascript_wp() {
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
$('.myajax').click(function(){
var mydata = $(this).data();
$('#wpajaxdisplay').html('<div style="text-align:center;">Loading...</div>');
//console.log(mydata);
var data = {
action: 'custom_action',
step: 9,
//whatever: 1234,
id: mydata.id
};
$.post('<?php echo esc_url( home_url() ); ?>/wp-admin/admin-ajax.php', data, function(response) {
$('#wpajaxdisplay').html(response);
});
});
});
</script>
<?php
}