Path to file from Functions.php
-
I”m trying to implement a rating system in my theme and there is a class made up of a bunch of functions that I’ve put in my functions.php file. One of the functions is throwing a warning. The function has a reference to a file that is used to log ratings in the template directory. I think I need a different syntax using WordPress path references but I don’t know how. I’d appreciate any help.
Here is the line of code I’m referring to.
var $RATES_RESULT_FILE = "results.txt";
-
You need an absolute path– either an absolute URL using
bloginfo()
or, most likely, an absolute filepath usingABSPATH
orTEMPLATEPATH
.I’ve tried TEMPLATEPATH but that didn’t work. Maybe I didn’t use the correct syntax.
Along the same subject there is a line in the javascript that calls the class from the functions file and I don’t know how to get there regarding the path issue. If you can help I’d appreciate it.Thanks
Here is the call from the javascript file that lives in a js folder
httpObject.open("GET", "https://localhost/wordpress/wp-content/themes/modernscribe/functions.php?item="+item+"&rating="+rate+"&max="+max, true);
Here is the referance to the text file that is a writable file to write rates and ip addresses to. The way I’m using it now and it works.
var $RATES_RESULT_FILE = "wp-content/themes/modernscribe/results.txt";
Here is the way I tried to do it using TEMPLATEPATH:
var $RATES_RESULT_FILE = TEMPLATEPATH . '/results.txt';
Use TEMPLATEPATH and echo the path so that you can read it and make sure its correct.
The way I’m using it now and it works.
Ummm…. so what’s the problem?
Is this–
var $RATES_RESULT_FILE = TEMPLATEPATH . '/results.txt';
— part of a javascript function?Well It was working until I realized that the javascript wasn’t working correctly as it was letting me vote several times and not updating. So I wrote a direct path from the javascript file using
httpObject.open("GET", "https://localhost/wordpress/wp-content/themes/modernscribe/functions.php?item="+item+"&rating="+rate+"&max="+max, true);
Then I got this error
Warning: fopen(wp-content/themes/modernscribe/results.txt) [function.fopen]: failed to open stream: No such file or directory in C:\wamp\www\wordpress\wp-content\themes\modernscribe\functions.php on line 107Warning: Invalid argument supplied for foreach() in C:\wamp\www\wordpress\wp-content\themes\modernscribe\functions.php on line 26
Sorry I didn’t include all that earlier.Is this– var $RATES_RESULT_FILE = TEMPLATEPATH . ‘/results.txt’;– part of a javascript function?
No it’s part of the class located in the functions.php file.
Let me try this again. Since I didn’t explain the issue to well last time and I think apljdi gave up on me.
I have a star rating package that is not a plug in. The package has three files that interact with each other from a template file created by me under a WordPress theme.
One of the tree files is a php file that holds a php class. with all the needed functions. I copied the class and placed it in my functions.php file so it could be called from my template file.
The second file is a text file that gets ip addresses and rating scores written to it by the php class.
The third file is a javascript file that is called by the header of the template file and also has a function that submits the rating to the class in the functions.php file which then writes to the results.txt file.
Okay. The first thing I did was copy the class into the functions.php file and theen fired up the package with a call from my template file with this line of code.
‘<?php $handler->displayRating(‘Book1′,5); ?>’
which creates a new class for rating but threw this warning on my page.Warning: Invalid argument supplied for foreach() in wordpress/wp-content/themes/mytheme/functions.php line 26
which is this line of code.
‘foreach ($fileContent as $line) {‘.I traced the code back to the creation of the $fileContent variable which had it’s roots in this line of code that creates a class variable with the results.txt file as the value.
‘var $RATES_RESULT_FILE = “results.txt”;’
I then changed that line to this.
‘var $RATES_RESULT_FILE = “wp-content/themes/mytheme/results.txt”;’ with the result of no error and the rating blocks being displayed. As I tested I realized I was able to vote more than once which was a bug. In the javascript file there is a function that controls the submit rating action. Within this function I found that it had a line to open the original file that stored the php class which I had copied to the functions.php file. Here is the original line of code.
‘httpObject.open(“GET”, “submitRating..php?item=”+item+”&rating=”+rate+”&max=”+max, true);’
I changed it to this line of code.
‘httpObject.open(“GET”, “https://localhost/wordpress/wp-content/themese/mytheme/functions.php?item=”+item+”&rating=”+rate+”&max=”+max, true);’
I reloaded my page without error. I moused over the rating block no error. I clicked on the rating block and got two warnings.Warning: fopen(wp-content/themes/mytheme/results.txt) [function.fopen]: failed to open stream: No such file or directory in C:\wamp\www\wordpress\wp-content\themes\mytheme\functions.php on line 107
Here is line 107
‘$f = fopen($this->RATES_RESULT_FILE,”a+”);’Warning: Invalid argument supplied for foreach() in C:\wamp\www\wordpress\wp-content\themes\mytheme\functions.php on line 26
Now we have come full loop with the same error I started out with due to this line of code.
‘foreach ($fileContent as $line)’.
Sorry this is so envolved. I just wanted to be clear and concise. I would paste more of the code but I was told not to paste large blocks of code here.
All I’m really trying to achieve here is to learn about WordPress and how it deals with paths and how I can deal with such issues in the future. If I wanted to go the easy route I would just get a plugin.Thanks in advance if you can help or willing to try
Since I didn’t explain the issue to well last time and I think apljdi gave up on me.
Didn’t give up. Try to remember that, as much fun as this is, I do get hungry and ‘The Man’ makes me pay for my own food– even makes me work for the money to pay for the food. ‘The Man’ is a jerk, man. ??
Ok. I’m unclear on something. I am assuming it was ‘submitRating.php’ that you copied. Did you copy the whole file into functions.php or just part of it? Because its being used in a ‘GET’ request, it seems to me that it might be best to leave at least part of it in a separate file.
You can post code to the pastebin if you need.
Hey. Thanks for helping out. I forget that others have the luxury of work. I’m unemployed and I promised my wife I would learn a new career so I’m spending all of my time learning WordPress, PHP, MySQL and Javascript. I am working on this about 15 hours a day so I’m sorry that I made it sound if you were not helpful. I’m getting the hang of how this forum works. I know not to bump an issue too. Anyway. What you said above
it seems to me that it might be best to leave at least part of it in a separate file.
seems to be true. I replaced the call to the file ‘submitRating.php’ from the javascript file and left the class in the functions.php file and everything worked fine. Should I post the entire php class in the bin or just certain lines? Thanks again for helping.
If its working fine, there is no need to post anything more. Good luck.
Okay. Well I appreciate your time. Have fun.
Regarding your previous code…
var $RATES_RESULT_FILE = TEMPLATEPATH . '/results.txt';
I don’t think that’ll work declaring the path in that manner..
I believe you’d need to build the path in the constructor ..
class someclassname { var $RATES_RESULT_FILE = '/results.txt'; function someclassname() { $this->RATES_RESULT_FILE = TEMPLATE_PATH . $this->RATES_RESULT_FILE; .. no more code, above just for illustration..
- The topic ‘Path to file from Functions.php’ is closed to new replies.