rkaartikeyan
rkaartikeyan

Reputation: 2007

How to execute a custom php file with a wordpress website

I have installed wordpress blog in my domain(Ex: http://mywordpresswebsite.com). Now i have a php file called serve.php in the root like http://mywordpresswebsite.com/serve.php.

Now if i open the url in browser its saying page not found.

then i have added RewriteRule ^serve.php$ serve.php [L] in htaccess code, still its saying page not found.

Please give me a solution for this as soon as possible, thanks in advance.

Upvotes: 3

Views: 16272

Answers (2)

J W
J W

Reputation: 386

Actually, if it is just plain php code that you want to run, you can simply upload the file to some accessible location on your website and specify the web path to run it.

For example, upload serve.php to your theme directory and visit:

http://mywordpresswebsite.com/wp-content/themes/<theme_name_here>/serve.php

Of course, you need to replace <theme_name_here> with the name of the theme folder.

However, if your code in serve.php is not just plain PHP, but needs to interact with Wordpress objects, the execution will produce an error. PHP does not know anything about Wordpress. To see the error, you may want to temporary enable debugging output for your site by finding the .htaccess file in its root directory and appending to it :

 php_flag display_errors on

So, in case you do need to interact with your Wordpress site, you need the hack, described by Danijel

Upvotes: 4

Danijel
Danijel

Reputation: 12709

Wordpress templates are located in wp-content/themes/yourthemename/. Easy way to load a custom php file is to put serve.php in that folder and to make that file a page template by putting comment below at the beginning of the file:

<?php
/*
Template Name: Serve
*/

Now go to admin->pages, create a new page and assign that page template from the template dropdown. Open the http://mywordpresswebsite.com/serve/ url in browser ( click on Show Page in admin bar ) and serve.php will be loaded.

Upvotes: 9

Related Questions