3

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.

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
  • Where and when are you getting that error? I can not reproduce it with the code you posted. Please, provide some more details about the problem.
    – cybmeta
    Commented Jun 8, 2015 at 9:42
  • This only appears on the top of the screen when I am adding a new post only. Commented Jun 8, 2015 at 9:47

2 Answers 2

8

That error is because you are not providing the post ID to check capabilities:

This line:

if( !current_user_can( 'edit_post' ) ) {

Should be:

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

If @cybmeta 's answer does not work for you (you don't have any particular post ID and check if user can edit posts in general) => you probably just forgot "s" at the end :)

Code quote from docs + some comments of mine:

current_user_can( 'edit_posts' ); // SEE THE EXTRA "s" HERE ? :)
current_user_can( 'edit_post', $post->ID ); // check for specific post (NO "s"!)
current_user_can( 'edit_post_meta', $post->ID, $meta_key );

DOCS: current_user_can( string $capability, mixed ...$args )

  • also there is a DOC-typo in the whole docs with ...$args being documented as just $args and their are practicaly not documented besides the code quote above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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