字preSS阿贾克斯返回从functions.php的全功能HTML网页,而不是echo全功能、而不是、网页、functions

由网友(心蛊)分享简介:我使用的一个词preSS Ajax调用从函数返回的字preSS主题的functions.php简单内容。但是,将返回一个完整的HTML页面来代替。I am using a wordpress ajax call to return simple content from a function in wordpress...

我使用的一个词preSS Ajax调用从函数返回的字preSS主题的functions.php简单内容。但是,将返回一个完整的HTML页面来代替。

I am using a wordpress ajax call to return simple content from a function in wordpress theme functions.php. However, a full html page is returned instead.

下面是Ajax调用

<?php   
$ajax_nonce = wp_create_nonce("iwhq_beginner_select_course");
?>

<script type="text/javascript" language="javascript">
jQuery(document).ready(function(){

jQuery("#beg_golf_course").change(function() {  //do this when course changes

//in Wordpress ajaxurl always points to admin-ajax.php
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
var course_id = 4; 

//Do the ajax
    jQuery.ajax({
        type: "POST", 
        url: ajaxurl,

        //NOTE - the action parameter calls the function in functions.php
        data: { action: 'select_course_aj', course_id: course_id, _ajax_nonce: '<?php echo $ajax_nonce; ?>' },


        //display alert on success
        success: function(html){ 
            alert(html);
        } 
    }); //close jQuery.ajax(
    return false;

    });
});
</script>

这是在functions.php的功能

And this is the function in functions.php

function select_course_func(){
echo $_POST["course_id"];
die();
}

add_action('wp_ajax_select_course_aj','select_course_func');

包含了jQuery AJAX调用页面的HTML实际显示在警报,而不是回声。

The HTML of the page containing the jquery ajax call is actually displayed in the alert instead of the echo.

任何天才在那里能告诉我为什么吗?

Any geniuses out there able to tell me why?

谢谢 马克

推荐答案

好了,问题解决了。见我最后3以上的评论加...

OK, Problem solved. See my last 3 comments above plus...

!定义('DOING_AJAX')是可用于测试用户没有正在执行Ajax请求一个常数。我结合这与我的逻辑重定向非管理员的前端和它的作品了。

!defined('DOING_AJAX') is a constant that can be used to test that the user is not performing an ajax request. I combined this with my logic for redirecting non-admins to the frontend and it works now.

/* check the role of current loged in user for redirection */
add_action('admin_init','rt_checkRole');
function rt_checkRole() {

    global $wp_roles;
    $currentrole ='';
    foreach ( $wp_roles->role_names as $role => $name ) {
        if ( current_user_can( $role ) ){
                    $currentrole = $role;
                }
        }
        if(!defined('DOING_AJAX') && (!$currentrole || ($currentrole != 'administrator' && $currentrole != 'editor'))){
            wp_redirect (site_url().'/front-end-login/');
        }
}

发现了!定义(DOING_AJAX')的http://word$p$pss.stackexchange.com/questions/26100/redirect-out-of-wp-admin-without-losing-admin-ajax-php

感谢所有谁评论。

阅读全文

相关推荐

最新文章