まず、function.phpの中に以下のコードを入れる。
add_action('wp_ajax_tell_me', 'tell_me');
add_action('wp_ajax_nopriv_tell_me', 'tell_me');
function tell_me() {
$id = ---; //反映したいデータが載っているページのID
$res[0] = get_the_title($id);
$res[1] = get_post_meta($id,'latitude',true);
$res[2] = get_post_meta($id,'longitude',true);
echo json_encode($res, JSON_UNESCAPED_UNICODE);
die();
}
さらに、外部のサイトの利用を可能にする”Access-Control-Allow-Origin”をfunction.php内に追加
/** Allow for cross-domain requests (from the front end). */
send_origin_headers();
header("Access-Control-Allow-Origin: *"); //追加
受け取り側のionicの方のindex.htmlには以下を追加
<script>
var wp_url_admin_ajax = 'http://eggboys100.loacl/wp-admin/admin-ajax.php';
jQuery(function ($){
$.ajax({
type: 'POST',
dataType: 'json',
crossDomain: true,
url: wp_url_admin_ajax,
data: {
action : 'tell_me'
},
success: function(response){
mark(response);
}
});
});
</script>
これでfunctionのほうでのfuncion tell meがindex.htmlでのaction:tell meのおかげで連動し作動するはず。


