Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a plugin.
In this plugin I have a function that will display some data.
In this function I use is_admin() to determine if the function is being called from the backend, because then I can add some additional information.

In an admin page, the function is loaded normaly. After an update using jQuery, the function is called to display new data.

The problem is that when the function is called through jQuery, the is_admin() is false, even if I am backend in admin mode.

Why isn't is_admin working whne called through jQuery?

Update

The code for is_admin() is called when I click the save button for a new event (or updating existing event).

After clicking save / update, the calendar list is updated through jQuery.

Here are parts of the plugin code.

// create custom plugin settings menu
if ( function_exists('add_action') ) {
  add_shortcode('eventcal', 'shortcode_display_event');
}

function shortcode_display_event($attr) {
  include_once('eventcal.class.php');
  $ec = new EventCalendar();

    extract(shortcode_atts(array(
        'type' => 'simple',
        'parent_id' => '',
        'category' => '',
        'count' => '10',
        'link' => ''
    ), $attr));

  $data['events'] = $ec->getMultipleEvents($cal_type, $count, $parent_id, $category);
  $data['link'] = $link;
  $data['type'] = $type;   
  $ec->displayCalendarList($data); 
}

This is the bit that is added if I'm in admin mode:

function extendedList( $data ) {
  (...)
  if(is_admin()) 
    $eventList .= '<th class="eventActions"></th>';
  $eventList .= '</tr></thead><tbody>';
  (...)
}

And here is the code for updating calendar list after update.

// New event - Form Submit
jQuery('#formNewEvent').live('submit' ,function() {

    var formData = jQuery('#formNewEvent').serializeArray();

    //Event ID is only given a value when an event has been clicked.
    if(jQuery('#event_id').val() != '')
    {

      // UPDATE EVENT
      jQuery.post("/wp-content/plugins/wp-eventcal/eventcal_jquery.php", { instance: 'updateEvent', formData : formData, eventID : jQuery('#event_id').val() },
      function(data) {
        if(data.status == 'success') {
          // Here we update calendar list after data has been updated
          jQuery.post("/wp-content/plugins/wp-eventcal/eventcal_jquery.php", { instance: 'getEventList' },
          function(list) {
            jQuery('#eventListContainer').html(list);                  
          });
        }      
      }, "json");
    }
    else {
      // ADD NEW EVENT
      jQuery.post("/wp-content/plugins/wp-eventcal/eventcal_jquery.php", { instance: 'addNewEvent', formData : formData },
      function(data) {
        // Here we update calendar list after new event have been added         
        if(data.status) {
          jQuery.post("/wp-content/plugins/wp-eventcal/eventcal_jquery.php", { instance: 'getEventList' },
          function(list) {
            jQuery('#eventListContainer').html(list);         
          });
        } 
      }, "json");        
    }
  return false;
}); 
share|improve this question

1 Answer 1

Hi @Steven:

Is it possible that you are not adding the jQuery() within an 'init' or 'admin_init'; my guess is 'yes'. Further, if you use an 'admin_init' hook you won't need to call is_admin().

Better yet, here's an answer about best practices when using jQuery within WordPress:

share|improve this answer
    
hmm... I think you might be right... but the code should be available even if I'm not backend (shortcode). I tested using init on add_shortcode, but hen the calendar was not rendered backend. –  Steven Jan 30 '11 at 13:29
    
@Steve - You've got a lot going on here so it's hard for me to isolate the problem without spending tons of time on this. Simple question for now: did you get something that works for you? If yes, maybe learn the nuances later. If not, please elaborate. –  MikeSchinkel Jan 31 '11 at 2:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.