Skip to main content
added 67 characters in body
Source Link

I've looked at previous examples of this problem but I can't find a solution for what is causing this notice based on what I've seen from other people. This appears at the top of the screen when I am adding a new post.

I've looked at previous examples of this problem but I can't find a solution for what is causing this notice based on what I've seen from other people.

I've looked at previous examples of this problem but I can't find a solution for what is causing this notice based on what I've seen from other people. This appears at the top of the screen when I am adding a new post.

Source Link

How do I resolve Notice: Undefined offset: 0 in /wp-includes/capabilities.php on line 1145

I've looked at previous examples of this problem but I can't find a solution for what is causing this notice based on what I've seen from other people.

1145 is:

$post = get_post( $args[0] );

I'm not getting any other kind of error so I'm not sure where in my code this is causing the problem.

Any help on this?

This is the code:

//show metabox in post editing page
add_action('add_meta_boxes', 'kk_add_metabox' );

//save metabox data
add_action('save_post', 'kk_save_metabox' );

//register widgets
add_action('widgets_init', 'kk_widget_init');

function kk_add_metabox() {
    add_meta_box('kk_youtube', 'YouTube Video Link','kk_youtube_handler', 'post');
}

/**
 * metabox handler
 */
function kk_youtube_handler($post)
{
    $youtube_link = esc_attr( get_post_meta( $post->ID, 'kk_youtube', true ) );

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

/**
 * save metadata
 */
function kk_save_metabox($post_id) {

if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
}

//check if user can edit post
if( !current_user_can( 'edit_post' ) ) {
    return;
}

    if( isset($_POST['kk_youtube'] )) {
        update_post_meta($post_id, 'kk_youtube', esc_url($_POST['kk_youtube']));
    }
}

/**
 * register widget
 */
function kk_widget_init() {
    register_widget('KK_Widget');
}

/**
 * Class KK_Widget widget class
 */
class KK_Widget extends WP_Widget
{
function __construct()
{
    $widget_options = array(
        'classname' => 'kk_class', // For CSS class name
        'description' => 'Show a YouTube video from post metadata'
    );

    $this->WP_Widget('kk_id', 'YouTube Video', $widget_options);
}

/**
 * Show widget form in Appearance/Widgets
 */
function form($instance)
{
    $defaults = array(
        'title' => 'YouTube Video'
    );

    $instance = wp_parse_args((array)$instance, $defaults);
    var_dump($instance);
    $title = esc_attr($instance['title']);

    echo '<p>Title <input type="text" class="widefat" name="' . $this->get_field_name('title') . '" value="' . $title . '" /></p>';
}

function update($new_instance, $old_instance)
{
    $instance = $old_instance;

    $instance['title'] = strip_tags($new_instance['title']);

    return $instance;
}

/**
 * Show widget in post/page
 */
function widget($args, $instance)
{
    global $before_widget;
    global $after_widget;
    global $before_title;
    global $after_title;

    extract( $args );
    $title = apply_filters('widget_title', $instance['title']);

    //show only if single post
    if(is_single()) {
        echo $before_widget;
        echo $before_title.$title.$after_title;

        //get post metadata
        $kk_youtube = esc_url(get_post_meta(get_the_ID(), 'kk_youtube', true));

        //print widget content
        echo '<iframe width="200" height="200" frameborder="0" allowfullscreen src="http://www.youtube.com/embed/' . $this->get_yt_videoid($kk_youtube) . '"></iframe>';

        echo $after_widget;
    }
}

function get_yt_videoid($url)
{
    parse_str(parse_url($url, PHP_URL_QUERY), $my_array_of_vars);

    return $my_array_of_vars['v'];
}
}