admin管理员组文章数量:1393035
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
2 Answers
Reset to default 1Is 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");
}
本文标签: pluginsProblems with jQuery and isadmin()
版权声明:本文标题:plugins - Problems with jQuery and is_admin() 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744662556a2618333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论