wordpress custom post types – broken publish button

I created a gallery post type (as part of a plugin) that contains besides a title and some meta information also a wp_list_table that queries those attachments which have the current post as post_parent. I ran into a problem when suddenly my publish button stopped working. No matter if I’m creating a new gallery or if I’m editing an old one, once I click on update/publish my changes get lost and I end up on edit.php.
Anybody knows what that’s all about?

I where able to figure out where the problem seems to be. It’s in my list_table class which inherits from wp_list_table. after commenting out some unimportant functions i ended up with a different error but still no new or updated gallery. Now I get the are you sure you want to do this page.

Read More

Even the most basic class won’t work…

if( ! class_exists( 'WP_List_Table' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}

class Yard_Attachment_Table extends WP_List_Table {
function __construct() {
    global $status, $page;      
    parent::__construct( array(
        'singular'  => 'yard Attachment',
        'plural'    => 'yard Attachments',
        'ajax'      => false
    ) );

}

function column_default($item, $column_name) {
    return 'default';
}
function get_columns(){
    $columns = array(
        'checkbox'  => '<input type="checkbox" />', //for simplicity its not 'cb'
        'thumb'     => 'Thumbnail',
        'title'     => 'Titel',
        'pos'       => 'Position'
    );
    return $columns;
}
function prepare_items() {
    global $wpdb;
    $columns = $this->get_columns();
    $hidden = array();
    $sortable = $this->get_sortable_columns();

    $this->_column_headers = array($columns, $hidden, $sortable);

    if (isset($_REQUEST['post'])) {
        $query = "  SELECT * 
                    FROM $wpdb->posts 
                    WHERE post_type = 'attachment' 
                    AND post_parent = {$_REQUEST['post']}";
        $data = $wpdb->get_results($query, ARRAY_A);
    } else {
        $data = array();
    }

    $this->items = $data;

}
}

In the plugin class’ constructor I use
add_action('add_meta_boxes_yard_gallery', array($this, 'yard_metaboxes'));.
In yard_metaboxes I use add_meta_box and in the function I have as a callback i’m creating a new instance of my table class and I call prepare_items() and display()

Turning error_reporting on my page dies with these messages:

Strict Standards: Only variables should be passed by reference in /Applications/MAMP/htdocs/Web/ChristophRokitta/wp v.1.0/wp-includes/pomo/mo.php on line 210

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/Web/ChristophRokitta/wp v.1.0/wp-includes/pomo/mo.php:210) in /Applications/MAMP/htdocs/Web/ChristophRokitta/wp v.1.0/wp-includes/pluggable.php on line 876

BTW I’m not localizing.
Please help! If i had more reputation I’d offer it.

Adding the meta box code

in my plugin file:

require_once( plugin_dir_path( __FILE__ ) . 'class-yard.php' );
Yard::get_instance();

in my class-yard file the meta box methods are at the bottom:

class Yard {
    protected static $instance = null;
    private function __construct() {
        include_once('class-yard-attachments.php');

        add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_styles' ) );

        add_action('after_setup_theme', array($this, 'yard_thumbnails'));

        add_action('init', array($this, 'yard_post_type'));
        add_action('init', array($this, 'yard_taxonomies'));
        add_filter('manage_yard_gallery_posts_columns', array($this, 'yard_add_columns'));
        add_action('manage_posts_custom_column', array($this, 'yard_fill_columns'));
        add_action('add_meta_boxes_yard_gallery', array($this, 'yard_metaboxes'));
    }

    public static function get_instance() {// If the single instance hasn't been set, set it now.
        if (null == self::$instance ) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function enqueue_admin_styles() {
        $screen = get_current_screen();
        if ($screen->post_type == 'yard_gallery') {
            wp_register_style( 'yard-gallery-style', plugins_url('css/yard-gallery-style.css', __FILE__) );
            wp_enqueue_style( 'yard-gallery-style' );
        }
    }

    public function yard_thumbnails() {
        //add_image_size('yard-thumbnail', 100, 100, true);
    }

    public function yard_post_type() {
        $gallery_labels = array(
            'name' => 'Galerien',
            'singular_name' => 'Galerie',
            'all_items' => 'Alle Galerien',
            'add_new' => 'Erstellen',
            'add_new_item' => 'Neue Galerie erstellen',
            'edit_item' => 'Galerie bearbeiten',
            'new_item' => 'Neue Galerie',
            'view' => 'Galerie anzeigen',
            'view_item' => 'Gallerie anzeigen',
            'search_items' => 'Galerie durchsuchen',
            'not_found' => 'Keine Galerien gefunden',
            'not_found_in_trash' => 'Es befinden sich keine Galerien im Papierkorb',
            'parent_item_colon' => ''
        );
        $gallery_args = array(
            'labels' => $gallery_labels,
            'public' => true,
            // 'publicly_queryable' => true,
            // 'show_ui' => true,
            // 'show_in_menu' => true,
            // 'query_var' => true,
            'rewrite' => true,
            // 'capability_type' => 'post',
            // 'hierarchical' => false,
            'menu_position' => 12,
            'supports' => array(
                'title'
            )
            // 'menu_icon' => plugin_dir_url(__FILE__) . '/assets/icon_16_grey.png'//16x16 png if you want an icon
        );
        register_post_type('yard_gallery', $gallery_args);
    }

    public function yard_taxonomies() {
        register_taxonomy(
            'yard_work_type',
            'yard_gallery',
            array(
                'hierarchical' => true,
                'label' => 'Art der Arbeit'
            )
        );
        register_taxonomy(
            'yard_subject',
            'yard_gallery',
            array(
                'hierarchical' => true,
                'label' => 'Motiv'
            )
        );
    }

    public function yard_add_columns( $columns ){
        $columns = array(
            'cb' => '<input type="checkbox">',
            'yard_post_thumb' => 'Thumbnail',
            'title' => 'Bezeichnung',
            'yard_pos' => 'Position',
            'date' => 'Datum'
        );
        return $columns;
    }
    public function yard_fill_columns( $column ) {
        global $post;
        switch ($column) {
            case 'yard_post_thumb' :
                echo the_post_thumbnail('admin-list-thumb');
                break;
        }
    }

    public function yard_metaboxes( $post ) {
        global $wp_meta_boxes;

        add_meta_box(
            'yard-attachments',
            'Bilder',
            array($this, 'get_yard_attachment_table'),
            'yard_gallery'
        );
    }
    public function get_yard_attachment_table() {
        $yard_list_table = new Yard_Attachment_Table();
        $yard_list_table->prepare_items();

        $yard_list_table->display();
    }
}

Related posts

Leave a Reply

1 comment

  1. Strict Standards: Only variables should be passed by reference in /Applications/MAMP/htdocs/Web/ChristophRokitta/wp v.1.0/wp-includes/pomo/mo.php on line 210

    The error message tells it all. The “…/popo/mo.php” file has to do with (is related to) the WordPress translation. (I bet you’re using WordPress with German language files).

    I can’t see what could be wrong in the code you’ve posted here, but something is interfering with translation. Looking at what the error message tells us, some variable that WordPress tries to translate fails to be translated since it’s not the correct type.

    Warning: Cannot modify header information – headers already sent by (output started at /Applications/MAMP/htdocs/Web/ChristophRokitta/wp v.1.0/wp-includes/pomo/mo.php:210) in /Applications/MAMP/htdocs/Web/ChristophRokitta/wp v.1.0/wp-includes/pluggable.php on line 876

    This is a logic result of the previous error message and the headers WordPress tries to send to the browser.

    What’s happening: the first error message is being pushed to the client and flushed to the browser screen. Next, WordPress tries to send it’s usual headers and produces an error while doing so because headers have to be send before ANY content is being send to the client.

    In other words: the “Cannot modify header information” error always comes up when you echo something to screen and then try to send “header(…)” information. In this case, the translation problem produces the first error message and then WordPress tries to send headers, which fails and produces the second error message.

    TIPS

    • Check everything you’re doing which is related to translation (read: wherever you are passing “German” and/or “English” language strings)

    and even more important

    • Make sure that you’re actually passing the correct type(s)… looking at the error and your code, it could well be you’re passing a class, a class reference, or another object somewhere instead of the expected variable.