douzhuozhu9544 2015-03-12 19:18 采纳率: 0%
浏览 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

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

报告相同问题?

悬赏问题

  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 RT-Thread Studio编译问题