How to execute a custom php file with a wordpress website - php

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.

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.

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

Related

Index.php instead Index.html, how to access, and where is the .htaccess file that i have apparently have to change?

so i downloaded a website template, unzipped it, and uploaded it on my host on bluehost, just to test it out. I tested several, but one of them didn't work, and it seems it's because that one has a Index.php instead of a Index.html file. I did some research, and stumbled upon things like redirecting the site to the php file instead of the html and such. Also, something with .htaccess...i have a lot of questions regarding this, but for now i just want to know how to treat this index.php like if it was an index.html.
I searched for .htaccess to change the parameter to make it load .php instead of .html
<?php
/**
* The template for displaying the header
*
* Displays all of the head element and everything up until the page header div.
*
* #package Neve
* #since 1.0.0
*/
$header_classes = apply_filters( 'nv_header_classes', 'header' );
?><!DOCTYPE html>
<html <?php language_attributes(); ?>
I just guess that i need to change the above header or some alternative of it. Please help. Thank you!
Btw: my website: https://sumproblems.com/ (i hope it is allowed to link this here. if not, sorry!)
The thing is that you are trying to view a WordPress theme, without the installation of WordPress.
You need first to download and install WordPress on your hosting and then upload this theme via WordPress admin (very easy). Good luck with that and enjoy WordPress
As long as the index.php is the only file named index in the directory, the webserver will show it.
Not sure if you have tried this but, remove everything you've uploaded (in this case, the templates) from the main website directory (where you've placed the templates) and just upload the one with the index.php file. Usually, there's no need to change anything on the .htaccess file, as the web server will recognize that he needs to show that file in the first place.
About the location of the .htaccess file: It should be on the main directory.
If this does not work, post a link to Pastebin or any other service of your choice with the contents of the .htaccess file so we can take a look.

How to remove "wp-content/uploads/2019/10" from media file URL?

For a current project, I am supposed to remove "/uploads/2019/10" from specific media file URLs. For instance, I need this, "https://www.mywebsite.com/wp-content/uploads/2019/10/myAwesomedoc.pdf", to be "https://www.mywebsite.com/myAwesomedoc.pdf". How can I accomplish this?
BACKGROUND: This needs to be done only for PDFs on the site. So far, I've tried redirects, through plugins (Pretty Link) and via .htaccess file per the direction of a peer but this didn't work because redirects don't change the actual "home" URL.
I've read elsewhere that in wp-config.php that I can insert code like define( 'UPLOADS', 'files' ); but this would affect all files, not to mention I would need to relocate all PDFs to this new folder called "files".
Lastly, I'm aware that under Settings>Media , I can disable the uploads folder from sorting in YYYY/MM format.
Here are an example of what I produced in .htaccess for redirects
# New redirects
redirect 307 /wp-content/uploads/2019/10/myAwesomedoc.pdf /myAwesomedoc.pdf
This wasn't fruitful as, unlike redirect plugins, this made the file system look for a file called "myAwesomedoc.pdf" in the root file, which of course didn't exist.
After the "redirect link" method wasn't satisfactory, I attempted to go the "wp-config.php" route and with what I found, I wasn't able to find a way to only change the various paths a PDF file may have on this website [e.g., "https://www.mywebsite.com/wp-content/uploads/2019/10/myAwesomedoc.pdf", "https://www.mywebsite.com/projects/assignments/myAwesomedoc.pdf", "https://www.mywebsite.com/products/documents/myAwesomedoc.pdf"].
I appreciate any help I may get on this.
I guess you got confused between Redirection and Url rewriting!
There is a possible way of doing that with .Htaccess by matching pdf extensions with regex and pointing them to the real path in media directory.
But some anomalies would happen cause of different dates patterns used by wordpress!
I think Redirection wouldn't solve the issue unless files are already moved to root directory.
How about you write your own plugin for this feature?
Would this feature be shared with other users (editors, contributors...)?
For PDF files you don't any media processing functionalities (resizing...)
So The plugin would:
- upload files "Safely" to root folder.
- List only pdf files
It depends on how you are publishing those PDFs.. If they are meant for download only then you just share their links.
Otherwise with a viewer or extra information , you could define a custom post type for those docs and use custom fields...
Then you redirect old files urls to new location.

Wordpress plugin not recognizing required file

require_once($_SERVER['DOCUMENT_ROOT']."/Event_Logger.php");
global $Event_Logger;
$Event_Logger = new EventLogger();
Doing this displays a blank page and does not continue loading the page
The file on my server is located at public_html/Event_Logger.php
So I know I am doing this wrong $_SERVER['DOCUMENT_ROOT'] returns something I wasnt expecting so I am asking how do I get the top level of my site basically the folder public_html
You can't (and shouldn't) include a file using URLs. You should use the path to the file locally, on the same server. If the plugin you're building lives on technologyforthefuture.org, then you can require it from the root path. If you're trying to include the file from an external website, that's not possible.

Custom directory viewer (php)

I started a little project a few days ago, a directory viewer. (Not a redesigned htaccess thing.)
It's written in PHP and works great except for a few little things.
I have one file (masterfile) where all parts of the viewer (css, php, ..) come together and build the final viewer. Whenever you access a directory without an index.php, index.html, etc. in it, you should end up in this masterfile and see your directory (-content).
Example: example.com/css/ => You're in css dir => Show custom dir viewer (css folder)
Idea: Disable .htaccess indexing which produces an 403 error, redirect this error to masterfile.
Options -Indexes
ErrorDocument 403 /masterfile.php
This does work, however it lists the content of the masterfile directory and not the content from original folder (example: /css/) Ideas?
Possible solution (I don't like): Put a file, that includes this "masterfile", in EVERY directory and name it index.php
I hope you guys have some ideas, I appreciate any help!
You can put something like this at the top of masterfile.php:
$parsed = parse_url($_SERVER['REQUEST_URI']);
$files = scandir($_SERVER['DOCUMENT_ROOT'].$parsed['path']);
maybe with some adjustment depending of your server configuration. The parse_url stuff is to remove eventual GET and/or segments.

How to include php file to wordpress theme(index.php)

I would like to include cars.php file to index.php theme; the cars.php file is script, which loads data from the server and works as booking system, when I am trying to connect as usual way include('/.cars.php');, and check the source code in browser, its there, but no forms appear.
Could be the problem with wordpress engine itself?
I think you have a typo include('/.cars.php'); is likely include('./cars.php');
What you have now would look in the root directory for a file called .cars.php

Categories