ajax调用wordpress函数 ajax调用wordpress functions.php中创建方法 updateCont ,老年人记忆系列!!!
wordpress functions.php中创建方法 updateCont
添加方法 wp_ajax_ {action}未登录调用
添加方法 wp_ajax_nopriv_{action}登录调用
function updateCont(){
global $wpdb;
$post_id = $_POST['post_id'];
$key = $_POST['key'];
$value = $_POST['value'];
update_post_meta($post_id, $key, $value);
echo $value;
die();
}
add_action('wp_ajax_updateCont', 'updateCont');
add_action('wp_ajax_nopriv_updateCont', 'updateCont');
jquery
jQuery.ajax({
type:"POST",
url: "<?php echo admin_url('admin-ajax.php'); ?>",
data: {"post_id":<?php echo get_the_ID();?>,
"action": "updateCont","key":"top_left_content", "value":"new content"},
success:function(data){
console.log(data);// is 0
}
});