douzhuozhu9544 2015-03-12 19:18
浏览 92
已采纳

如何将ajax集成到Wordpress主题?

I am trying to use ajax in wordpress

I have two files, php and js

this is my php code 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}?>

and here it is my js code 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;
}

they work fine outside wordpress

how to integrate them in wordpress theme

Note: this should work in post type called "meetings" so this is what i wrote on 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);

        }
    }
});
  • 写回答

1条回答 默认 最新

  • dongwaner1367 2015-03-20 20:26
    关注

    First step

    the url you need to hit that is :

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

    now there are couple of method to get this into the js . that is your "ajaxurl"

    Second Step

    with the url , you need to pass a action like :

    url?action=your_ajax_hit

    in jquery , it would be like :

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

    Third Step

    into the functions.php you can add

    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();
    }
    

    you can see its

    wp_ajax_ your_ajax_hit

    thats it

    please read this : http://codex.wordpress.org/AJAX_in_Plugins

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名