1

Hey guys I'm having a huge problem initializing jQuery on the backend of WordPress (widgets.php). I'm building a widget to display some select options that can only be accessed through SOAP, so I had to ajaxify it using admin-ajax.php. Everything works perfectly on the front-end but when it comes to the backend it breaks completely.

function widget_inject() {
echo "<script>
jQuery(document).ready(function($) {
var ajaxurl = '".admin_url('admin-ajax.php')."';
var list_target_id = 'list-target'; //first select list ID
var list_select_id = 'list-select'; //second select list ID
var initial_target_html = '<option value=\"\">Please select category...</option>'; 

$('#'+list_target_id).html(initial_target_html);

$('#'+list_select_id).change(function(e) {

var selectvalue = $(this).val();


$('#'+list_target_id).html('<option value=\"\">Loading...</option>');

if (selectvalue == \"\") {

   $('#'+list_target_id).html(initial_target_html);
} else {

  $.ajax({url: ajaxurl,
  data: {
        action: 'parentcatajax1',
        parentCat: selectvalue
    },
         success: function(output) {
            //alert(output);
            $('#'+list_target_id).html(output);
        },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status + \" \"+ thrownError);
      }});
    }
});
});</script>";
}
add_action('admin_enqueue_scripts','widget_inject');

^This is what I'm trying. I've tried admin-init, admin-head, admin-footer none of them seem to work. & yeah I have...

add_action('wp_ajax_nopriv_parentcatajax1', 'parentCatCallback1');
add_action('wp_ajax_parentcatajax1', 'parentCatCallback1');

for my ajax function; it works perfectly on the front end.

I'm at a stand still for a client & can't figure out what to do. Any suggestions? Thanks in advance!

4
  • 2
    not working is not very helpful, what does console say? is the script showing on pages source? but offhand, try adding (jQuery) after your document ready closing bracket. Commented Jul 1, 2014 at 23:23
  • It's not saying anything in the console, except the regular postmessage.js stuff that wordpress spits out. & yeah the script is registered in the source...I read that I have to add the .live function even though it's depreciated. Not sure why, I tried it & I can't get it to work. Maybe I'm doing it worng not sure. Thanks for the quick reply. Commented Jul 2, 2014 at 3:37
  • Okay I heard mozilla had a good debugger so I went with Firebug. Firebug gives me the error. jQuery(...).ready(...) is not a function Commented Jul 2, 2014 at 6:48
  • I didn't say not working, it works perfectly on the frontend and then does nothing at all on the backend of Wordpress (This is what I think is key as it's probably a certain action that I'm missing) Commented Jul 2, 2014 at 6:57

1 Answer 1

2

Your printing your jQuery before wordpress has initialized jQuery. Wp_enqueue scripts is not the point where it starts printing the scripts onto the page. The below will clear your jQuery not defined error, let me know if there are more errors after this.

function widget_inject() {
   echo "<script>
             jQuery(document).ready(function($) {
         alert('ready');//re-enter your code here
             })(jQuery);
         </script>";
}
add_action('admin_print_scripts','widget_inject', 100);//hook= 'admin_print_scripts'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.