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

4.8k views Asked by At

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 when I am adding a new post only.

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'];
}
}
2

There are 2 answers

0
etruel On

I found the solution here https://wordpress.org/support/topic/patch-notice-undefined-offset-0-in-capabilitiesphp-on-line-1102 May seem different issue but I could fix the issue changing:

if ( ! current_user_can( 'edit_post' ) ) return;

to

if ( ! current_user_can( 'edit_posts' ) ) return;

In other scenary if you have a custom taxonomy, you must be sure that 'assign_term' capability on 'capabilities' args is related to 'edit_posts'

'assign_terms' => 'edit_posts',

or

'assign_terms' => 'edit_mycustomposttypes',

(note the 's' at end)

example:

    $capabilities = array(
        'manage_terms' => 'manage_mytaxs',
        'edit_terms' => 'manage_mytaxs',
        'delete_terms' => 'manage_mytaxs',
        **'assign_terms' => 'edit_mycustomposttypes',**
    );
    $args = array(
        'show_ui'           => true,
        'show_admin_column' => true,
        **'capabilities'      => $capabilities,**
    );
    register_taxonomy( 'mytax', 'mycustomposttype', $args );
0
esteemed.squire On

This line:

if( !current_user_can( 'edit_post' ) ) {

Should be:

if( !current_user_can( 'edit_post', $post_id ) ) {