Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to fix "Undefined Index" on admin dashboard widgets? WordPress

  • SOLVED

I get the error "Notice: Undefined index: lmm-admin-dashboard-widget in /home/html/mapsmarker/wp-content/plugins/leaflet-maps-marker/leaflet-maps-marker.php on line 158" when displaying the admin dashboard widget for my plugin mapsmarker.com (just install the latest version from https://wordpress.org/extend/plugins/leaflet-maps-marker, turn on debug and look at the Dashboard).

I could find whats causing this. Here is the code (line 158 is bold) - any help is appreciated!

add_action('widgets_init', create_function('', 'return register_widget("lmm_recent_marker_widget");'));
if ( isset($lmm_options['misc_admin_dashboard_widget']) && ($lmm_options['misc_admin_dashboard_widget'] == 'enabled') ){
add_action('wp_dashboard_setup', array( &$this,'lmm_register_widgets' ));
}
}
function lmm_register_widgets(){
wp_add_dashboard_widget( 'lmm-admin-dashboard-widget', __('Leaflet Maps Marker - recent markers','lmm'), array( &$this,'lmm_dashboard_widget'), array( &$this,'lmm_dashboard_widget_control'));
}
function lmm_dashboard_widget(){
global $wpdb;
$table_name_markers = $wpdb->prefix.'leafletmapsmarker_markers';
$widgets = get_option( 'dashboard_widget_options' );
$widget_id = 'lmm-admin-dashboard-widget';
$number_of_markers = isset( $widgets[$widget_id] ) && isset( $widgets[$widget_id]['items'] ) ? absint( $widgets[$widget_id]['items'] ) : 4;
$result = $wpdb->get_results($wpdb->prepare("SELECT ID,markername,icon,createdon,createdby FROM $table_name_markers ORDER BY createdon desc LIMIT $number_of_markers"), ARRAY_A);
if ($result != NULL) {
echo '<table style="margin-bottom:5px;"><tr>';
foreach ($result as $row ) {
$icon = ($row['icon'] == NULL) ? LEAFLET_PLUGIN_URL . 'leaflet-dist/images/marker.png' : LEAFLET_PLUGIN_ICONS_URL.'/' . $row['icon'];
echo '<td><a href="' . LEAFLET_WP_ADMIN_URL . 'admin.php?page=leafletmapsmarker_marker&id=' . $row['ID'] . '" title="' . esc_attr__('edit marker','lmm') . '"><img src="' . $icon . '" style="width:80%;"></a>';
echo '<td style="vertical-align:top;line-height:1.2em;">';
echo '<a href="' . LEAFLET_WP_ADMIN_URL . 'admin.php?page=leafletmapsmarker_marker&id=' . $row['ID'] . '" title="' . esc_attr__('edit marker','lmm') . '">'.htmlspecialchars(stripslashes($row['markername'])).'</a>';
echo '</td></tr>';
}
echo '</table>';
} else {
echo '<p style="margin-bottom:5px;">' . __('No marker created yet','lmm') . '</p>';
}
<strong>if ($widgets[$widget_id]['blogposts'] == 0) </strong>
{
require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'class-simplepie.php');
$feed = new SimplePie();
if ( file_exists(LEAFLET_PLUGIN_ICONS_DIR . DIRECTORY_SEPARATOR . 'information.png') ) {
$feed->enable_cache(true);
$feed->set_cache_location($location = LEAFLET_PLUGIN_ICONS_DIR);
} else {
$feed->enable_cache(false);
}
$feed->set_feed_url('http://feeds.feedburner.com/MapsMarker');
$feed->init();
$feed->handle_content_type();
if ( $feed == NULL ) { echo 'not null'; }
echo '<hr><strong><p>' . __('Latest blog posts from www.mapsmarker.com','lmm') . '</p></strong>';
foreach (array_slice($feed->get_items(), 0, 3) as $item) {
echo '<p>' . $item->get_date('j F Y') . ': <strong><a href="' . $item->get_permalink() . '">' . $item->get_title() . '</a></strong><br/>' . $item->get_description() . '</p>'.PHP_EOL;
}
echo '<p>...</p>';
}
}

Answers (2)

2012-09-07

Martin Pham answers:

try this

if (isset($widgets[$widget_id]) && 0 == $widgets[$widget_id]['blogposts']) {
// something
}


Martin Pham comments:

please update function control_callback , for fisrt call function it will update the default value and use the code I posted above.

function lmm_dashboard_widget_control(){
$widget_id = 'lmm-admin-dashboard-widget';
$form_id = 'lmm-admin-dashboard-widget-control';
$update = false;
if ( !$widget_options = get_option( 'dashboard_widget_options' ) )
$widget_options = array();
if ( !isset($widget_options[$widget_id]) ) {
// default value
$widget_options[$widget_id] = array(
'blogposts' => 0
);
$update = true;
}
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST[$form_id]) ) {
$number = ($_POST[$form_id]['items'] == NULL) ? '3' : absint( $_POST[$form_id]['items'] );
//$number = absint( $_POST[$form_id]['items'] );
$blogposts = isset($_POST[$form_id]['blogposts']) ? '1' : '0';
$widget_options[$widget_id]['items'] = $number;
$widget_options[$widget_id]['blogposts'] = $blogposts;
$update = true;
}
if($update) update_option( 'dashboard_widget_options', $widget_options );
$number = isset( $widget_options[$widget_id]['items'] ) ? (int) $widget_options[$widget_id]['items'] : '';
echo '<p><label for="lmm-admin-dashboard-widget-number">' . __('Number of markers to show:') . ' </label>';
echo '<input id="lmm-admin-dashboard-widget-number" name="'.$form_id.'[items]" type="text" value="' . $number . '" size="2" /></p>';
echo '<p><label for="lmm-admin-dashboard-widget-blogposts">' . __('Hide blog posts and link section:') . ' </label>';
echo '<input id="lmm-admin-dashboard-widget-blogposts" name="'.$form_id.'[blogposts]" type="checkbox" ' . checked($widget_options[$widget_id]['blogposts'],1,false) . '/></p>';
}


Robert Harm comments:

that worked - thanks!

2012-09-07

John Cotton answers:

<blockquote>if ($widgets[$widget_id]['blogposts'] == 0) </blockquote>

Is that the line that's causing the problem?


If so, $widgets[$widget_id] is tested earlier so probably isn't an issue. Which would mean $widgets[$widget_id]['blogposts'] doesn't exist.

This might not really fix your problem, but it would make the error go away....:

Since there is a test you could just modify the line to

if (isset($widgets[$widget_id]['blogposts']) && $widgets[$widget_id]['blogposts'] == 0)

If you wanted the if code to run you could replace the logical and with an or.



Robert Harm comments:

yes, that line is causing the error - unfortunately, your code doesnt work - if I use a logical or, the undefined index warning still appears; if I use AND, the blog posts below are not shown :-/