1

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 \wordpress\wp-content\themes\tema2014\functions\widgets.php on line 148

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

And

Notice: Undefined index: link in \wordpress\wp-content\themes\tema2014\functions\widgets.php on line 148

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

And

Notice: Undefined index: _sidebar in \wordpress\wp-content\themes\tema2014\functions\widgets.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?

  • Your code is incomplete, the $custom array is returned by a function called get_post_custom, which is absent from your code examples. What your seeing is a standard PHP notice that your trying to access something that isn't there. This happens in both 3.7 and 3.8, it is not new, the only change is you're now aware of it. I recommend you install XDebug so that you can catch more mistakes and bugs like this that normally go unseen and cause issues further down the line – Tom J Nowell Jan 16 '14 at 11:32
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.

  • Thank you RRikesh, that sorted the first to errors, if I may ask, any ideas for the last mentioned problem. Bit of a leek when it comes to coding, lol. – Pieter Goosen Jan 16 '14 at 14:27
  • if( isset( $custom["_sidebar"][0] ) ){ and you do the same thing. – RRikesh Jan 16 '14 at 14:37
  • Great, I think I get this, sort of, lol. You've been a great help – Pieter Goosen Jan 16 '14 at 14:52
0

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);
  • You should not use if when you're using ternary operators. – RRikesh Jan 16 '14 at 12:01
  • Sorry I put it there by mistake, simply use (!empty($_POST["link"]))? $_POST["link"]:'default_value'; – Akash Arora Jan 16 '14 at 12:10
  • You can edit your answer and correct it directly there. – RRikesh Jan 16 '14 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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