$variable filepath to theme in require() statement
-
Hi,
I am new to WordPress with limited experience with PHP, but have a reasonable understanding of programming structure.
As you do, I am wanting to expand and tailor WordPress to achieve the aims of my Web Site.
To do so, I have come across a plug-in, that, by the author’s own admission is a bit “quick and nasty”. But it will do the job with one major exception.
The script tries to intercept a click on a link to do a conditional check. Based on the condition, the script either allows the link to proceed to the original page or diverts the user to another page.
Unfortunately, because of the initial click, the page headers of the original page are sent. Then if the conditional check diverts it to another page, the error “headers already sent” occurs.
With my limited knowledge I also did a “quick and nasty” work around. By my figuring, I could not stop the original headers from being sent, but I could stop them from being sent a second time by stripping the headers from the alternate page and slotting in a require() in the script’s condition thus linking that page up with the original page headers. ( They are basically the same anyway).
So I did this and it works great.
BUT……
Knowing the alternate page and its location, it was simple enough to code in a hard link to the require(). ie;
\\require (“https://www.my_site.com/wp-content/themes/my_theme/my_alternate.php”);\\
But, to ensure the portability of the theme, I want to amend the require() so that it works in any theme without the need to hard code the new theme name.
So I attempted to amend the original script by adding a couple of variables.
Now, before reading this, please understand that during two sleepless nights I have tried everything I can think off using every combination of (), “”‘, ‘, normal variables, global variables, etc. etc, etc, So if you wonder why I have globals etc here, its just where I have ended up. But, having globals or not having them does not seem to affect my problem.
So, I declared two global variables directly below the function call. They are $myfile and $mypath.
After the conditional where my original hard coded require() statement was I now have the following code.
\\$myfile = (“/my_alternate.php”);\\
\\$mypath = (bloginfo(‘template_url’));\\
\\$mypath = trim(“$mypath” . “$myfile”);\\
\\require $mypath;\\
\\print ($mypath);\\
(The purpose of the print ($mypath); is just a temporary test of the variable contents – see more below)
In running this amendment to the require() I get the following errors;
“Warning: my_function(/my_alternate.php): failed to open stream: No such file or directory in /home/mysite3/public_html/wp-content/plugins/script.php on line 208
Fatal error: my_function(): Failed opening required ‘/my_alternate.php’ (include_path=’.:/usr/lib/php’) in /home/mysite3/public_html/wp-content/plugins/script.php on line 208″
I know that the correct path is being picked up by the above code because if I substitute the require $mypath; above with the hard coded require:
\\ (“https://www.my_site.com/wp-content/themes/my_theme/my_alternate.php”); \\
the script functions correctly and the print($mypath) prints https://www.my_site.com/wp-content/themes/my_theme/my_alternate.php at the bottom of the page.
So what am I missing here? Do I have to declare the path string as a path? How can I get the above require $mypath; to work?
TIA
– Neil
- The topic ‘$variable filepath to theme in require() statement’ is closed to new replies.