wp_enqueue_script in action hook wp_print_scripts - wordpress

In the WordPress documentation page for the function wp_enqueue_script, it is clearly written:
Note: This function will not work if it is called from a wp_head action, as the tags are output before wp_head runs. Instead, call wp_enqueue_script from an init action function (to load it in all pages), template_redirect (to load it in public pages only), or admin_print_scripts (for admin pages only). Do not use wp_print_scripts (see here for an explanation).
Do not use wp_print_scripts action is what I want to highlight, but do a simple google search on "How to include Javscript in WordPress". You will find most of examples are using wp_print_scripts action to call wp_enqueue_script. And it seems like everyone is ok with it.
So am I missing or misunderstanding something here?
EDIT
The codex has been modified. It now says:
This function will not work if it is called from a wp_head or wp_print_scripts actions, as the files need to be enqueued before those actions are run. See the Usage section for the correct hooks to use.

Yeah, I been there lots of times. The short answer is all those guys that print script requests are all wrong or as some folks suggest it is faster not to use wp_enqueue_script but to load everything dynamically with Modernizr.load (yesnope.js) or any other js loader lib.
My recommendation would be to stick with wp_enqueue_script as the best practice, but if you are developing themes for mobile, or you are extremely worried about speed and not bloating your browser with requests, I really recommend to load everything dynamically with a single js file (that you may call with wp_enqueue_script). Even consider inlining everything out and not loading jQuery but zepto.js or so.
Many developers do this terrible practice of loading everything printed, not only bad taste, but terrible for experienced theme developers I might say :S
For example thematic and several other blank themes rely on this practices and this truncates development speed with childthemes.
If you are not sure about wp_enqueue_script just give the wordpress codex a read. It is thoroughly documented.
Regards,

Related

Should I make woocommerce template via action hooks or edit directly?

I'm learning WooCommerce Theme development. I have a question about making template.
For example, I'm making "archive-product.php". Should I edit some HTML code to this file to design layout? Or should I use action hooks to design? Which is better?
I have copied this file to "mytheme/woocommerce" folder.
Working with custom template files was, in my experience, more typical years ago than today, the exception being when you're radically re-designing them. Working with hooks and filters instead will generally be more flexible and maintainable across WC, WC add-on, and theme changes and updates, and, if properly organized, the functions will be easier to switch on and off during debugging, or to remove at whatever point.

Locating involved functions in WordPress

I'm looking at a site someone else developed. We're using a child theme that is calling back to a lot of its parent's functions. There are about two dozen plugins. Somewhere in there is a bug that's preventing a theme setting from propagating to the rendered page. The problem is, I am not sure what's involved.
I've tried enabling wp_debug, but the page is fairly silent. I've inserted HTML comments into functions and templates that I thought would be involved, but I haven't found one that is actually involved.
Is there a plugin or some way to force WordPress to log every file/function that's loaded for a page? How would you approach this scenario?
This is probably not the best answer, but I've dealt with a lot of similar situations on some of my customers who own WordPress websites.
I usually download the database and website locally and setup a local website using LocalWP, but you can use any web server like XAMPP or WAMP, actually. In the end, this is helpful, because I can quickly find apply functions of my hooks by doing a full-text search on the project.
To track down the error, I echo some logging with a helper function which will also print hook information.
If the code of your child theme is reached, this is a good start. If your website renders with the parent theme, you can focus on the hooks and calls in your child theme and hopefully you will find the error.
You really have to bite the bullet and follow the chain. If your parent theme renders anything you should be able to comment your child theme step by step to pinpoint the cause of the problem.

How can I control where Wordpress injects code from a plugin (into the page)

I have a Wordpress plugin that appears abandoned by its author.
The plugin makes 1 terrible mistake that I'm trying to fix: It injects itself in the top of the page, on top of everything else, at the beginning of the head element. That causes problems and is bad practice.
How can I edit this beast so it will inject itself just before /head?
Is there a function like this:
injectAtHeadClose(<?php echo "stuff that don't belong in the first line of the page.");?>
Or maybe some other way to achieve this? I'm sure that most plugins control where their code gets injected, but I just don't know how.
Sorry if I'm misusing the word "inject."
Thanks.
You're looking for hooks. There are two types in wordpress - filters and actions.
Here is an explanation of the plugin API (Hooks):
Plugin API
List of Actions
List of Filters

Get outuput of wp_head or wp_footer in wordpress Rest API

Is there a way to get what come with wp_head() or wp_footer() in rest api call?
I mean scripts and styles that can come from plugin or WordPress itself when I use wp_enqueue_script for example?
The reason is that I want to make everything with js framework, but I want to save the extendable by the plugins (not with all but if it's for styling for example that gives only special style).
If it's impossible so it would be good to know why...?
Also, if there is a way to circumvent this it also would be helpful in future.
You would have to create a custom API endpoint which contains the code for the header or footer. Then on whatever you're consuming the API with you'd have to parse and execute that code.

remove plugin on specific template page

I am using http://wordpress.org/extend/plugins/pdf24-post-to-pdf/ pdf generator plugin for my site, but I only want it present on certain pages.
How can I remove it from all pages except a certain template I allow?
I believe this can be done with filters, but I have googled and googled and can't seem to get anywhere.
I don't think that's possible without modifying the plugin source code. That would be a nice security issue, IMO.
Plugins are files simply included on the loading process. At least, there's no hook I could find on WordPress source code for you to interfere in this behavior. Take a look just above the plugins_loaded hook, and the wp_get_active_and_valid_plugins function.
What you can try to do, instead, is just modifying the plugin with the condition you need. In the top of the pdf24.php file, just below the standard comments, put:
if (!condition_met())
return;

Resources