Relative and absolute path in wordpress doesn't work - wordpress

I'm improving a word-press web site. I need to include a file to a index WordPress theme, but that happen is really frustrating for me.
If I do in the index.php wordpress theme file, everything is ok:
include('myfile.php'); // myfile.php is of course in the same folder as index.php.
And the include is done properly.
But, if I try to move "myfile.php" to , for example, the father folder, the next thing should work, but no...:
include('../myfile.php');
This is for me really frustrating because doesn't have sense...
Next thing I have tried, setting all the absolute path:
include('http://example.com/myfile.php') but again, it doesn't work.

Wordpress has shortcuts for this. Try using
<?php get_template_part('myfile'); ?>
In fact, this is the right way to do an include

the absolute path must be the path of your file system.
you can try use realpath() php function, which solves the absolute path of the file through the parameter relative path.
see documentation http://php.net/manual/en/function.realpath.php

Related

Problems with css inside a directory

I have a problem where I want to redirect to a css file which stylesheet link is inside essentials.php in the main directory. however when including the essentials.php the browser redirects to the wrong url see image. how can i fix this?
I fixed my issue with a php variable $site which is defined as the url, in this way i can change the variable from localhost to the real site and is it easy to switch. In this way I do not have directory problems since the whole url is given.
$siteurl=localhost/project
href="<?php echo "$siteurl";?>/videos/watch?v=">

Image path in css not working properly

I am trying to use images in css file in my laravel project that makes use of twitter bootstrap and Jason Lewis' Basset.
Now when I use a image in my css like
.div {
background: url('asset/img/photo.jpg');
}
I get the path to
http://localhost/asset/css/less/assets/img/photo.jpg
However the real path should be
http://localhost/~Username/laravelProject/public/asset/img/photo.jpg
The only way to achieve this is to use this url in my css
/../~Username/laravelProject/public/asset/img/photo.jpg
You guys would understand this is a no go if I have to move the site from local to server and have to change all the urls every time.. So Why is this not working properly? I am using this laravel starter site made by andrew13
startersite github
Last but not least.. this is the structure
The issue is that the path is bellow the css doc, relative file paths work by adding there files location to the beginning of the url, this means that it is looking for (Mypath)/asset/img/photo.jpg
(or: public/asset/css/asset/img/photo.jpg from what I can see)
Without a PHP server to return dynamic url's, sadly your only 2 options are:
Move the CSS to the root, or at least lower then the IMG
Use a absolute url
With php you have a few options, my favorite being a "Node" structure
How this works is placing a php file in the root folder that sets a constant of its location:
<?php
define('ROOTPATH', dirname(__FILE__));
?>
When ever you want to use this just include the file, and use ROOTPATH:
<?php
include 'TheFileName.php';
echo ROOTPATH,'/public/asset/img/photo.jpg';
?>
On most php servers this is already set to $_SERVER['DOCUMENT_ROOT'], but on a local host with will need to use a node.
Please note this is sudo code, some things may have to be changed

CodeIgniter CSS relative links

I've looked at other answers involving this question, but still can't get it to work. I'm trying to link my CSS file relatively. I've tried the base_url() concat and it still doesn't work. Maybe I just have a misconception about CI URLs. Anyway, my CSS file is under views->templates->Item_CSS.css
I have tried everything and still get no results. If anyone can help I'd be grateful. Thanks!
You won't be able to reach that path easily, since codeigniter uses it's index.php (situated in the root) to handle everything. The views folder isn't the base path
What I do is to add a css and js folder in the root of CI (where index.php,.htaccess lies), after that your css routes will be www.example.com/css/mycss.css or base_url().css/mycss.css.
In case you get a forbidden error you might need to modify the .htaccess to allow accessing those folders
Assuming that you config your base url as:
$config['base_url'] = 'http://www.abc.com/';
and you put the application folder where the system and index.php file is. Then use the following code:
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>application/views/templates/Item_CSS.css" />

Path to css and images

For example I have site http://localhost/site
In IIS I set that 404 error causes redirection to default.aspx
If I type something like http://localhost/site/nodirectory , (there are no such folder) all works perfectly.
But if I only add slah at end http://localhost/site/nodirectory/, page can't display css and images.
Images and css are located in their own folder. I tried different paths: "gfx/logo.gif", "/gfx/logo.gif"
Does anyone have some ideas about that?
If your css and images are relative paths, say ResolveClientUrl("~/gfx/logo.gif") this renders to the client as src="gfx/logo.gif", which the browser with a slash thinks is /nodirectory/gfx/logo.gif instead of just /gfx/logo.gif.
To resolve this, don't use .ResolveClientUrl(), use .ResolveUrl(), this will make the src render src="/gfx/logo.gif" The beginning / makes it definitive, it's that path from the root of the domain.
You'll see this same hebavior if you're doing paths that start with ../ or gfx/ yourself...make them relative to the application base so there's no chance of confusion.
There are a couple of options...
1)
In your HTML page, make the path to CSS and scripts relative...
"/scripts/myscript.js"
Where the scripts folder is the first folder after the root folder
2)
You can add the base tag to your page, which means ALL page resources will be treated as relative to the root location you specify...
<base href="http://www.mysite.com">
More info about these two options.
If you can, option 1 is perhaps a bit cleaner. You know explicitly the resources that you are affecting. Using the base tag will affect ALL relative paths on your page. Images, Links, Scripts, CSS et al. The second option works best if you developed your 404 page assuming it would be in the root folder, but it could actually be referenced from any non-existent directory. You just put your root address in the base tag and it will all behave exactly as you expect.
With either option, the images can be relative to the location of your CSS file.

Drupal paths in themes

On certain pages drupal_get_path isn't working correctly (or it is and I've got the wrong function)
The base path is wrong
Example:
Image is supposed to be at
http://domain.com/sites/all/modules/pecapture/images/headline_dontmissout.jpg
But when on
http://domain.com/node/9
The URL is
http://domain.com/node/sites/all/modules/pecapture/images/headline_dontmissout.jpg
The same happens on the page
http://domain.com/admin/build/ and block edit page
How do I get the right path?
added base_path() to beginning of my paths...
base_path (http://api.drupal.org/api/base_path), if you use php code.
In html case, just add "/", like: /sites/all/modules/pecapture/images/headline_dontmissout.jpg
One problem: if you work on subfolder (Drupal installed in internal folder of main site): http://domain.com/subfoldersite, it will not correct, becase will remove "subfoldersite".

Resources