0

Still trying to figure my way around Wordpress plugin development, how can I resolve this undefined error? This appears when I add a new post, it can't find the post so I'm trying to figure out how to resolve it with an isset.

Notice: Undefined index: kk_youtube in /var/www/html/wordpress/wp-content/plugins/video_widget/video_widget.php on line 33

The error is on this line 33:

$youtube_link = esc_attr($value['kk_youtube'][0]);

I've been trying to wrap it in isset($post) but it's still not working.

This is the entire function call.

function kk_youtube_handler($post) {
    $value = get_post_custom($post->ID);

    $youtube_link = esc_attr($value['kk_youtube'][0]);

    echo '<label for="kk_youtube">YouTube Video Link</label>
          <input type="text" id="kk_youtube" name="kk_youtube" value="' . 
          $youtube_link . '" />';
}

Can I get some help on this?

0

The error is that you're trying to read $value['kk_youtube'] when there is no kk_youtube key in the array (undefined index). Rather than use isset, use get_post_meta with the third argument "single" true:

$youtube_link = esc_attr( get_post_meta( $post->ID, 'kk_youtube', true ) );

This is effectively the same as doing:

$value = get_post_custom( $post->ID );
if ( isset( $value['kk_youtube'][0] ) )
     $youtube_link = esc_attr( $value['kk_youtube'][0] );
else
     $youtube_link = '';
1
  • Ah, thank you sir... I was using the wrong parameter for isset. Thanks for explaining and providing an alternative solution. Jun 8 '15 at 9:24

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.