• In WordPress 3.9.2 access to admin-ajax was working for non loggedin users. When I upgraded the script stop working.

    I have the following code in functions.php

    function theme_name_scripts() {
        wp_enqueue_script('returnval', get_template_directory_uri() . '/library/js/ajax.js', array('jquery'), true );
    
        wp_localize_script('returnval', 'MyAjax', array(
            // URL to wp-admin/admin-ajax.php to process the request
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
    
          // generate a nonce with a unique ID "myajax-post-comment-nonce"
            // so that you can check it later when an AJAX request is sent
            'security' => wp_create_nonce( 'str' )
        ));
    
        wp_enqueue_script( 'jquery' );
    
    }
    
    /*We need this for security */
    add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
    
    function get_images_callback() {
       // check_ajax_referer('my-special-string', 'security' );
    
            $postID=$_REQUEST['postid'];
            $secImg = get_post_meta($postID, '_cmb_secImg', true );
            $post = get_post($postID);
            $content = apply_filters('the_content', $post->post_content);
            $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'meta_query' => array(array('key' => 'mc_selectDisplay','value'=>1)) );
            $attachments = get_posts( $args );
    
            if ( $attachments ) {
                $imgRecord = 1;
                foreach ( $attachments as $attachment ) {
                    $attachUrl=wp_get_attachment_url( $attachment->ID );
                    $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
                    $title = get_post_meta($attachment->ID, 'title', true);
                    $lnk= get_post_meta($attachment->ID, 'mc_url', true);
                    $description = $attachment->post_content;
                    $images[] = array('id'=>$imgRecord,'url'=>$attachUrl,'lnk'=>$lnk,'descr'=>$description,'alt'=>$alt,'title'=>$title);
                    $imgRecord = $imgRecord + 1;
                }
            }
        echo json_encode($images);
        die(); // this is required to return a proper result
    }
    
    add_action('init', 'ajax_Security');
    function ajax_Security(){
        add_action('wp_ajax_get_images', 'get_images_callback');
        add_action('wp_ajax_nopriv_get_images', 'get_images_callback');
    }

    In My ajax.js i have the following code.

    jQuery(document).ready(function($) {
    
    /*Get post id */
      var pstID = 50;
    
        var data = {
            action: 'get_images',
            postid: pstID,
            security : MyAjax.security,
            dataType: 'JSON'
        };
    
        /*Preload images */
        $.fn.loadImages= function() {
            this.each(function(){
                $('<img/>')[0].src = this;
            });
        }
    
        jQuery.ajax({
            url: MyAjax.ajaxurl,
            data:data,
            context: document.body
        }).done(function(data)
          {
    
              var dbreturn= JSON.parse(data);
    
              var imgStr="";
              var lstImages="";
              var preloadImg=[];
    
              /*Loop Thru json data */
              for(var i = 0; i < dbreturn.length; i++) {
                  var dbField = dbreturn[i];
                  imgStr=imgStr+'<div class="slide"><div class="textslide"><h2>'+dbField.descr+'</h2></div><a href="'+dbField.lnk+'"><img class="img-responsive" src="'+dbField.url+'" alt="'+dbField.alt+'"></a></div>';
                  preloadImg[i]=dbField.url;
              }
    
              /*Preload images */
              $(preloadImg).loadImages();
    
              /*Append images to slider */
              $("#homeSlider").html(imgStr);
    
              /*initialize slider */
              jQuery('#homeSlider').cycle({pauseOnHover: true,slides:"> div.slide",pager:"#pager"});
    
        });
    
    });

    This code was working in WordPress 3.9.2

Viewing 1 replies (of 1 total)
  • Thread Starter sgate1

    (@sgate1)

    Still trying to figure this out. Its probably something I am missing since the upgrade.

Viewing 1 replies (of 1 total)

The topic ‘admin-ajax.php not working after update’ is closed to new replies.