How to Globally Use wp_localize_script() Ajax URL





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}






up vote
1
down vote

favorite
1












I have added this to my functions.php and need to use ajaxURL in all of enqueued scripts in the template (instead of enqueuing only one script here



add_action( 'wp_enqueue_scripts', 'ajaxify_enqueue_scripts' );
function ajaxify_enqueue_scripts() {
wp_localize_script( 'ajaxify', 'ajaxURL', array('ajax_url' => get_template_directory_uri() . '/app/login.php' ));
}
add_action( 'wp_ajax_nopriv_set_ajaxify', 'set_ajaxify' );
add_action( 'wp_ajax_set_ajaxify', 'set_ajaxify' );


but when I try to call an ajax method I am getting this error



Uncaught ReferenceError: ajaxURL is not defined


Is there any way to add the ajaxURL to all scripts?










share|improve this question
























  • What action is ajaxify_enqueue_scripts hooked to?
    – Milo
    Nov 16 at 4:40










  • Hi Milo sorry I just updated the code (forget to add the action). As you can see it is hooked to 'wp_enqueue_scripts'
    – Behseini
    Nov 16 at 4:48

















up vote
1
down vote

favorite
1












I have added this to my functions.php and need to use ajaxURL in all of enqueued scripts in the template (instead of enqueuing only one script here



add_action( 'wp_enqueue_scripts', 'ajaxify_enqueue_scripts' );
function ajaxify_enqueue_scripts() {
wp_localize_script( 'ajaxify', 'ajaxURL', array('ajax_url' => get_template_directory_uri() . '/app/login.php' ));
}
add_action( 'wp_ajax_nopriv_set_ajaxify', 'set_ajaxify' );
add_action( 'wp_ajax_set_ajaxify', 'set_ajaxify' );


but when I try to call an ajax method I am getting this error



Uncaught ReferenceError: ajaxURL is not defined


Is there any way to add the ajaxURL to all scripts?










share|improve this question
























  • What action is ajaxify_enqueue_scripts hooked to?
    – Milo
    Nov 16 at 4:40










  • Hi Milo sorry I just updated the code (forget to add the action). As you can see it is hooked to 'wp_enqueue_scripts'
    – Behseini
    Nov 16 at 4:48













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have added this to my functions.php and need to use ajaxURL in all of enqueued scripts in the template (instead of enqueuing only one script here



add_action( 'wp_enqueue_scripts', 'ajaxify_enqueue_scripts' );
function ajaxify_enqueue_scripts() {
wp_localize_script( 'ajaxify', 'ajaxURL', array('ajax_url' => get_template_directory_uri() . '/app/login.php' ));
}
add_action( 'wp_ajax_nopriv_set_ajaxify', 'set_ajaxify' );
add_action( 'wp_ajax_set_ajaxify', 'set_ajaxify' );


but when I try to call an ajax method I am getting this error



Uncaught ReferenceError: ajaxURL is not defined


Is there any way to add the ajaxURL to all scripts?










share|improve this question















I have added this to my functions.php and need to use ajaxURL in all of enqueued scripts in the template (instead of enqueuing only one script here



add_action( 'wp_enqueue_scripts', 'ajaxify_enqueue_scripts' );
function ajaxify_enqueue_scripts() {
wp_localize_script( 'ajaxify', 'ajaxURL', array('ajax_url' => get_template_directory_uri() . '/app/login.php' ));
}
add_action( 'wp_ajax_nopriv_set_ajaxify', 'set_ajaxify' );
add_action( 'wp_ajax_set_ajaxify', 'set_ajaxify' );


but when I try to call an ajax method I am getting this error



Uncaught ReferenceError: ajaxURL is not defined


Is there any way to add the ajaxURL to all scripts?







theme-development ajax wp-localize-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 at 4:47

























asked Nov 16 at 4:29









Behseini

2801315




2801315












  • What action is ajaxify_enqueue_scripts hooked to?
    – Milo
    Nov 16 at 4:40










  • Hi Milo sorry I just updated the code (forget to add the action). As you can see it is hooked to 'wp_enqueue_scripts'
    – Behseini
    Nov 16 at 4:48


















  • What action is ajaxify_enqueue_scripts hooked to?
    – Milo
    Nov 16 at 4:40










  • Hi Milo sorry I just updated the code (forget to add the action). As you can see it is hooked to 'wp_enqueue_scripts'
    – Behseini
    Nov 16 at 4:48
















What action is ajaxify_enqueue_scripts hooked to?
– Milo
Nov 16 at 4:40




What action is ajaxify_enqueue_scripts hooked to?
– Milo
Nov 16 at 4:40












Hi Milo sorry I just updated the code (forget to add the action). As you can see it is hooked to 'wp_enqueue_scripts'
– Behseini
Nov 16 at 4:48




Hi Milo sorry I just updated the code (forget to add the action). As you can see it is hooked to 'wp_enqueue_scripts'
– Behseini
Nov 16 at 4:48










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










You can conditionally echo the code on only few templates or specific pages. Here is an example:



add_action ( 'wp_head', 'my_js_variables' );
function my_js_variables(){
// for specific page templates
$current_template = get_page_template();

// return if there is no page template, or if the page template is other than template-x1.php or template-x2.php
if( !isset($current_template) || ( $current_template != 'template-x1.php' && $current_template != 'template-x2.php' ) ){ return; } ?>
<script type="text/javascript">
var ajaxurl = <?php echo json_encode( admin_url( "admin-ajax.php" ) ); ?>;
var ajaxnonce = <?php echo json_encode( wp_create_nonce( "itr_ajax_nonce" ) ); ?>;
var myarray = <?php echo json_encode( array(
'foo' => 'bar',
'available' => TRUE,
'ship' => array( 1, 2, 3, ),
) ); ?>
</script>
<?php
}





share|improve this answer










New contributor




vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    1
    down vote













    To have the ajaxurl variable available on the frontend the easiest way is to add this snippet to you theme’s function.php file:



    add_action('wp_head', 'myplugin_ajaxurl');
    function myplugin_ajaxurl() {
    echo '<script type="text/javascript">
    var ajaxurl = "' . admin_url('admin-ajax.php') . '";
    </script>';
    }


    This get the url of the ajax submission page and creates a variable in the head of the HTML with it. Now ‘ajaxurl’ is available in your theme so you can start making it more modern and dynamic.






    share|improve this answer










    New contributor




    vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • Thanks a lots vikrant but I have two questions now, 1- is there any way to add this to specific head instead of all head? 2- I have seen some example at this methos using var ajaxnonce = '<?php echo wp_create_nonce( "itr_ajax_nonce" ); ?>'; can you please let me know what is the usage of nonce here?
      – Behseini
      Nov 16 at 4:44












    • i send some example please check . This code help for you .
      – vikrant zilpe
      Nov 16 at 5:55











    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "110"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f319373%2fhow-to-globally-use-wp-localize-script-ajax-url%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    You can conditionally echo the code on only few templates or specific pages. Here is an example:



    add_action ( 'wp_head', 'my_js_variables' );
    function my_js_variables(){
    // for specific page templates
    $current_template = get_page_template();

    // return if there is no page template, or if the page template is other than template-x1.php or template-x2.php
    if( !isset($current_template) || ( $current_template != 'template-x1.php' && $current_template != 'template-x2.php' ) ){ return; } ?>
    <script type="text/javascript">
    var ajaxurl = <?php echo json_encode( admin_url( "admin-ajax.php" ) ); ?>;
    var ajaxnonce = <?php echo json_encode( wp_create_nonce( "itr_ajax_nonce" ) ); ?>;
    var myarray = <?php echo json_encode( array(
    'foo' => 'bar',
    'available' => TRUE,
    'ship' => array( 1, 2, 3, ),
    ) ); ?>
    </script>
    <?php
    }





    share|improve this answer










    New contributor




    vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote



      accepted










      You can conditionally echo the code on only few templates or specific pages. Here is an example:



      add_action ( 'wp_head', 'my_js_variables' );
      function my_js_variables(){
      // for specific page templates
      $current_template = get_page_template();

      // return if there is no page template, or if the page template is other than template-x1.php or template-x2.php
      if( !isset($current_template) || ( $current_template != 'template-x1.php' && $current_template != 'template-x2.php' ) ){ return; } ?>
      <script type="text/javascript">
      var ajaxurl = <?php echo json_encode( admin_url( "admin-ajax.php" ) ); ?>;
      var ajaxnonce = <?php echo json_encode( wp_create_nonce( "itr_ajax_nonce" ) ); ?>;
      var myarray = <?php echo json_encode( array(
      'foo' => 'bar',
      'available' => TRUE,
      'ship' => array( 1, 2, 3, ),
      ) ); ?>
      </script>
      <?php
      }





      share|improve this answer










      New contributor




      vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        You can conditionally echo the code on only few templates or specific pages. Here is an example:



        add_action ( 'wp_head', 'my_js_variables' );
        function my_js_variables(){
        // for specific page templates
        $current_template = get_page_template();

        // return if there is no page template, or if the page template is other than template-x1.php or template-x2.php
        if( !isset($current_template) || ( $current_template != 'template-x1.php' && $current_template != 'template-x2.php' ) ){ return; } ?>
        <script type="text/javascript">
        var ajaxurl = <?php echo json_encode( admin_url( "admin-ajax.php" ) ); ?>;
        var ajaxnonce = <?php echo json_encode( wp_create_nonce( "itr_ajax_nonce" ) ); ?>;
        var myarray = <?php echo json_encode( array(
        'foo' => 'bar',
        'available' => TRUE,
        'ship' => array( 1, 2, 3, ),
        ) ); ?>
        </script>
        <?php
        }





        share|improve this answer










        New contributor




        vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        You can conditionally echo the code on only few templates or specific pages. Here is an example:



        add_action ( 'wp_head', 'my_js_variables' );
        function my_js_variables(){
        // for specific page templates
        $current_template = get_page_template();

        // return if there is no page template, or if the page template is other than template-x1.php or template-x2.php
        if( !isset($current_template) || ( $current_template != 'template-x1.php' && $current_template != 'template-x2.php' ) ){ return; } ?>
        <script type="text/javascript">
        var ajaxurl = <?php echo json_encode( admin_url( "admin-ajax.php" ) ); ?>;
        var ajaxnonce = <?php echo json_encode( wp_create_nonce( "itr_ajax_nonce" ) ); ?>;
        var myarray = <?php echo json_encode( array(
        'foo' => 'bar',
        'available' => TRUE,
        'ship' => array( 1, 2, 3, ),
        ) ); ?>
        </script>
        <?php
        }






        share|improve this answer










        New contributor




        vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer








        edited Nov 16 at 9:58









        grgarside

        1053




        1053






        New contributor




        vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered Nov 16 at 5:54









        vikrant zilpe

        1478




        1478




        New contributor




        vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.
























            up vote
            1
            down vote













            To have the ajaxurl variable available on the frontend the easiest way is to add this snippet to you theme’s function.php file:



            add_action('wp_head', 'myplugin_ajaxurl');
            function myplugin_ajaxurl() {
            echo '<script type="text/javascript">
            var ajaxurl = "' . admin_url('admin-ajax.php') . '";
            </script>';
            }


            This get the url of the ajax submission page and creates a variable in the head of the HTML with it. Now ‘ajaxurl’ is available in your theme so you can start making it more modern and dynamic.






            share|improve this answer










            New contributor




            vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • Thanks a lots vikrant but I have two questions now, 1- is there any way to add this to specific head instead of all head? 2- I have seen some example at this methos using var ajaxnonce = '<?php echo wp_create_nonce( "itr_ajax_nonce" ); ?>'; can you please let me know what is the usage of nonce here?
              – Behseini
              Nov 16 at 4:44












            • i send some example please check . This code help for you .
              – vikrant zilpe
              Nov 16 at 5:55















            up vote
            1
            down vote













            To have the ajaxurl variable available on the frontend the easiest way is to add this snippet to you theme’s function.php file:



            add_action('wp_head', 'myplugin_ajaxurl');
            function myplugin_ajaxurl() {
            echo '<script type="text/javascript">
            var ajaxurl = "' . admin_url('admin-ajax.php') . '";
            </script>';
            }


            This get the url of the ajax submission page and creates a variable in the head of the HTML with it. Now ‘ajaxurl’ is available in your theme so you can start making it more modern and dynamic.






            share|improve this answer










            New contributor




            vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • Thanks a lots vikrant but I have two questions now, 1- is there any way to add this to specific head instead of all head? 2- I have seen some example at this methos using var ajaxnonce = '<?php echo wp_create_nonce( "itr_ajax_nonce" ); ?>'; can you please let me know what is the usage of nonce here?
              – Behseini
              Nov 16 at 4:44












            • i send some example please check . This code help for you .
              – vikrant zilpe
              Nov 16 at 5:55













            up vote
            1
            down vote










            up vote
            1
            down vote









            To have the ajaxurl variable available on the frontend the easiest way is to add this snippet to you theme’s function.php file:



            add_action('wp_head', 'myplugin_ajaxurl');
            function myplugin_ajaxurl() {
            echo '<script type="text/javascript">
            var ajaxurl = "' . admin_url('admin-ajax.php') . '";
            </script>';
            }


            This get the url of the ajax submission page and creates a variable in the head of the HTML with it. Now ‘ajaxurl’ is available in your theme so you can start making it more modern and dynamic.






            share|improve this answer










            New contributor




            vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            To have the ajaxurl variable available on the frontend the easiest way is to add this snippet to you theme’s function.php file:



            add_action('wp_head', 'myplugin_ajaxurl');
            function myplugin_ajaxurl() {
            echo '<script type="text/javascript">
            var ajaxurl = "' . admin_url('admin-ajax.php') . '";
            </script>';
            }


            This get the url of the ajax submission page and creates a variable in the head of the HTML with it. Now ‘ajaxurl’ is available in your theme so you can start making it more modern and dynamic.







            share|improve this answer










            New contributor




            vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer








            edited Nov 16 at 9:05









            grgarside

            1053




            1053






            New contributor




            vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered Nov 16 at 4:39









            vikrant zilpe

            1478




            1478




            New contributor




            vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            vikrant zilpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.












            • Thanks a lots vikrant but I have two questions now, 1- is there any way to add this to specific head instead of all head? 2- I have seen some example at this methos using var ajaxnonce = '<?php echo wp_create_nonce( "itr_ajax_nonce" ); ?>'; can you please let me know what is the usage of nonce here?
              – Behseini
              Nov 16 at 4:44












            • i send some example please check . This code help for you .
              – vikrant zilpe
              Nov 16 at 5:55


















            • Thanks a lots vikrant but I have two questions now, 1- is there any way to add this to specific head instead of all head? 2- I have seen some example at this methos using var ajaxnonce = '<?php echo wp_create_nonce( "itr_ajax_nonce" ); ?>'; can you please let me know what is the usage of nonce here?
              – Behseini
              Nov 16 at 4:44












            • i send some example please check . This code help for you .
              – vikrant zilpe
              Nov 16 at 5:55
















            Thanks a lots vikrant but I have two questions now, 1- is there any way to add this to specific head instead of all head? 2- I have seen some example at this methos using var ajaxnonce = '<?php echo wp_create_nonce( "itr_ajax_nonce" ); ?>'; can you please let me know what is the usage of nonce here?
            – Behseini
            Nov 16 at 4:44






            Thanks a lots vikrant but I have two questions now, 1- is there any way to add this to specific head instead of all head? 2- I have seen some example at this methos using var ajaxnonce = '<?php echo wp_create_nonce( "itr_ajax_nonce" ); ?>'; can you please let me know what is the usage of nonce here?
            – Behseini
            Nov 16 at 4:44














            i send some example please check . This code help for you .
            – vikrant zilpe
            Nov 16 at 5:55




            i send some example please check . This code help for you .
            – vikrant zilpe
            Nov 16 at 5:55


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f319373%2fhow-to-globally-use-wp-localize-script-ajax-url%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            QoS: MAC-Priority for clients behind a repeater

            Ивакино (Тотемский район)

            Can't locate Autom4te/ChannelDefs.pm in @INC (when it definitely is there)