plugins - Problems with jQuery and is_admin()

I've created a plugin.In this plugin I have a function that will display some data.In this function I use is_admi

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;
}); 

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 edited Jan 30, 2011 at 13:26 Steven asked Jan 30, 2011 at 11:40 StevenSteven 2,6207 gold badges41 silver badges61 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

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:

  • What is the best way to add custom javascript files to the site?

Check the class attribute for body: If the theme is using body_class() the body has a class named logged-in for users that are logged in. Be aware the function can be used on the element html too.

Example

if(jQuery('body').hasClass('logged-in')){
    alert("Logged In");
}

if(!jQuery('body').hasClass('logged-in')){
    alert("Logged Out");
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744662556a4586549.html

相关推荐

  • plugins - Problems with jQuery and is_admin()

    I've created a plugin.In this plugin I have a function that will display some data.In this function I use is_admi

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信