简体   繁体   中英

bloginfo('template_directory') in jQuery to be used in Admin Panel

I am trying to customize contact form 7 database and for it I need to use bloginfo('template_directory') in jQuery.

As suggested in earlier posts I tried :

  1. I tried adding variable in header.php
  2. Used wp_localize_script & enqueue_script
  3. Even tried define('CONCATENATE_SCRIPTS', false);

But no luck. Please advise.

My Mistake as I didn't posted codes. Here are the codes that I am trying to use in wp admin where I am getting error for var custom.templateDir :

jQuery(document).ready(function($)
{
    jQuery(".acceptEntry").on("click", function($)        
    {
        $.preventDefault();

        console.log(custom.templateDir);

        var name_1 = jQuery('.name span').text();
        console.log(name_1);

        jQuery.ajax({
          type: "POST",
          url: custom.templateDir  + "/php-new-page.php",
          data: { name: name_1 }
        }).done(function( msg ) {
          alert( "Data Saved: " + msg );
        });    
    });
});

For templateDir , I even tried :

  1. url: "/php-new-page.php"
  2. Defining var templateDir in header.php

bloginfo function echoed the output, you need a return function like

get_bloginfo();

Suggest to use get_template_directory_uri() instead of get_bloginfo().

   function my_js_variables(){ ?>
            <script type="text/javascript">
            var ajaxURL = '<?php echo bloginfo('template_directory'); ?>';
            var ajaxnonce = '<?php echo wp_create_nonce( "itr_ajax_nonce" ); ?>';
          </script>
          <?php }
    add_action ( 'wp_head', 'my_js_variables' );

gives me me html codes on header.php :

<script type="text/javascript">
        var ajaxURL = 'http://www.eweb.co/mcb/wp/wp-content/themes/mcb';
</script>

But how to access ajaxURL on admin side?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM