Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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?

like image 950
Florian Avatar asked Apr 10 '26 01:04

Florian


1 Answers

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.

like image 86
David Chase Avatar answered Apr 11 '26 15:04

David Chase



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!