【问题标题】:How to integrate ajax to Wordpress theme?如何将 ajax 集成到 Wordpress 主题?
【发布时间】:2015-03-12 19:18:37
【问题描述】:

我正在尝试在 wordpress 中使用 ajax

我有两个文件,php和js

这是我的 php 代码 combo_check-out.php

function iRange($first, $last, $format = 'm/d/Y' ) { 
        $dates = array();
        $current = strtotime($first);
        $i=1;
        while( $i <= $last ) { 
            $dates[] = date($format, $current);
            $current = strtotime('+1 day', $current);
            $i++;
        }

        $time = date("m/d/Y",$current);
        return $time;
    }

if($_REQUEST)
{
    $id     = $_REQUEST['parent_id'];
    ?>


    <select name="check-out"  id="check-out-date">
        <option value="<?php echo iRange($id, 1, $format = 'm/d/Y' ) ?>">"1 Day (Same Day)"</option>
        <option value="<?php echo iRange($id, 2, $format = 'm/d/Y' ) ?>">"2 Days"</option>
        <option value="<?php echo iRange($id, 3, $format = 'm/d/Y' ) ?>">"3 Days"</option>
        <option value="<?php echo iRange($id, 4, $format = 'm/d/Y' ) ?>">"4 Days"</option>
    </select>   

<?php}?>

这是我的 js 代码 combo_checkout_iRange.js

$(document).ready(function() {
    $('#loader').hide();
    $('#check-in-date').change(function(){
        $('#check-out-date-wrap').fadeOut();
        $('#loader').show();
        $.post("combo_check-out.php", {
            parent_id: $('#check-in-date').val(),
        }, function(response){
            setTimeout("finishAjax('check-out-date-wrap', '"+escape(response)+"')", 400);
        });
        return false;
    });
});

//JQuery to hide Loader and return restults
function finishAjax(id, response){
  $('#loader').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
} 

function alert_id()
{
    if($('#check-out-date').val() == '')
    alert('Please select a sub category.');
    else
    alert($("#check-out-date").val());
    return false;
}

它们在 wordpress 之外也能正常工作

如何将它们集成到 wordpress 主题中

注意:这应该适用于名为“会议”的帖子类型 这就是我写在function.php

add_action("wp_enqueue_scripts", function() {

     if (is_single()) {
        if (get_post_type() == 'meetings')
        {
            wp_enqueue_script('combo_checkout_iRange', get_template_directory_uri() . '/js/combo_checkout_iRange.js', array( 'jquery' ), '1.0' ,true);

        }
    }
});

【问题讨论】:

  • 查看我对类似问题的回答...在 wordpress 中正确使用 ajax 需要比您拥有的更多步骤...stackoverflow.com/a/28966067/4572608
  • 谢谢@danbahrami 我理解你写的东西,但在我的情况下我无法实现它.. 你能解释一下在 ajax 函数中写什么.. 再次感谢你的帮助跨度>
  • 为什么不能实现呢?问题不在于您的 Ajax 功能,而在于您在一个拥有自己的规则集的平台上工作。如果您想在 Wordpress 上实现 Ajax,您只需按照该答案中的步骤操作即可。
  • 快速的解决方案是使用更明确的 PHP 页面路径。 @danbahrami 建议的解决方案将脚本路径本地化,这样您就不必明确定义您尝试加载的 PHP 文件的路径,但如果您完全限定 PHP 实际所在的主题目录的 PHP 路径,它应该可以工作。

标签: javascript php jquery ajax wordpress


【解决方案1】:

第一步

你需要点击的网址是:

<?php echo admin_url( 'admin-ajax.php' ); ?>

现在有几种方法可以将它放入 js 中。那是你的“ajaxurl”

第二步

使用 url,您需要传递如下操作:

url?action=your_ajax_hit

在 jquery 中,它会像:

$.ajax({
 url : ajaxurl+'?action=your_ajax_hit',
 type : 'post'
.....
.....
.....
});

第三步

进入functions.php你可以添加

add_action("wp_ajax_your_ajax_hit", "your_function_name");
add_action("wp_ajax_nopriv_your_ajax_hit", "your_function_name");

function your_function_name(){

   // your code 

  wp_die();
}

你可以看到它的

wp_ajax_your_ajax_hit

就是这样

请阅读:http://codex.wordpress.org/AJAX_in_Plugins

【讨论】:

  • 非常感谢您的帮助.. 但是我在这段代码中做了很多更新Here是我的最后一次更新,但我现在的问题是我的 ajax 请求返回“0”
  • 是的,我做到了,它现在可以工作了 :) 我的问题出在 Ajax 处理程序的位置 Thnak you :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多