wp_enqueue_script o wp_head

Many plugins that I have downloaded use the wp_head action hook to add JavaScript to the header, I have always used the wp_enqueue_script() to enter JS/Ajax inside tags. What is the difference? And is this correct?

preguntado el 04 de julio de 12 a las 02:07

2 Respuestas

The proper action to hook into when enqueueing scrips is wp_enqueue_scripts

There is no documentation for this hook yet see admin_enque_scripts, wp_enqueue_scripts is basically the same hook except that it only runs on the front end.

If you use the wp_print_scripts hook your scripts will also be added to the admin.

Vea también el publicación de blog de desarrollo para más información.

Respondido el 20 de junio de 20 a las 10:06

El documentation page for WP actions says that they are related.

Runs when the template calls the wp_head function. This hook is generally placed near the top of a page template between <head> y </head>. This hook does not take any parameters.

Runs just before WordPress prints registered JavaScript scripts into the page header.

Therefore, if you wish to emit a script block <script>....</script>, you could use either. wp_print_scripts seems appropriate if you'd like your script block to immediately precede the script blocks for registered script references.

Respondido el 20 de junio de 20 a las 10:06

Many thanks. I searched for the documentation and I didn't return anything. Thank you very much for clearing this up for me. I suppose that many plugin authors fail to know the difference, and that is perhaps the reason with the inconsistency with the code that I have seen. - elmejorJORT

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.