Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法识别自定义wordpress php文件关系_Php_Jquery_Ajax_Wordpress_Include - Fatal编程技术网

无法识别自定义wordpress php文件关系

无法识别自定义wordpress php文件关系,php,jquery,ajax,wordpress,include,Php,Jquery,Ajax,Wordpress,Include,我试着做一些简单的事情: 在文件/inc/follow-event.php中,我调用了一个ajax post请求: jQuery.ajax({ type: "POST", data: { data:'asd'}, url: "/wp-content/themes/cust/lib/follow_cat_event.php" }); lib/follow_cat_event.php文件为: <?php $user = wp_get_current_user(

我试着做一些简单的事情: 在文件/inc/follow-event.php中,我调用了一个ajax post请求:

jQuery.ajax({
    type: "POST",
    data: { data:'asd'},
    url: "/wp-content/themes/cust/lib/follow_cat_event.php"
});
lib/follow_cat_event.php文件为:

<?php
    $user = wp_get_current_user();
    echo $user->ID;
?>
编辑更新:

遵循_cat_event.php:

<?php 
    add_action('wp_ajax_my_action', 'my_action_callback');
    function my_action_callback(){
        global $wpdb;
        $user = wp_get_current_user();
        echo $user->ID;
        if ( isset($_GET['data']) ) {
        }
    }
?>

仍然不走运…

即使在function.php中添加
/inc/follow event.php
,它也无法在其中使用wordpress函数。因为
/inc/follow event.php
本身不属于wordpress函数的范围。你可以创建一个插件,并尝试这种方式

另外,在使用Wordpress默认AJAX处理程序实现AJAX时,不要忘记添加操作

jQuery.ajax({ 
    type: "POST",
    action : "my_action"
    data: { data:'asd'}, 
    url: "<?php admin_url( 'admin-ajax.php' ) ?>" 
});
jQuery.ajax({
类型:“POST”,
行动:“我的行动”
数据:{data:'asd'},
网址:“
});
现在,添加一个插件并激活它



这将允许您在Wordpress前端发出AJAX请求。

如果您正在编写自定义AJAX函数,您应该以“正确”的方式使用AJAX。对“wordpress中的ajax”进行一些研究。在wordpress主题中调用这样的PHP文件并不意味着wordpress函数会自动加载到其中。@Meschiany不会向
/wp content/themes/geektime/lib/follow_cat_event.PHP'发送请求,你的
wp_ajax`操作不会以这种方式工作。添加_操作('wp_ajax_st_st24388029','st24388029')); 添加动作(“wp_ajax_nopriv_st_24388029”、“st_24388029”)@pmandell请检查它是否表明wp_ajax_中的操作(action)是您在ajax请求中作为参数发送的。函数名不需要与动作相同。正如我在回答中提到的,我正在尝试使用动作参数发送我的_动作。所以在任何情况下都不应该是wp_ajax_st_24388029。@pmandell第二个参数到
add_action('wp_ajax{action}','custom_function_name')
可以是您编写的任何内容。它不需要与操作名称相同。:)
function myFunction(){
    jQuery.post(
        "/wp-content/themes/geektime/lib/follow_cat_event.php",
        {
        action: "my_action",
        data: { data:'asd'},
        }
    );
}
jQuery.ajax({ 
    type: "POST",
    action : "my_action"
    data: { data:'asd'}, 
    url: "<?php admin_url( 'admin-ajax.php' ) ?>" 
});
<?php

/*
Plugin Name: Wordpress AJAX
Author: Abhineet Verma
Version: 1.0
*/

function st_24388029(){
    // Do your stuff here
}

add_action( 'wp_ajax_my_action', 'st_24388029' );
add_action( 'wp_ajax_nopriv_my_action', 'st_24388029' );
?>