1

Scenario

I am working on a domain's website which is part of a multi-site installation.

I am looking to create a custom php page which will do some processing. This page would be accessed from the main WP site using a Javascript XMLHttpRequest

I do NOT want this page to have any of my theme's headers, content, etc. as this page will not actually be visited directly by the user's browser.

Example php (simplified);

<?php session_start() $_SESSION["stuff"] = "things"; echo "success"; exit(); ?>

Things I've tried

I tried using hooks and headers for the PHP within ‘elements’ however it appears that all of the available options only process the php after some headers have started being generated, therefore meaning that the php would not present data as accurately as expected.

It seemed obvious to me to simply create the '.php' file in the same way I would if weren't using WP, however I have struggled to find the directory where I should create such a file. I checked the configuration of the file: /etc/apache2/sites-enabled/000-default.conf but this just shows me 'DocumentRoot /var/www/wordpress' which is a the root directory of the multi-site area. This lead me to believe that it may be a little more complex than simply creating the file in a certain directory directly on the server.

I have also tried to use the “disable elements” for this page when in 'edit page' however this doesn’t seem to do enough either.

I also tried reviewing this previous thread but I can't see the solution, I ideally don't want the WP functionality at all for this single page. I just want to be able to access it from https://example.com/string.php (or similar).

Desired Outcome

I want the page to be as plain as possible OR to be able to execute the php early enough that the exit(); stops the actual page from being rendered into HTML (headers, etc.)

Also, I ideally don't want to just 'throw another plugin at it' if there's an alternative (but not ridiculous) way.

Please could you help me to find a solution?

Many Thanks!

2 Answers 2

1

is this not something that you could put into your functions.php file as a new function, adding the action to init to fire off on the page load? You could then put in an if statement so it only runs when specific criteria is met, such as a page ID or slug or template is loaded?

If this does need to be a blank page, then you could just create a file in the theme root called something like page-processor.php where you could just put in your required data. This page will load as blank so long as you don't include get_header() or get_footer(). Once you've created the file and given the template a name (Take a look here for some page template info if you're unfamiliar) then you can set that blank template as the page template in the attributes tab.

1

A couple of ways to achieve this:

1 - Make a new REST API endpoint

Wordpress provides some nice functions which makes ajax-style callable URL's that can encapsulate small pieces of functionality like you want trivial.

function myapi_dosomething() {
    // Do whatever you want here, set cookies, db operations, etc.
    // Use the Wordpress functionality wherever possible
    return "hello";
}

add_action( 'rest_api_init', function () {
  register_rest_route( 'myrestapi/v1', '/dosomething', array(
    'methods' => 'GET',
    'callback' => 'myapi_dosomething',
  ) );
} );

This will now provide the output 'hello' neatly wrapped in JSON, at the URL www.example.com/wp-json/myrestapi/v1/dosomething

2 - Host the file in a plugin

You could use the plugin infrastructure to host the PHP file inside the wp-content/plugins directory. This is a good way to achieve this because it makes your code neatly encapsulated in the plugin infrastructure which means it's easy to install in any Wordpress situation and Wordpress system and theme upgrades won't affect it.

It takes very little coding to do this, you just need a simple fake plugin file with the correct headers.

3 - Host the file outside the Wordpress webspace

If you moved your Wordpress files to a subdirectory in the root of your hosting, you could then stick any other PHP files or anything else in the root of your hosting (or e..g make a new directory for other applications or PHP files you're hosting). If your site is already running this is not ideal, and takes a bit of messing around with .htaccess files in order to make your Wordpress appear to be running in the root. This is mainly useful if you don't have much control over the server and need to host many separate PHP applications in the same webspace.

As you've maybe identified, putting a .php file inside the Wordpress filespace won't work, or can get Wordpress confused, and is not very compatible with

2
  • Hi @Moz, thanks for your thoughts. after a little research on your points, I'm thinking #3 probably would turn out to be the most complicated, especially seeing as the server already has another site live within this multi-site installation. and #2 seems less complicated compared to #1. Would you agree with this in a "general" sense? Also, are there any security differences/implications between these two options that I should consider? Thanks again!
    – Simon
    Aug 10, 2020 at 16:04
  • Ah yes, I should have added it but they were in order of ease/functionality. Definitely #3 is most annoying and difficult, esp. if #1 or #2 works for you. #2 is a tiny bit of a hack, you generally should not make raw PHP files from inside Wordpress available to outside, some small possibility it would introduce security holes. #1 is clean and if your functionality does have connection to Wordpress it makes sense to keep it inside the WP infrastructure, and means you don't have to worry about any special security considerations.
    – mozboz
    Aug 10, 2020 at 17:33

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.