在Wordpress中将jQueryAjax传递给PHP之后,如何在用于其他函数之前更新全局变量(而不使用async=false)?
第一次过帐到StackOverflow。(感谢您帮助我融入社区) 尽管我对编码基本上是新手,但我还是能够在Wordpress中完成以下步骤,这要感谢Zac Gordon在他概述的步骤之后提供的非常有用的视频和演示文件:在Wordpress中将jQueryAjax传递给PHP之后,如何在用于其他函数之前更新全局变量(而不使用async=false)?,php,jquery,ajax,wordpress,global-variables,Php,Jquery,Ajax,Wordpress,Global Variables,第一次过帐到StackOverflow。(感谢您帮助我融入社区) 尽管我对编码基本上是新手,但我还是能够在Wordpress中完成以下步骤,这要感谢Zac Gordon在他概述的步骤之后提供的非常有用的视频和演示文件: 通过wp_本地化_脚本传递Nonce和ajaxurl 在JavaScript中使用Nonce和URL进行AJAX调用 将PHP AJAX函数挂钩到Wordpress AJAX挂钩中 使用JavaScript处理AJAX响应 虽然我能够使用AJAX响应返回预期结果(可以打印到控制台
<?PHP
global $heights;
if ( null == $heights ) {
add_option( 'jsforwp_heights', 100 );
}
$heights = get_option( 'jsforwp_heights' );
function my_theme_scripts() {
wp_enqueue_script( 'my-great-script', get_template_directory_uri() . '/assets/js/my-great-script.js', array( 'jquery' ), '1.12.4', true );
wp_localize_script(
'my-great-script',
'jsforwp_globals',
[
'ajax_url' => admin_url('admin-ajax.php'),
'total_heights' => get_option('jsforwp_heights'),
'nonce' => wp_create_nonce('nonce_name')
]
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );
function screen_height_is(){
check_ajax_referer('nonce_name');
global $heights;
$heights = $_POST['height'];
update_option('jsforwp_heights',$heights);
// set_map_height($heights); //test
$response['success'] = true;
$response['height'] = $heights;
$response = json_encode($response);
echo $response;
wp_die();
}
add_action('wp_ajax_screen_height_is','screen_height_is',1);
add_action( 'wp_ajax_nopriv_screen_height_is', 'screen_height_is',1 );
function set_map_height(){
global $heights;
$testA = get_option('jsforwp_heights');
$testB = "px";
$height = $testA. $testB;
echo $height;
return $height;
}
add_filter('storymaps_heights','set_map_height',10);
//tests for function above
//function set_map_height($heights){
//$testA = '300'; //static option
//$testA = $heights; //works with same loading delay problem
//$testA = $response['height']; //doesn't work even if $response set as global
控制台日志打印:
{身高:“598”,成功:真的}
完整的
我有点不清楚是否/如何修改成功回调以实现我的总体目标?或者是否需要修改wp\u enqueue\u script()依赖项
如果这有帮助,PHP中的以下过滤器将挂接到storymaps-core.PHP文件中:
以下wp\u排队\u脚本驻留的位置:
function storymap_external_resources() {
wp_enqueue_style( 'storymap-stylesheet', 'https://cdn.knightlab.com/libs/storymapjs/latest/css/storymap.css' );
wp_enqueue_script( 'storymap-javascript', 'https://cdn.knightlab.com/libs/storymapjs/latest/js/storymap-min.js', array(), '1.0.0', false );
}
add_action( 'wp_enqueue_scripts', 'storymap_external_resources' );
非常感谢任何关于下一步尝试的帮助 尝试使用console.log或JSON.stringify(response)@KevinP:console.log(函数(response){console.log(response);回调当前已返回:{height:“615”,success:true}。然后,如果我收缩高度并重新加载页面,它将返回:{height:“507”,success:true}。因此,这会正确地传递到函数屏幕上。\u height_is()$heights=$\u POST['height']。但是在函数集加载到页面之前,不会更新它。Re:JSON.stringify(response),您是指“出错”警报吗?我尝试更改为if('success'==JSON.stringify(response)):但是运气不好。我修改了原来的帖子,将if('success'==response){替换为if(response['success']==true){以进入“出错”警报,并删除了我问题中与此部分相关的最后一部分。
add_filter('storymaps_heights','set_map_height',10);
function storymap_external_resources() {
wp_enqueue_style( 'storymap-stylesheet', 'https://cdn.knightlab.com/libs/storymapjs/latest/css/storymap.css' );
wp_enqueue_script( 'storymap-javascript', 'https://cdn.knightlab.com/libs/storymapjs/latest/js/storymap-min.js', array(), '1.0.0', false );
}
add_action( 'wp_enqueue_scripts', 'storymap_external_resources' );