0

I'm new to Wordpress so please be gentle.

I succesfully created a Admin submenu page. The submenu page itself is supposed to show a list of files that are in 'wp-admin/whatever/myFolder' with buttons attached and it does. Here is a button example:

<li>$file</li><input type='button' value='choose' data-filename='$file' data-path='$fullPath' name='choose' />

I $_POST the data to the file "myPost.php" on click, using AJAX with jQuery like the following in the same .php file that creates my submenu:

        $.ajax({
        type: "POST",
        url: '../wp-content/plugins/myPlugin/myPost.php',
        data: {
            fileName : $(this).data('filename'), 
            filePath  : $(this).data('path'), 
        },
        success:function(data) {
            console.log(data); 
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    }); 

The POST works flawlessly. The problem I'm facing is, that the "myPost.php" file can't execute further Wordpress functions like "add_action" or which ever, because the defined ('ABSPATH') at the beginning of "myPost.php" is always false.

Thanks for any help.

0

Follow these steps and you should be good to go:-

  1. Localize your script where you are implementing ajax(https://codex.wordpress.org/Function_Reference/wp_localize_script)
  2. Then make use of wp_ajax_{myfunction} (admin) and wp_ajax_nopriv_{myfunction}
  3. And lastly implement your code inside that function

    myfunction(){
        /****Your code goes here*****/
    }
    

    Eg. https://stackoverflow.com/questions/17855846/using-ajax-in-a-wordpress-plugin

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.