0

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

I can see it as examplepage.com/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.

  • This isn’t really how WordPress is supposed to work. Why is the root folder not an option? – Jacob Peattie Jun 12 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 Jun 12 at 17:27
  • In this situation the security part is not important. Would like to know how/if it could be done. – Zae Jun 12 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 Jun 12 at 17:50
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.com/somepost/.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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