What you want to do is wrong in terms of the Wordpress architecture. It is not necessary to create files that run separately from Wordpress .
If you want to do something through an AJAX- request in Wordpress write a plugin. Register your handler for the wp_ajax_nopriv action in wp_ajax_nopriv . Inside this handler you will have access to the Wordpress API.
It will turn out something like this:
add_action('wp_ajax_nopriv_myfunc', 'my_functionality_handler'); function my_functionality_handler() { // Тут выполняете ваши задумки и возвращаете результат }
In order for the handler to be invoked, you need to send a GET or POST request to wp-admin/admin-ajax.php . For the considered example, the request must have an action variable with the value myfunc (your value may be different).
Additional Information:
Updated
By the way, you can really place the AJAX action handler in the functions.php file in your theme directory.