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

templates - Adding regular php file to site

programmeradmin1浏览0评论

How do I create a page(in wordpress theme folder) so that it can be accessed on my site as examplepage/somepost.php?

I can see it as examplepage/wp-content/themes/mytheme/somepost.php but is there any way to cut off the path? Placing the file in the root folder is not an option.

Thanks in advance.

How do I create a page(in wordpress theme folder) so that it can be accessed on my site as examplepage/somepost.php?

I can see it as examplepage/wp-content/themes/mytheme/somepost.php but is there any way to cut off the path? Placing the file in the root folder is not an option.

Thanks in advance.

Share Improve this question edited Jun 12, 2019 at 17:26 Tom J Nowell 61.2k7 gold badges79 silver badges150 bronze badges asked Jun 12, 2019 at 17:03 ZaeZae 31 bronze badge 4
  • This isn’t really how WordPress is supposed to work. Why is the root folder not an option? – Jacob Peattie Commented Jun 12, 2019 at 17:18
  • 1 You would want to route the request through WordPress, having standalone PHP files that are opened directly in a browser has security and maintenance issues, as well as losing a lot of benefits. What does this PHP file do? – Tom J Nowell Commented Jun 12, 2019 at 17:27
  • In this situation the security part is not important. Would like to know how/if it could be done. – Zae Commented Jun 12, 2019 at 17:34
  • @Zae I disagree, but, there are several methods of integrating code like that into WP, and not all of them are appropriate for all uses. The use case is important to know, else you might get an innapropriate answer. E.g. is this a template? A form handler? AJAX endpoint? etc You may not think it important, but it's super critical info to know, otherwise you're either going to get an extremely long winded generic answer that most people won't be bothered typing, that heavily hedges its bets, or, an answer that only applies to certain conditions – Tom J Nowell Commented Jun 12, 2019 at 17:50
Add a comment  | 

2 Answers 2

Reset to default 1

You could accomplish this by setting up a redirect. If you're using an Apache server, for your example, you would add this to .htaccess above the WordPress block:

RewriteEngine On
RewriteRule somepost.php ^/somepost/

It's much more common, and recommended, to create a PHP file and a Page (or other post type) within wp-admin). This will still run all of your PHP code, and has the added benefit of automatically including all the WordPress functionality as opposed to an orphan PHP file. In somepost.php in your theme folder:

<?php
/* Template Name: Show Some Post
*/
// all your PHP here
?>

then within wp-admin, actually create a Page (or another post type that supports page-attributes) and select this particular Page Template. You will then have a URL such as http://examplepage/somepost/.

As WebElaine said, it's common practice to create a Page Template in your theme and then create a page in your WordPress dashboard and select the template you just created.

You can also create a PHP file in your theme and name it page-some-post.php and then create a page in your dashboard named Some Post.

发布评论

评论列表(0)

  1. 暂无评论