Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress myAjax is not defined

I am working in child theme and put following code for admin ajax js

function wpb_adding_scripts() {
/*  echo "string". get_stylesheet_directory_uri().'/css/jquery.bxslider.css';
    exit();*/
    wp_register_script('flexslider', get_stylesheet_directory_uri() . '/js/jquery.flexisel.js', array('jquery'),'1.1', true);
    wp_enqueue_script('flexslider');
    wp_enqueue_script('bxslider', get_stylesheet_directory_uri() . '/js/jquery.bxslider.min.js', array(),true, true);
    wp_enqueue_script('bxslider');

    wp_enqueue_script('custom', get_stylesheet_directory_uri() . '/js/custom.js', array(),true, true);
    wp_enqueue_script('custom');
    //wp_localize_script('admin_script', 'ajaxurl', admin_url( 'admin-ajax.php' ) );

    wp_localize_script('admin_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script('admin_script');

} 

add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts', 999 );

but it given me error like

ReferenceError: myAjax is not defined
    url : myAjax.ajaxurl,

I used myAjax declaration in my custom js..

jQuery('#load_more_posts').on('click',function(){
    var lng =jQuery(".post_item").length;

    jQuery.ajax({
      type : "post",
      url : myAjax.ajaxurl,
       data : {action: "load_more_posts_home",count : lng},
 }).done(function(response){
      var posts = JSON.parse(response);

      for( var i = 0; i < posts.length; i++ )
      {
        if( posts[i] == "0" )
          jQuery("#load_more_posts").fadeOut();
        else
          jQuery("#load_more_posts").before(posts[i]);
      }

    });
});

so how could i resolve this issue in my wordpress child theme.

like image 810
maddy Avatar asked Nov 30 '16 05:11

maddy


People also ask

Where is admin-Ajax PHP in WordPress?

By default, WordPress directs all Ajax calls through the admin-ajax. php file located in the site's /wp-admin directory.

What is URL in Ajax call?

The url parameter is a string containing the URL you want to reach with the Ajax call, while settings is an object literal containing the configuration for the Ajax request. In its first form, this function performs an Ajax request using the url parameter and the options specified in settings .

What is Wp_ajax_nopriv?

What is wp_ajax_nopriv ? wp_ajax_ prefix isn't used to create the hook name if user is not logged in, therefore wp_ajax_nopriv_ is used. wp_ajax_nopriv_ does the same thing as wp_ajax_ prefix does, unless it only fires when user is not logged in. So for not logged-in user the hook name is – 'wp_ajax_nopriv_' .


1 Answers

Try this:

wp_enqueue_script('custom'); //Name of the script. Should be unique.here is 'custom'
wp_localize_script('custom', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));  // remove admin_script and add unique javascript file.

Here above code localized the object:'myAjax' in script "custom". and you can access property "ajax_url" by adding below code in custom script file.

in custom.js

alert(myAjax.ajaxurl);
like image 155
vrajesh Avatar answered Oct 17 '22 10:10

vrajesh