最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

multisite - Custom php page outside the scope of my theme

programmeradmin4浏览0评论

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 .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!

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/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!

Share Improve this question asked Aug 8, 2020 at 16:39 SimonSimon 1112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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/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

发布评论

评论列表(0)

  1. 暂无评论