How to include php file to wordpress theme(index.php) - 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

Related

Directory Mismatch while including other PHP files in it

I am working on a project (the first one) and got this error :
[I am NOOB and new]
First of all my directory is something like this: (Adding on main files)
[Yellow lines point to files and the purple one leads to a folder.]
I am working on the User Section right now.
I made a header folder and added a header.php file to it. which contains a header(): void. this header function basically has my navbar (few things are retrieved with DB). Also this same file is linked with .css and .js from AdminSection.
Now while making this function I was doing try and error on my user dashboard so it is now working 100% correctly as I wanted.
But when I included this function into another PHP file for let's say UserProfile it doesn't work anymore as all the included files in Header.php were working for UserDashboard.php (since it is in another directory which is working because of my try and error.)
I searched for a bit about this and found no solution but to make a header file for every directory which seems to be stupid.
Any solutions for it?
A Snippet from my header file if required
[The paths are working for Dashboard.php but not for files in another directory. I want a solution where I have only one header.php and it works for every file no matter its directory]

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.

PHP include fail to open path of included files

I'm trying to include file from another php framework but doing so it's giving me an error failing open the stream for the files which are included inside the file I'm trying to include.
Any idea on how to include it properly so that the files included inside my included file are able to be processed?
What Framework are you using?
I have little experience with templates but I don't think you should be adding PHP code to a template file.
Click Here for another question on how to add PHP code to a template file.
However as far as your path goes, try using:
include("../../other/index.php");
Your include only looks only one directory up.
include ("../../other/index.php");
would be two directories up so into the "admin" directory then the "other" directory
This has been asked before here

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.
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

cannot directly call the php file inside folders using the directory path

I have one root folder called GASS where I put all my php files and other related folders (templates,images,js,fonts,css)inside. When i try to run my project in localhost, http://localhost/GASS/alarm_A16GSM.php everything went smoothly. I wanted to change the URL to be more specific, http://localhost/GASS/alarmsystem/16zone/A16/overview.php thus i rename the php file and put it inside folders.
GASS
alarm-system
16-zone
A16
overview
However when i try to run the new URL,the page shows error.This is the error message:
Warning: include(templates/header.php): failed to open stream: No such file or directory in .....
Code for the first URL where the page load successfully.
<div class="overview"><a href="alarm_A16GSM.php" id="overview-selected"><span>
Code for the new URL where the page shows error.
<a href="alarm-system/16-zone/A16/overview.php" id="overview-selected">
It seems like i need to configure something which i do not know what it is.
How am i going to load the page successfully using the new URL? How am i going to traverse four levels up to the root directory so that the page load successfully? Why i cannot directly call the php file using the(alarm-system/16-zone/A16/overview.php) path?
p/s: sorry for my bad English.
It looks like there is a line in your Php file, probably like
include 'templates/header.php';
Include can't find the file using that relative path, because you moved the calling file.
Probably you could change that to
include '../../../../templates/header.php';
To get back down to the GASS folder that apparently has a folder called 'templates' with a file 'header.php' that is required.
An absolute path would be good, instead but it refers to the filesystem path, not webserver path - so you'd need to know your web root folder name on the server.
Copying all the folders (templates,images,js,fonts,css) to the folder overview will solve the issue. Now there is no template file on the folder 'overview' so header.php is failed to load. Another option is create a file save all the included file path and call this file.

Categories