Bypass the Wordpress Template Entirely - wordpress

I'm writing a simple plugin to take a specialized URI, run a specialized wp_query, and return JSON. As a result, I want to bypass all of the standard WordPress template system. My current method is to hook into the 'wp' action, do the query, print the JSON, and then die(), but this bypasses the caching system.
I'd like to run my custom query, output the JSON, and then tell Wordpress to skip over everything else in its lifecycle and immediately do the 'shutdown' action so that the JSON request can be cached for the next time.
I can't find anything in the codex that allows me to completely bypass the template system and jump to the shutdown action. What am I missing?

Look at the "template_include" hook.
https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include
It is run immediately before the template file is included.

Related

Wordpress: Disable WP Object Caching DURING Runtime

I tried to disable WP Object Caching during Runtime using
wp_using_ext_object_cache(false);
Unfortunately it still does not update, e.g. user meta data, from the DB. So when I change a value directly in PHPMYAdmin, I would expect this to be read DIRECTLY out of the DB freshly.
Does wp_using_ext_object_cache not change/work during runtime?
I think I just found the solution after having searched hours and hours before actually posting.
# Disable WP Obj Caching ...
wp_using_ext_object_cache(false);
wp_cache_flush();
wp_cache_init();
Seems to really cleaning/resetting the WP_Object Object behind it -> So all subsequent calls will directly read from DB!

Programmaticaly spoofing an http script request in an iframe

I'm building a backend admin system which edits json files that control the look and feel of the main site. I want to add a 'preview' button before the user hits save. To do that, I want to use the main site, but instead of calling the actual json file in production, save a temp version of it and redirect this user's traffic for that file to the temp file - from the original site code.
i've considered both chrome pluggins, configuring iframe somehow or, in worst case scenario, grabbing the production front-end, parsing out the call to the prod json file and replacing with new temp json file. That is obviously not ideal as it would entail a lot of work and if anything changes on the prod site, this will have to be updated.
I would love your ideas!
Do you have access to the main site's source code? You could implement a preview option from the main site which accepts a GET parameter and uses a temporary JSON setting based on this GET parameter.
From the backend admin system's point of view, it's just a matter of adding the JSON as part of the ajax GET request.
Unfortunately though, there is no easy way of doing this if you don't have access to the main site's source code or if you can't reach out to whoever maintains that main site.
Your cleanest option might be to recreate the main site's look and feel instead and pass it off as a "preview".

How do you remove the contact-info page from a plone site?

Every new plone site as a /contact-info/ url with a generic email form on it.
Spambots can easily find this and use it spam the webmaster with malicious emails (specifically note that the 'subject' can be set in the email).
This old thread http://plone.293351.n2.nabble.com/modify-or-disable-site-contact-form-td7258555.html suggests that you can simply update the template so it is an empty page. However, this is not enough to prevent spam.
wget http://localhost:8080/contact-info --post-data "sender_fullname=d&sender_from_address=doug#localhost.com&subject=d&message=d&form.submitted=1"
Will still send email on the form handler.
How do you completely disable this form handler?
edit: I see you can modify the form handler script at:
/portal_skins/plone_form_scripts/send_feedback_site/manage_main
/portal_skins/custom/contact-info
To generate a custom handler script and replace the default actions. However, this doesn't really remove the page, it just makes it do nothing.
What you must customize is not the template but the end point the send_feedback_site script.
Replace the whole script code with:
from zExceptions import NotFound
raise NotFound()
Or probably Unauthorized is better.
If you are running nginx or apache in front of your Plone site you could write a redirect rule for that URL. This could then be added to your 'standard' buildout so that it always is there for new sites.

Redirect modules

How can I get a module to redirect if a user doesn't have the correct permissions to view it instead of getting the usual "Access denied. You are not authorized to access this page." message?
If it is a module you are writing yourself use the goto function of Drupal
http://api.drupal.org/api/function/drupal_goto
If you are not writing it yourself then it's a bit tougher, you can set the error redirects with Drupal and some modules however for a specific module I think you might have to go in and patch it in some way.
pretty much anything you wanna do to modify drupal stock behaviour can be handled via hook_nodeapi
Custom Error provides the ability to easily customize 403 and 404 error messages. With that, you could use drupal_goto() to automatically redirect users, or drupal_get_destination() to build a login link that will return the user to the page they attempted to view.
For more general, login-specific functionality, you might check out LoginToboggan.
The source code for both modules will also include useful examples of how to approach this problem space if you do not want the overhead or external dependency of a module.

Best way to handle a 404 in a file download HTTP handler?

I've got a grid which provides some links for users to download files. Files are stored outside of the application, the path references are read from the database and a HTTP handler (*.ashx) is used to serve each requested file. It is, however, possible that there could be a database entry pointing to a non existent file. I catch the FileNotFoundException, but I'm not sure what would be the best method to inform the user of the missing file (so that they can contact support).
First idea is to set a standard 404 code on the response, and that's what I'm doing now.
A more helpful way would be to display a notification (jQuery) about a missing file, but the file download is not done in AJAX, so this would involve a two step process - a client side onclick handler calls a web service method to check if the file exists, if not, then I cancel the click (return false) and display a friendly message to the user. If the file exists however, I proceed with the normal execution. But this adds yet another server call.
Have you dealt with a similar problem? How did you solve it?
Some clarifications - the application is built in ASP.NET 2.0 and uses jQuery to call the web service methods.
Pawel, it seems you've answered your question already...
...(so that they can contact
support)...
By stating the above I would suggest you create a custom 404 page which notifies the user of the file not existing on disk and provide them information on how to get in contact with the support office.
I've created a HTTP Handler for handling files and if a file does not exist on disk then I return a 404 response. I've setup IIS to display a custom page if a 404 reponse has been thrown.. (and I do the same for error 500).
Hope this helps and good luck with finding the solution that fits your needs!

Resources