如何在WordPress中显示数据?

时间:2017-04-25 07:00:15

标签: php ajax wordpress

我在WP中进行了ajax调用

这是代码

/*COMPARE AJAX*/
if(!function_exists('re_add_compare')){ 
#compare toggling
function re_add_compare() {
    $post_ids_arr = array();    
    $multicats_on = rehub_option('compare_multicats_toggle');
    $multicats_array = rehub_get_compare_multicats();
    $out = '';
    $compareid = (int)$_POST['compareID'];

    $perform = $_POST['perform'];

    #user identity
    $ip = rehub_get_ip();
    $userid = get_current_user_id();
    $userid = empty($userid) ? $ip : $userid;

    if($multicats_on =='1' && !empty($multicats_array)){
        foreach($multicats_array as $multicat){
            $cat_ids = $multicat[0];
            $cat_ids_arr = explode(',', $cat_ids);

            if(isset($multicat[3])){
                $term_slug = $multicat[3];
            } 
            else{
                $term_slug = 'category';
            }
            #check if post belongs to listed terms / categories
            $post_terms = wp_get_post_terms($compareid, $term_slug, array("fields" => "ids"));
            $post_in_cat = array_intersect($post_terms, $cat_ids_arr);

            if(array_filter($post_in_cat)) {
                $page_id = (int)$multicat[2];
                #existing posts
                //$post_ids = get_transient('re_compare_'. $page_id .'_' . $userid);

                $post_ids = $_COOKIE['re_compare'. $page_id];

                echo "<script>console.log( 'post_ids " . $post_ids . "' );</script>";
                switch($perform) {
                    case 'add':

                        if(isset($_COOKIE['re_compare'. $page_id])){
                            $post_ids_arr[] = $compareid;
                            //write_log( "$post_ids " . $post_ids_arr);
                            echo "<script>console.log( 'post_ids " . $post_ids_arr . "' );</script>";
                            //set_transient('re_compare_'. $page_id .'_' . $userid, $compareid, 30 * DAY_IN_SECONDS);
                            setcookie('re_compare_'. $page_id, $compareid, 3 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN);
                        }
                        else{
                            $post_ids_arr = explode(',', $post_ids);
                            if(($key = array_search($compareid, $post_ids_arr)) === false){
                                //$post_ids_arr[] = $compareid;
                                array_push($post_ids_arr, $compareid);
                                $newvalue = implode(',', $post_ids_arr);
                                //set_transient('re_compare_'. $page_id .'_' . $userid, $newvalue, 30 * DAY_IN_SECONDS);
                                setcookie('re_compare_'.$page_id,$newvalue,3 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN);
                            }
                        }

                        echo "<script>console.log( 'post_ids " . $post_ids . "' );</script>";
                    break;
                    case 'remove':
                        $post_ids_arr = explode(',', $post_ids);
                        if(($key = array_search($compareid, $post_ids_arr)) !== false){
                            unset($post_ids_arr[$key]);
                        }
                        $newvalue = implode(',', $post_ids_arr);
                        if(empty($newvalue)){
                            //delete_transient('re_compare_'. $page_id .'_' . $userid);
                            setcookie('re_compare_'. $page_id, '',time() - (15 * 60));
                        } 
                        else{
                            //set_transient('re_compare_'. $page_id .'_' . $userid, $newvalue, 30 * DAY_IN_SECONDS);
                            setcookie('re_compare_'. $page_id, $newvalue, 3 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
                        }
                    break;  

                    case 'removeall':           
                        unset($post_ids_arr);           
                        $newvalue = '';
                        //delete_transient('re_compare_'. $page_id .'_' . $userid);
                        setcookie('re_compare_'.$page_id,'', time() - ( 15 * 60 ) );
                    break;  
                }
                #html output
                $out = re_compare_item_in_panel($compareid);
                $count = count($post_ids_arr);
                $comparing_string = implode(',', $post_ids_arr);
            }
        }
    }else {
        //$post_ids = get_transient('re_compare' . $userid);
        $post_ids = $_COOKIE['re_compare'];
        switch($perform) {
            case 'add':
                if(empty($post_ids)) {
                    $post_ids_arr[] = $compareid;
                    echo "<script>console.log( '$post_ids_arr " . $post_ids_arr . "' );</script>";
                    //set_transient('re_compare_' . $userid, $compareid, 30 * DAY_IN_SECONDS);
                    setcookie('re_compare', $compareid,3 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
                } 
                else{
                    $post_ids_arr = explode(',',$post_ids);
                    if(($key = array_search($compareid, $post_ids_arr)) === false){
                        $post_ids_arr[] = $compareid;
                        $newvalue = implode(',', $post_ids_arr);
                        echo "<script>console.log( 'newvalue " . $newvalue . "' );</script>";
                        //set_transient('re_compare_' . $userid, $newvalue, 30 * DAY_IN_SECONDS);
                        setcookie('re_compare', $newvalue, 3 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN);
                    }
                }
            break;
            case 'remove':
                $post_ids_arr = explode(',', $post_ids);
                echo "<script>console.log( 'Remove post_ids_arr " . $post_ids_arr . "' );</script>";
                if(($key = array_search($compareid, $post_ids_arr)) !== false){
                    unset($post_ids_arr[$key]);
                }
                $newvalue = implode(',',$post_ids_arr);
                if(empty($newvalue)){
                    //delete_transient('re_compare_' . $userid);
                    setcookie('re_compare', '', time() - ( 15 * 60 ));
                }
                else{
                    //set_transient('re_compare_' . $userid, $newvalue, 30 * DAY_IN_SECONDS);
                    setcookie('re_compare', $newvalue, 3 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAINs);
                }
            break;  

            case 'removeall':

                unset($post_ids_arr[$key]);
                //delete_transient('re_compare_' . $userid);
                setcookie( 're_compare', '', time() - ( 15 * 60 ) );

            break;  
        }
        #html output
        $out = re_compare_item_in_panel($compareid);
        $count = count($post_ids_arr);
        $comparing_string = implode(',', $post_ids_arr);
        $page_id = rehub_option('compare_page');
    }

    #generate the response
    $response = json_encode( array( 'content' => $out, 'comparing' => $comparing_string, 'count' => $count, 'pageid' => $page_id ) );

    #response output
    header( "Content-Type: application/json" );
    echo $response;
    exit;
}
}
add_action('wp_ajax_re_add_compare', 're_add_compare');
add_action('wp_ajax_nopriv_re_add_compare', 're_add_compare');

我想在wordPress中显示变量值,如上所述

echo "<script>console.log( 'Remove post_ids_arr " . $post_ids_arr . "' );</script>"; 

但它没有显示任何内容。我也使用了write_log( "$post_ids " . $post_ids_arr);但没有任何用处。

wp-config.php档案

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_DEBUG_LOG', true );

除了ajax调用之外,还要查看变量值以了解我是否正确定制。如何实现这一点。

0 个答案:

没有答案