wp-load.php & require_once, in an ajax call - ajax

I am developing a WP Plugin.
Currently, I am making an ajax call with jquery to a standalone php file in my plugin folder.
Let's presume that this file is called test.php.
The file is NOT loaded prior to this call, so no native wordpress functions work in this file.
As a result, I have require_once('wp.load'); in test.php.
Here stems my questions:
The only function I need from wordpress is $wpdb->insert. Is there a better function to include than wp-load? Something that doesn't include so much, since all I need is this one function!
I understand that require_once loads the file, ONLY if it hasn't been loaded yet. Maybe I'm overlooking the situation, but how does this work with ajax? If there is a "success" response, does test.php close, thus when its called again, it loads wp-load.php again?
Thanks!

If there is a "success" response, does test.php close, thus when its
called again, it loads wp-load.php again?
Exactly. The concept behind require_once (or include_once) is to make sure you don’t reload the same functions, classes & variables into the same currently running PHP code at the same time:
include_once may be used in cases where the same file might be
included and evaluated more than once during a particular execution of
a script, so in this case it may help avoid problems such as function
redefinitions, variable value reassignments, etc.
And since PHP scripts run only on request, what you describe is exactly how it would happen.
That said, I wouldn’t over-think the concept of loading the whole wp.load each time it’s needed. I doubt it will cause such a performance hit that it would even be noticeable.

You should consider follow this method to write an Ajax powered plugin. There is usualy no need to create a separate PHP file and load WordPress core into that file. Read that carefully and you'll find out, that is relatively easy to accomplish and is more flexible thant stand alone PHP file...

Related

Turn off mod rewrite for specific template in Codeigniter

If I have a view located at application/views/templates/games/gamename/ is there a way to turn off mod_rewrite for that directory?
Also, is there a way to not use the smarty templating for that view?
The reason I want to do this is because I have a java script game which unfortunately is having trouble getting data from the backend, therefore the game fails to load and gets stuck on the preloader. This is also why I want to turn off the smarty template because it is not properly loading the correct assets.
I think you're focusing yourself in the wrong way to solve the problem. If you're having trouble with your request from the frontend to the backend, then, change the call in the JS to point out the right file, or route the call correctly in application/config/routes.php

How to manage URLs in CodeIgniter so they can be updated in a single place

I believe Smarty templates has functionality built in that allows you to manage your site URLs from a config file so if something gets moved, you only have to update the URL in one place. Does this sort of functionality exist in CodeIgniter? If not, any pointers or examples on how/where to add it?
For example:
Instead of hard-coding the link it would be: Settings
But where would you want to set $links so that it was available everywhere? Or is it really best to just hard code them?
Take a look at the config class. It allows you to make custom config files.
It's not entirely made for URL's but you sure can use them.
The base url should be basically right at the start of /app/config/config.php, where app is the name of your codeigniter application folder. You access it through calls to the base_url() function.
Yes, it's called Routes, configuration located at config/routes.php. Documentation
If you ask about the rendered html of the links, then your best bet would be using site_url() in conjunction with constants, for example site_url(URL_SETTINGS);, there is no built in functionality for that, but I can say I don't think that is necessary as it would be used too rarely, but it would influence performance every single load.

Codeigniter Cache

I'm trying to implement caching in code igniter. I'm completely new to this so it might be a dumb question but lets see if there's an answer out there.
I've gone to database.php and enabled caching and placed my path. I believe the path is fine because my delete all files call removes the index.html and the htaccess file from that folder. However nothing gets cached as I use the app. I've tried to turn cache on using $this->db_cacheon() but nothing seems to write any files to that folder. My setup is that I have models to handle the connection to the database table in question. So inside of that model I would do a select from tablename and try to have that cached but nothing seems to happen. Does anyone have an idea what I could do to fix this?
There are three types of caching. I don't know much about database caching. I have done output caching.
$this->output->cache(n);
Whatever page you want to cache,
you can use this in controller function which loads your view page.
You can find your cache pages in system/cache folder.
Do you mean $this->db->cache_on()
Have a look at the manual:
http://codeigniter.com/user_guide/database/caching.html
http://codeigniter.com/user_guide/libraries/caching.html

Does use of echo base_url(); to call CSS, images and Javascript files make a website slow?

I am using Codeigniter. I am keeping my images,CSS and Javascript files in a folder called "support" in the document root of my application. So my document root folder looks like this-
.settings
application
support
system
.buildpath
.project
index
.htacces
Now my question is will it make my website take time to load as I have to use <? echo base_url();?>support/ every time I need to get something from my support folder? Because you see when I am using <? echo base_url();?>I am actually calling the full website address.. and I have 7 CSS and 13 javascript files to call from "support" so it will definitely take time to load the website. (Please correct me if I am wrong). If you think by this a website can get slow could you please tell me where exactly should I put my CSS,images and javascript files in. I heard views is not a good place for this.
Thanks in Advance :)
This question is probably bigger than you think.
First of all, using <? echo base_url();?> instead of "hard-coding" your web address will not slow down your site. A function call like this is very negligible to the speed of loading your pages.
I think the other part of your question is regarding architecture.
When you think of speed for your website, you need to know what factors slow down the loading of your page. (Although not an exhaustive list, this will help in your case):
the number of files (images, css, javascript, etc.) that need to load for your page
the cache-ability of those files
some server side header nonsense (e-tags and so-forth)
the processing to build your php pages
the size of your page
Now, in your instance, I would recommend putting all of your "static" files in the document root under a folder (say static). Then, access them all in your "views" with the base_url() function.
This way, your page as it's delivered to the browser, will make external calls for those static files - allowing the browser to cache all of those files (assuming the headers are set up correctly). If you put them into views, then they're actually added to the page that is being requested. So, the next page that is requested has to download those files again along with that second page being requested. Make sense?
To help with the "number of files", you can always concatenate and minify any css/javascript that you have. So instead of the browser downloading and caching 8 js files, you can serve it 1 js file with all of your code.

.getJSON call to URL

I'm a little confused as to what exactly is going on here. For instance...
http://bidstick.com/latest/21249%7C21250%7C21252.js&localtime=1263468042061/
Is pulling down some JSON information, and the numbers being passed are specifying which auctions need information, but what is processing these variables. Is there some server side script that generates the JSON? It appears to be dynamic, as you can manually change the numbers and get a different response. I.E. just change any of the numbers in the link and it will provide different information. Thanks in advance.
I think what is puzzling you is: how does the response change when the parameters in the URL are changed?
If so, here's your answer:
Server side scripts are generating the output that you see. These scripts read the input URL and know what parameters to work on. (More accurately, these parameters are GET arguments). So a server script can read this, and decide what to do accordingly.
PS: now I come to think of it, you must be knowing all this, since you already know what JSON is.
EDIT:
Such scripts are not for the perusal of users of the website. This output is for use by the website itself, or for use by other websites.
To accomplish this: there are two ways I can think of:
URL rewriting
processing PHP inside a .js file (yes this is possible).
You can do that by telling Apache that .js files should be processed by PHP
I believe that can be done using the AddType directive of Apache's config file.
Yes, there is a server-side script that is generating some kind of JSON-serialized output. Both ASP.NET MVC and Ruby on Rails have really simple ways of generating JSON results from what look like standard URLS.

Resources