Debug errors on sidebar

I’m uising a child theme of twentyfourteen on wordpress 3.8. When in debug mode, I get the following errors regarding a widget.

Notice: Trying to get property of non-object in
wordpresswp-contentthemestema2014functionswidgets.php on line
148

Read More
update_post_meta($post->ID, "_sidebar", $_POST["link"]);

And

Notice: Undefined index: link in
wordpresswp-contentthemestema2014functionswidgets.php on line
148

update_post_meta($post->ID, "_sidebar", $_POST["link"]);

And

Notice: Undefined index: _sidebar in
wordpresswp-contentthemestema2014functionswidgets.php on line
121

$link    = $custom["_sidebar"][0];

Here is the full code.

$dynamic_widget_areas = array (
                                'sidebar-21'       => __( 'Page Specific Sidebar 1', 'pietergoosen' ),
                                'sidebar-22'       => __( 'Page Specific Sidebar 2', 'pietergoosen' ),
                                'sidebar-23'       => __( 'Page Specific Sidebar 3', 'pietergoosen' ),
                                'sidebar-24'       => __( 'Page Specific Sidebar 4', 'pietergoosen' ),
                                'sidebar-25'       => __( 'Page Specific Sidebar 5', 'pietergoosen' ),
                                'sidebar-26'       => __( 'Page Specific Sidebar 6', 'pietergoosen' ),
                                'sidebar-27'       => __( 'Page Specific Sidebar 7', 'pietergoosen' ),
                                'sidebar-28'       => __( 'Page Specific Sidebar 8', 'pietergoosen' ),
                                'sidebar-29'       => __( 'Page Specific Sidebar 9', 'pietergoosen' ),
                                'sidebar-30'       => __( 'Page Specific Sidebar 10', 'pietergoosen' ),
                );

foreach ( $dynamic_widget_areas as $id => $dynamic_widget_area ) {
        register_sidebar(
                array (
                'name'          => __( $dynamic_widget_area, 'pietergoosen' ),
                'id'            =>  $id,
                                'description'   => __( 'Page specific sidebars above the content sidebar', 'pietergoosen' ),
                                'before_widget' => '<aside id="%1$s" class="widget %2$s">',
                                'after_widget'  => '</aside>',
                                'before_title'  => '<h1 class="widget-title">',
                                'after_title'   => '</h1>',
        ));
    }
}
add_action( 'widgets_init', 'pietergoosen_widgets_init' );     

// Add dynamic sidebars
add_action('admin_init', 'sidebar_init');
        add_action('save_post', 'save_sidebar_link');
        function sidebar_init(){
                add_meta_box("sidebar_meta", "Sidebar options", "sidebar_link", "page", "side", "default");
        }
        function sidebar_link(){
                global $post, $dynamic_widget_areas;
                $custom  = get_post_custom($post->ID);
                $link    = $custom["_sidebar"][0];
        ?>
<div class="link_header">
        <?
        echo '<select name="link" class="sidebar-selection">';
        echo '<option>Choose Sidebar</option>';
        echo '<option>No sidebars to choose from</option>';
        foreach ( $dynamic_widget_areas as $list ){
                    if($link == $list){
                      echo '<option value="'.$list.'" selected="true">'.$list.'</option>';
                        }else{
                      echo '<option value="'.$list.'">'.$list.'</option>';
                        }
                }
        echo '</select><br />';
        ?>
</div>
<p>Choose the sidebar to use with this page.</p>
<?php
}
function save_sidebar_link(){
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {return $post->ID;}
        update_post_meta($post->ID, "_sidebar", $_POST["link"]);
}
add_action('admin_head', 'sidebar_css');
function sidebar_css() {
        echo'
        <style type="text/css">
                .sidebar-selection{width:100%;}
        </style>
        ';
}

Any ideas how to solve this?

Related posts

2 comments

  1. You should check that the variable exists before using it. isset() will return true if a variable has been set and is not null.

    For example:

    update_post_meta($post->ID, "_sidebar", $_POST["link"]);
    

    will become:

    if ( isset( $_POST["link"] ) ) {
      update_post_meta( $post->ID, "_sidebar", $_POST["link"] );
    }
    

    As an aside, it’s a good idea to validate your data before saving.

  2. Yes I have been noticing the same warning and notices with version 3.8, but there are no notices as in version 3.7, what you can do in this case is use

    $variable = empty($_POST["link"])? 'default_value' : $_POST["link"];
    

    If you want to supress notices go to wp-config.php and change the constant WP_DEBUG to false.

    define('WP_DEBUG',false);
    

Comments are closed.