wp_localize_script in WordPress: Passing PHP Data to JavaScript

Поділитися
Вставка
  • Опубліковано 26 вер 2024
  • Follow the link below to Check Out the Ultimate Guide on How to add Javascript & jQuery to WordPress
    plethorathemes...
    Make any server side data (PHP) available to your scripts (JavaScript) using the WordPress wp_localize_script function.
    JavaScript Code: gist.github.co...
    PHP Code:
    gist.github.co...
    Codex: wp_localize_script Reference
    codex.wordpres...
  • Навчання та стиль

КОМЕНТАРІ • 6

  • @bm_pug
    @bm_pug 3 роки тому

    Good day. Your video is getting the closest to an answer I think I might need. So what f Im not on a child theme but a custom theme entirely?

  • @edwards9845
    @edwards9845 4 роки тому

    What if we want to use plain javascript?

    • @plethorathemes
      @plethorathemes  4 роки тому +1

      Hello Edward. You can use plain JavaScript without any problem. Just replace the jQuery code with a simple JS function.

    • @edwards9845
      @edwards9845 4 роки тому

      @@plethorathemes Thanks. Would you be able to share the code for plain Javascript?

    • @plethorathemes
      @plethorathemes  4 роки тому +2

      @@edwards9845 Sure. You can use the following code:
      document.addEventListener("DOMContentLoaded", function(){
      alert( php_data.message );
      });
      Let us know if everything worked out OK for you.

    • @edwards9845
      @edwards9845 4 роки тому

      @@plethorathemes Thanks!