WordPress Plugin template_redirect throws 404

I’m trying to load a specific file of a plugin via a URL in WordPress using template_redirect. The file gets shown correctly, however the header sent is 404, not 200.

The code in the plugin is:

Read More
add_filter('query_vars','plugin_add_jb');
function plugin_add_jb($vars) {
    $vars[] = 'jb_ajax';
    return $vars;
}

add_action('template_redirect', 'plugin_jb_check');
function plugin_jb_check() {
    if(intval(get_query_var('jb_ajax')) == '1') {
        $jb_action = $_GET['action'];
        if($jb_action == 'refer') {
            include('./wp-content/plugins/JobBounties/ajax_refer.php');
        }
        elseif ($jb_action == 'refer_post') {
            include('./wp-content/plugins/JobBounties/ajax_refer_post.php');
        }
        exit;
    }
}

Anyone have any idea why it’s throwing 404?

Related posts

Leave a Reply

1 comment

  1. WordPress may not recognize this plugin or data and throws a 404 status.

    To combat the 404 status simply test for it, remove the flag and then send header 200 status with the include.

    function wp15905643_include($template) {
        global $wp_query;
        if ($wp_query->is_404) {
            $wp_query->is_404 = false;
        }
        header("HTTP/1.1 200 OK");
        include($template);
    
    }
    

    then use it in your plugin_jb_check function like so and if that helps.

    wp15905643_include('./wp-content/plugins/JobBounties/ajax_refer.php');

    sources here.