用AJAX调用wordpress短代码 [英] Call wordpress shortcode with AJAX

查看:106
本文介绍了用AJAX调用wordpress短代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用开关按钮运行短代码。如果开关是ON,我会拨打一个短代码,如果它是OFF,我会打电话给另一个。

I would like to run shortcodes using a switch button. If the switch is " ON", I call a shortcode and if it is "OFF" I call another .

作为测试,我尝试在点击时调用一个短代码与AJAX的单一链接,它给了我这个:

As a test I tried calling a shortcode at the click on a single link with AJAX, it gives me this:

文件page-recherche.php:

File "page-recherche.php" :

<a href="" id="clicklien">CLICK HERE</a>


<script>
$("#clicklien").click(function(e){
      e.preventDefault();
    $.ajax({
    url: 'http://www.capitainebar.com/wp-content/themes/Capitaine-Bar/shortcode-recherche.php',
    success: function (data) {
        // this is executed when ajax call finished well
        console.log('content of the executed page: ' + data);
        $('body').append(data);

    },
    error: function (xhr, status, error) {
        // executed if something went wrong during call
        if (xhr.status > 0) alert('got error: ' + status); // status 0 - when load is interrupted
    }
});

});
</script

调用的文件是shortcode-recherche.php:

The file called is "shortcode-recherche.php" :

<?php echo do_shortcode( '[search-form id="1" showall="1"]' ); ?>

结果是致命错误。好像代码是在shortcode-recherche.php而不是page-recherche.php中运行。

The result is a FATAL error . As if the code was running in "shortcode-recherche.php" rather than "page-recherche.php".

请注意,如果直接编写短代码,则可以正常工作进入我的页面,没有AJAX调用。

Notice that the shortcode works fine if I write it directly into my page, without the AJAX call.

你可以看到结果这里

推荐答案

当你直接调用PHP文件时,不涉及WordPress。这意味着像 do_shortcode()这样的函数甚至不存在。

When you call a PHP file directly, WordPress is not involved. This means that functions like do_shortcode() do not even exist.

相反,你需要请求一个文件被WordPress捕获(即使通常是404)。然后,让您的插件知道URL。您可以使用查询变量(简单)或重写规则(困难,更漂亮)来执行此操作。例如:

Instead, you need to request a file that is caught by WordPress (even if normally 404). Then, make your plugin aware of the URL. You can do this with query variables (easy) or rewrite rules (difficult, prettier). For example:

查询变量: example.org/?custom_shortcode=gallery

Rerwrite规则: example.org/custom_shortcode/gallery /

Rerwrite rule: example.org/custom_shortcode/gallery/

无论您选择哪个选项,当您访问此URL并拦截它时,您的插件都需要注意。完成后,您需要退出脚本以防止WordPress尝试显示404页面。

Whichever option you choose, your plugin needs to be aware when you access this URL and intercept it. When you are done, you need to exit the script to prevent WordPress from trying to show a 404 page.

这是一个示例,您可以简单地插入到您的功能中.php文件。

Here is an example which you can simply drop in to your functions.php file.

function shortcode_test() {
  if ( !empty($_REQUEST['shortcode']) ) {
    // Try and sanitize your shortcode to prevent possible exploits. Users typically can't call shortcodes directly.
    $shortcode_name = esc_attr($_REQUEST['shortcode']);

    // Wrap the shortcode in tags. You might also want to add arguments here.
    $full_shortcode = sprintf('[%s]', $shortcode_name);

    // Perform the shortcode
    echo do_shortcode( $full_shortcode );

    // Stop the script before WordPress tries to display a template file.
    exit;
  }
}
add_action('init', 'shortcode_test');

您可以通过访问您的网站来测试这一点,并在网址末尾添加:

You can test this by visiting your site with this added on the end of the URL:

?shortcode = gallery

这应显示图库的短代码扩展为HTML。一旦这个工作,只需将它绑定到您现有的AJAX函数。

This should display the gallery shortcode expanded as HTML. Once this is working, just tie it in to your existing AJAX function.

这篇关于用AJAX调用wordpress短代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆