0

http://my_domain/path_to_my_script/my_php_script.php

When I try to run my_php_script.php directly over the Internet giving the path above in my browser, I am being redirected to the home page of Wordpress.

Why that situation happens?

I've commented all the lines in the .htaccess file which can be found in the root Wordpress in order to see if the problem is in that file, but the situation gets worse - I got 500 Internal Server Error when I try to access that php file over the Internet.

I am using that script in ajax requests.

Could you plese give me some directions on how I could solve that problem.

0

To use an external script with ajax, you must register an action to trigger it. To do it, you need to use wp_ajax_YourAction and wp_ajax_nopriv_YourAction

add_action('wp_ajax_YourAction', 'myAjaxFunction');
// just a way to quickly print your js
add_action('wp_head', 'print_js');

function print_js(){
    ?><script type="text/javascript" >
            jQuery(document).ready(function($) {
                var data = {
                        action: 'YourAction',
                        fun: '42',

                    };
                $.post(ajaxurl, data, function(response) {
                        console.log(response);
                        var json = $.parseJSON(response);

                        alert('the fun number is'+response.fun);

                });
            });
    </script><?php
}
function myAjaxFunction(){
    if(isset($_POST['fun'])){
        $action = $_POST['fun'];
    }

    switch ($action) {

        case 'fun':
            echo json_encode(array('fun'=>$_POST['fun']+1))
            exit();
        break;

        default:
        break;
}

Depending on how your want to trigger your ajax call and the datas you want to post, this is just a simple example.

  • Thank you for the reply! Your solution is a standard one for WordPress ajax but my idea is a little bit different. – George Oct 18 '16 at 13:04
  • Maybe you can get your solution with a call made like an api call with wp_remote_get, wp_remote_post, process the data and work with the response with wp_remote_retrieve_body. But it's not an Ajax way – Benoti Oct 18 '16 at 13:11
  • My idea is the following. I am trying to load a php script using an ajax request, not to process some data passed as get() or post() request in a php script but to load the php script using JS. Because I am restricted to use JS, my only option is to use ajax. In a standard ajax application, which doesn't use any framework like WP the solution is like this $('.some_div_container').load('script.php',...), or .get(), or .post() Is there a way in WP so that I can load a php script in that way? – George Oct 18 '16 at 13:12
  • sorry, unconsciously I pressed the ENTER key! – George Oct 18 '16 at 13:13
  • Can you edit your answer with some code. 500 errors can be easy to find with some of your code. – Benoti Oct 18 '16 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.