admin管理员组文章数量:1289667
I'm a software engineer with a WordPress site with hundreds of pages, and it's working very well.
There are certain cases, however, where I'd love to be able to code my own page with no interaction with / interference from the WordPress system (no themes, no styles, no javascript, etc) yet still have it located on that same subdomain.
How can I set to display an HTML file uploaded somewhere within
/wp-content/
?
And as a bonus, I'd love if it would also work with a PHP file (which would generate the HTML).
I'd like to do this without manually editing the .htaccess
file.
Here is an example that comes close:
The LeadPages WordPress plugin seems to do a variation of what I'm hoping for. It allows a user to specify a "slug" (relative path, such as special-page
) and what content to display there. But the content needs to be hosted on LeadPages to use this plugin, obviously.
What I'm hoping to do is host my own HTML file somewhere on my server (e.g. in a publicly-accessible folder such as /wp-content/
) and then have a certain relative path point to it.
Look at this .htaccess
file that I manually edited (and which works how I want):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks
DirectoryIndex /wp-content/raw/book-sale.html [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^thanks$ /wp-content/raw/book-opt-in-thank-you.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I would have loved to have been able to specify from within the WordPress admin that the site root (/
) should show the contents of /wp-content/raw/book-sale.html
, and /thanks
should show /wp-content/raw/book-opt-in-thank-you.html
(so that I wouldn't need to go in an edit the .htaccess
file directly.
I'm a software engineer with a WordPress site with hundreds of pages, and it's working very well.
There are certain cases, however, where I'd love to be able to code my own page with no interaction with / interference from the WordPress system (no themes, no styles, no javascript, etc) yet still have it located on that same subdomain.
How can I set https://example/special-page
to display an HTML file uploaded somewhere within /wp-content/
?
And as a bonus, I'd love if it would also work with a PHP file (which would generate the HTML).
I'd like to do this without manually editing the .htaccess
file.
Here is an example that comes close:
The LeadPages WordPress plugin seems to do a variation of what I'm hoping for. It allows a user to specify a "slug" (relative path, such as special-page
) and what content to display there. But the content needs to be hosted on LeadPages to use this plugin, obviously.
What I'm hoping to do is host my own HTML file somewhere on my server (e.g. in a publicly-accessible folder such as /wp-content/
) and then have a certain relative path point to it.
Look at this .htaccess
file that I manually edited (and which works how I want):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks
DirectoryIndex /wp-content/raw/book-sale.html [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^thanks$ /wp-content/raw/book-opt-in-thank-you.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I would have loved to have been able to specify from within the WordPress admin that the site root (/
) should show the contents of /wp-content/raw/book-sale.html
, and /thanks
should show /wp-content/raw/book-opt-in-thank-you.html
(so that I wouldn't need to go in an edit the .htaccess
file directly.
- It's not too tricky to do. Are you using your own theme? I'm trying to sleep at the moment but if no-one jumps in I'll try for an answer tomorrow. – Andy Macaulay-Brook Commented Jun 8, 2016 at 23:36
- I could use a child theme to sort of "make" a theme into "mine" if that will allow me to do what you recommend. Thanks! – Ryan Commented Jun 9, 2016 at 0:08
- It would allow an approach where you create a Page with the URL you want, add your HTML file to the Page as an attachment and set a special page template that uses get_template_part() to pull in the attached file. – Andy Macaulay-Brook Commented Jun 9, 2016 at 0:13
- Thanks. Perhaps you're talking about this? wordpress.stackexchange/a/128869/51462 – Ryan Commented Jun 9, 2016 at 0:35
- 1 That would need a template for each page. Where the HTML is in that answer, put in code to get the URL of the attached HTML file and then include it. However note the possible security risk of including files that an editor level user can upload. You needn't use a special template, just put a check for an HTML attachment in the theme's page.php. I do similar in front-page.php on some development sites to show a holding page to visitors who aren't logged in. – Andy Macaulay-Brook Commented Jun 9, 2016 at 0:40
3 Answers
Reset to default 1If you're willing to create a child theme and put your PHP files in there, this approach is relatively clean. For each file, create a rewrite rule where you specify the path you want to use and the filename that should load. In the example below, /custom-path/
loads custom-file.php
from the root of your child theme directory and /custom-path-2/
loads custom-file-2.php
. Don't forget to flush permalinks after adding rewrites.
/*
* Set up a rewrite for each path which should load a file.
*/
function my_standalone_rewrites() {
add_rewrite_rule( '^custom-path/?', 'index.php?standalone=custom-file', 'top' );
add_rewrite_rule( '^custom-path-2/?', 'index.php?standalone=custom-file-2', 'top' );
}
add_action( 'init', 'my_standalone_rewrites' );
/*
* Make `standalone` available as a query var.
*/
function my_query_vars( $vars ) {
$vars[] = 'standalone';
return $vars;
}
add_filter( 'query_vars', 'my_query_vars' );
/*
* If `standalone` is set when parsing the main query, load the standalone file.
*/
function my_standalone_path( &$wp_query ) {
if ( $wp_query->is_main_query() && get_query_var( 'standalone', false ) ) {
// Load filename.php from your child theme root.
get_template_part( get_query_var( 'standalone' ) );
exit;
}
}
add_action( 'parse_query', 'my_standalone_path' );
You could create a page with the slug you want, and add a custom field that specifies the HTML path to output (in this example using either htmlfilepath
or htmlurl
as the metakey):
add_action('wp','maybe_direct_html_output');
function maybe_direct_html_output() {
if (is_singular()) {
global $post;
$htmlfilepath = get_post_meta($post->ID,'htmlfilepath',true);
if ( ($htmlfilepath) && (file_exists($htmlfilepath)) ) {
echo file_get_contents($htmlfilepath); exit;
}
$htmlurl = get_post_meta($post->ID,'htmlurl',true);
if ($htmlurl) {
$html = wp_remote_get($html);
if (!is_wp_error($html)) {echo $html['body']; exit;}
}
}
}
So no need for .htaccess
when doing it this way... I just noticed you want to use wp-content
so you could just use a relative path and simplify this to (using htmlfile
metakey):
add_action('wp','maybe_direct_html_output');
function maybe_direct_html_output() {
if (is_singular()) {
global $post;
$htmlrelpath = get_post_meta($post->ID,'htmlfile',true);
if (!$htmlrelpath) {return;}
$htmlfilepath = WP_CONTENT_DIR.$htmlrelpath;
if (!file_exists($htmlfilepath)) {return;}
echo file_get_contents($htmlrelpath); exit;
}
}
Remembering that WP_CONTENT_DIR
has no trailing slash so you would enter a relative path custom field value for the page such as /raw/book-sale.html
For anyone with similar problem, this free plugin has been around for a long time and does partly what is asked here (it doesn't work with PHP files). It lets you create custom HTML pages that bypass WordPress's templating system and they are called by custom URIs. https://wordpress/plugins/wp-custom-html-pages/
- Look for WP Custom HTML Pages plugin on the repository, install and activate
- Under Pages menu in admin panel look for HTML Pages menu item
- Create any number of pages and assign them custom URI. The pages are made by adding code into Ace editor.
本文标签: domainHow to display a raw HTML page (bypassing WordPress themescriptsetc)
版权声明:本文标题:domain - How to display a raw HTML page (bypassing WordPress theme, scripts, etc) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741414123a2377409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论