Ajax post request doesn’t work in wp theme directory

This is an example of ajax post request. It work when placed outside of wordpress active theme directory. However when it is there and norefresh.php is also uploaded there it doesn’t work no matter what exact patch to norefresh.php I use (site/themefolder/norefresh.php or server patch or local patch norefresh.php or /norefresh.php). Doesn’t work at all.

Is there anything about wordpress that prevents the execution.What should I do?

Read More
$.ajax({
type: "POST",
url: "norefresh.php",
data: reqdata,
cache: false,
success: function(html) {
//document.getElementById('myTextarea').value = html;
alert(html);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Related posts

1 comment

  1. You can allow access using htaccess to the specific file, but this will be a pain for your clients / if you move sites etc?

    Better just to hook the function either from your plugin or theme. Then you don’t need to worry about relative uris. (some minor differences in the js but other than that virtually the same)

    add_action( 'wp_ajax_custom_hook', 'custom_function' );
    add_action( 'wp_ajax_nopriv_custom_hook', 'custom_function' ); //-->front end hook...
    
    function custom_function(){
    
        // handle code...
    }
    
    add_action('wp_head','define_ajaxurl'); //--> define ajaxurl using php so we can neatly place all js in js file
    
    function define_ajaxurl() {
        ?>
        <script type="text/javascript">
        var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
        </script>
        <?php
    }
    

    JS

    $.ajax({
        type: "POST",
        url: ajaxurl, //-->see php function hooked to wphead to define this!!
        //data: reqdata, //--> need also to pass the action....which is the hook!! 
        data: {
            action: 'custom_hook', //-->name of hook used as above..ie. wp_ajax_nameofhook...
            'otherfield': 'yea'
        }
    
        cache: false,
        success: function(html) {
        //document.getElementById('myTextarea').value = html;
        alert(html);
        }
    });
    

Comments are closed.