javascript - 如何从 WordPress 中的 ajax 处理程序调用我主题的 functions.php 中的方法?

标签 javascript php jquery html wordpress

我的主题的 functions.php 文件中有一个方法,我想通过 ajax 处理程序调用它。

拥有 ajax 处理程序的目的是避免为每个 ajax 请求加载 wordpress 堆栈(包括插件)..

函数.php

function klarna_checkout_sc() {
    echo do_shortcode('[woocommerce_klarna_]');
    exit();
}

add_action('wp_ajax_nopriv_klarnacheckoutsc', 'klarna_checkout_sc');
add_action('wp_ajax_klarnacheckoutsc', 'klarna_checkout_sc');

jquery 脚本:

  $.ajax({
    type: 'GET',
    url: 'https://mywebsite/wp-content/plugins/zb-ajax-handler/zbajaxhandler.php',
    cache: false,
    data: { action: 'klarnacheckoutsc'},
    success: function(data) {
    },
    error: function(xhr,status,error) {
      console.log('klarnacheckoutsc error:'+error);
    }
  });

zbajaxhandler.php:(ajax 处理程序)

<?php

define('SHORTINIT',true);
require_once('../../../wp-load.php');

klarna_checkout_sc();

echo "success";

?>

klarna_checkout_sc() 添加到 ajax 处理程序文件中时,我遇到了上面的 500 内部服务器错误

你知道如何正确调用 klarna_checkout_sc() 方法到我的事件 Divi 主题吗?

感谢任何帮助。谢谢

最佳答案

尝试使用 Action Hook 而不是创建调用短代码的函数。通过替换此行在您的 ajax 处理程序中调用它

klarna_checkout_sc();

do_action( 'kco_wc_before_checkout_form' );

来源是https://docs.woocommerce.com/document/klarna-checkout-hooks-actions-filters/

关于javascript - 如何从 WordPress 中的 ajax 处理程序调用我主题的 functions.php 中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53444949/

相关文章:

javascript - 将脚本标记作为字符串加载并将其附加到 html header

javascript - TinyMCE 和 wiris 插件 - 保存并加载后,方程不显示(显示丢失的图像)

php - Docker:从一个容器中的应用程序和服务器登录

Javascript 从数组中删除重复的对象

javascript - 在调整大小和浏览器宽度时触发 jQuery

jQuery $.getJSON : "Failed to load resource: cancelled"

javascript json格式化键值

javascript - 在 mapStateToProps 中应用参数

JavaScript 替换正则表达式

php - [a-zA-Z]+ 会消除任何 xss 攻击的机会吗?