lundi 2 mai 2016

Custom post type custom columns not working after reactivation

i am currently learning how to develop wordpress plugin, i hav watch a tutorial and followed all necessary steps to come up with a working custom post type having custom columns, suddenly when things got long, my new changes doesnt apply, i deactivate and reactivate the plugin i hav just created but still nothing works. It added the custom post but not the custom columns and meta box. Can any one help me with this, im just a newbie who seeks answer, i know this is easy for experts, i dont need people who will just vote down this question. Thanks a lot.

this is the plugin code:

<?php
/**
*Plugin Name: Article
*Plugin URI:  #
*Description: Lorem Ipsum
*Version:     1
*Author:      Neil
*Author URI:  #
*License:     GPL2
*License URI: http://ift.tt/XUg0iV
*
*/


//Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
function neil_article_register_post_type() {

    $singular = 'Article';
    $plural = 'Articles';
    $slug = str_replace( ' ', '_', strtolower( $singular ) );
    $labels = array(
        'name'                  => $plural,
        'singular_name'         => $singular,
        'add_new'               => 'Add New',
        'add_new_item'          => 'Add New ' . $singular,
        'edit'                  => 'Edit',
        'edit_item'             => 'Edit ' . $singular,
        'new_item'              => 'New ' . $singular,
        'view'                  => 'View ' . $singular,
        'view_item'             => 'View ' . $singular,
        'search_term'           => 'Search ' . $plural,
        'parent'                => 'Parent ' . $singular,
        'not_found'             => 'No ' . $plural .' found',
        'not_found_in_trash'    => 'No ' . $plural .' in Trash'
        );
    $args = array(
        'labels'              => $labels,
            'public'              => true,
            'publicly_queryable'  => true,
            'exclude_from_search' => false,
            'show_in_nav_menus'   => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 10,
            'menu_icon'           => 'dashicons-media-document',
            'can_export'          => true,
            'delete_with_user'    => false,
            'hierarchical'        => false,
            'has_archive'         => true,
            'query_var'           => true,
            'capability_type'     => 'post',
            'map_meta_cap'        => true,
            // 'capabilities' => array(),
            'rewrite'             => array( 
                'slug' => $slug,
                'with_front' => true,
                'pages' => true,
                'feeds' => true,
            ),
            'supports'            => array( 
                'title', 
                'editor', 
                'author', 
                'custom-fields',
                'comments' 
            )
    );
    register_post_type( $slug, $args );
}

function neil_set_article_columns( $columns ){
    $NewColumns = array();
    $NewColumns['title'] = 'Article Title';
    $NewColumns['content'] = 'Article Content';
    $NewColumns['url'] = 'Permalink';
    $NewColumns['qr'] = 'QR code';
    $NewColumns['area'] = 'Area';
    $NewColumns['date'] = 'Date Published';

    return $NewColumns;

}

function neil_article_custom_column( $column, $post_id ){
    switch ($column) {
        case 'content':
            echo get_the_excerpt();
            break;
        case 'qr':
            echo 'QR PIC HERE';
            break;
        case 'url':
            echo '<a href="get_the_permalink()">'.get_the_permalink().'</a>';
            break;
        case 'area':
            echo 'accreditation area of the article';
            break;      
    }
}

/* LINK META BOXES */

function neil_article_add_meta_box(){
    add_meta_box( 'article_link', 'Article Permalink', 'neil_article_link_callback', 'neil-article','side' );
}

function neil_article_link_callback( $post ){
    wp_nonce_field( 'neil_save_article_link_data', 'neil_article_link_meta_box_nonce' );

    $value = get_post_meta( $post->ID,'_article_link_value_key',true );

    echo '<label for="neil_article_link_field">The link: </label>';
    echo '<input type="text" id="neil_article_link_field" name="neil_article_link_field" value="' .esc_attr( $value ) . '" size="100" />';
}

function neil_save_article_link_data( $post_id ){

    if( ! isset( $_POST['neil_article_link_meta_box_nonce'] ) ){
        return;
    }

    if ( ! wp_verify_nonce( $_POST['neil_article_link_meta_box_nonce'],'neil_save_article_link_data' ) ) {
        return;
    }

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

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    if (! isset( $_POST['neil_article_link_field'])) {
        return;
    }

    $mydata = sanitize_text_field( $_POST['neil_article_link_field'] );
    update_post_meta( $post_id, '_article_link_value_key', $mydata );
}

add_action( 'init', 'neil_article_register_post_type' );

function neil_article_install() {

    // Trigger our function that registers the custom post type
    neil_article_register_post_type();

    // Clear the permalinks after the post type has been registered
    flush_rewrite_rules();

}
register_activation_hook( __FILE__, 'neil_article_install' );

add_filter( 'manage_neil-article_posts_columns','neil_set_article_columns' );
add_action( 'manage_neil-article_posts_custom_column','neil_article_custom_column',10,2 );

add_action( 'add_meta_boxes','neil_article_add_meta_box' );
add_action( 'save_post','neil_save_article_link_data' );

function neil_article_deactivation() {

    // Our post type will be automatically removed, so no need to unregister it

    // Clear the permalinks to remove our post type's rules
    flush_rewrite_rules();

}
register_deactivation_hook( __FILE__, 'neil_article_deactivation' );



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire