2015-06-08 31 views
1

我已經看過以前的這個問題的例子,但我找不到解決方案是什麼導致此基於我從其他人看到的內容。這僅在我添加新帖子時出現。WordPress的:我如何解決注意:未定義的偏移量:0行1145行/wp-includes/capabilities.php

1145:

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

我沒有得到任何其他類型的錯誤,所以我不知道在我的代碼,這是造成問題的原因。

對此有何幫助?

這是代碼:

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

回答

1

這條線:

if(!current_user_can('edit_post')) { 

應該是:

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

我發現這裏https://wordpress.org/support/topic/patch-notice-undefined-offset-0-in-capabilitiesphp-on-line-1102 解決方案可能看似不同的問題,但我可以解決問題的變化:

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

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

在其他寫景 如果你有一個自定義分類,你必須確保在 '能力' ARGS 'assign_term' 能力是關係到 'edit_posts'

'assign_terms' => 'edit_posts', 

'assign_terms' => 'edit_mycustomposttypes', 

(注意 's' 在端)

例如:

$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);