0

Why does it seem that WP requires me to utilize the entire and true file path instead of being able to use a relative file path? I know for safety its better not to use a true file path. In one of my functions, I am specifying a .php file located. Seems no matter what I do, I can not get my function to point to find the file.

The full directory that the php file is in domain_name/public_html/wp-content/mythemename/assets/php/select-change.php

A portion of the code that needs to locate my php file:

jQuery.post("assets/php/select-change.php"); When using the above I get a 404 error and it tells me it can not find the file using - domain_name.com/page_name/php/select-change.php . It seems insistent upon putting the page name in there.

Even when I place the select-change.php file directly into the same directory that contains the calling .js file (assets/js), then it still cant find it since it is looking for mydomain_name.com/page_name/select-change.php which is not the path. Solution?

(ive also tried things like):

`jQuery.post("select-change.php");
jQuery.post("../select-change.php");
jQuery.post("/select-change.php");
jQuery.post("php/select-change.php");
jQuery.post("../../select-change.php");`

The PHP file:

function change_post() {
if($_POST && $_POST['change']){
$wpdb->get_results($wpdb->prepare('UPDATE Staff_Connector_Table SET Staff_List_Staff_Member_Id =  "'.$_POST['change'].'" Table_id ='.$tbl_id));
}
}
9
  • What happens in select-change.php - what do you need to post to it?
    – Q Studio
    Commented Jan 3, 2021 at 17:33
  • It is a db update that comes off the button click.
    – Kris
    Commented Jan 3, 2021 at 17:56
  • Can you share it, so we can understand it better?
    – Q Studio
    Commented Jan 3, 2021 at 18:00
  • 2
    You should not be making direct requests to PHP files in your theme, it is extreme bad practice, insecure, and has lots of issues ( such as the problem you're having ). If you want to do things with AJAX, add a REST API endpoint instead. Look into developer.wordpress.org/reference/functions/register_rest_route you'll get a pretty URL to make requests against
    – Tom J Nowell
    Commented Jan 3, 2021 at 18:20
  • 1
    Also, relative URLs are relative to the current URL in the browser, not the location of the file the code runs in
    – Tom J Nowell
    Commented Jan 3, 2021 at 18:22

1 Answer 1

1

In PHP, it's not necessary to post to a PHP file, it is only required to include ( or require ) files and then use checks and validations to ensure that code is only processed when required.

You simply need to include the PHP file as part of your application - it will process the PHP is the conditions are met.

4
  • Thanks how would this solve the issue of specifying the path? If I am not mistaken you need to specifiy the file path in the require or require_once, yes?
    – Kris
    Commented Jan 3, 2021 at 18:44
  • require_once( get_template_directory() . '/assets/php/select-change.php' );
    – Q Studio
    Commented Jan 3, 2021 at 21:31
  • Isnt that a security risk of showing the absolute path out to that point?
    – Kris
    Commented Jan 3, 2021 at 21:35
  • requiring a file does not expose anything, as this is all server-side - PHP tries to find the file and run the code it finds inside - require_once will only allow that file to be included once, no matter how many times it is referenced in the codebase.
    – Q Studio
    Commented Jan 3, 2021 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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