如何使用WP自定义帖子类型创建完整的插件

时间:2015-12-03 15:17:23

标签: wordpress wordpress-plugin

我想创建一个WP自定义帖子类型插件。我尝试过使用ACF插件并生成自定义字段:http://pastebin.com/Nz7f8ARw

我不知道如何使用上面生成的字段创建独立的插件而不使用ACF插件。

我尝试使用以下代码,但不知道如何继续进行。

 <?php 
/*Plugin Name: CNSLounge
Description: This plugin registers the 'Authors CNSLounge' post type.
Version: 1.0
License: GPLv2
*/


// Register Custom Post Type
function CNSLounge() {

    $labels = array(
        'name'                  => _x( 'Authorz', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Authorz', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Authorz', 'text_domain' ),
        'name_admin_bar'        => __( 'Authorz', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
        'all_items'             => __( 'All Items', 'text_domain' ),
        'add_new_item'          => __( 'Add New Authorz', 'text_domain' ),
        'add_new'               => __( 'Add Authorz', 'text_domain' ),
        'new_item'              => __( 'New Authorz', 'text_domain' ),
        'edit_item'             => __( 'Edit Authorz', 'text_domain' ),
        'update_item'           => __( 'Update Authorz', 'text_domain' ),
        'view_item'             => __( 'View Authorz', 'text_domain' ),
        'search_items'          => __( 'Search Authorz', 'text_domain' ),
        'not_found'             => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
        'items_list'            => __( 'Authorz list', 'text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'Authorz', 'text_domain' ),
        'description'           => __( 'CNS Authorz  Lounge', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-universal-access-alt',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => false,       
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'post',
    );
    register_post_type( 'post_type', $args );

}
add_action( 'init', 'CNSLounge', 0 );





?>

1 个答案:

答案 0 :(得分:1)

试试这个它会起作用你可以从这里生成代码http://jeremyhixon.com/tool/wordpress-meta-box-generator-v2-beta/

    function CNSLounge() {
        register_post_type('CNSLounge', array(
        'labels' => array(
        'name'                  => _x( 'Authorz', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Authorz', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Authorz', 'text_domain' ),
        'name_admin_bar'        => __( 'Authorz', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
        'all_items'             => __( 'All Authors', 'text_domain' ),
        'add_new_item'          => __( 'Add New Authorz', 'text_domain' ),
        'add_new'               => __( 'Add Authorz', 'text_domain' ),
        'new_item'              => __( 'New Authorz', 'text_domain' ),
        'edit_item'             => __( 'Edit Authorz', 'text_domain' ),
        'update_item'           => __( 'Update Authorz', 'text_domain' ),
        'view_item'             => __( 'View Authorz', 'text_domain' ),
        'search_items'          => __( 'Search Authorz', 'text_domain' ),
        'not_found'             => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
        'items_list'            => __( 'Authorz list', 'text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
             ),
            'public' => true,
            'menu_position' => 15,
            'supports' => array( 'revisions', 'page-attributes', 'post-formats', ),
            'taxonomies'  => array( 'category', 'post_tag' ),
            'menu_icon' => plugins_url('images/image.png', __FILE__),
            'rewrite'   => array( 'slug' => 'CNSLounge' ),
            'has_archive' => true,
            'show_in_admin_bar'     => true,
            'show_in_nav_menus'     => true,
            'can_export'            => true,
            'has_archive'           => false,       
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'capability_type'       => 'post',
            ));

            register_post_type( 'post_type', $args );

        }
        add_action( 'init', 'CNSLounge', 0 );


    class Rational_Meta_Box {
        private $screens = array(
            'CNSLounge',
        );
        private $fields = array(
            array(
                'id' => 'authorz-name',
                'label' => 'Authorz Name',
                'type' => 'text',
                'required' => 1,
                'placeholder' => 'Author Name',
                'maxlength' => 120, 
            ),
            array(
                'id' => 'author-photo',
                'label' => 'Author Photo',
                'type' => 'media',
                'required' => 0,
                'return_format' => 'array',
                'preview_size' => 'thumbnail',
                'library' => 'all',
                'min_width' => '',
                'min_height' => '',
                'min_size' => '',
                'max_width' => '',
                'max_height' => '',
                'max_size' => '',
                'mime_types' => '',
            ),
            array(
                'id' => 'active-since',
                'label' => 'Active Since',
                'type' => 'date',
                'required' => 1,
                'first_day' => 0,
            ),
            array(
                'id' => 'languages-expression',
                'label' => 'Languages Expression',
                'type' => 'text',
                'required' => 1,
                'placeholder' => 'Language(s) of Expression',
            ),
            array(
                'id' => 'now-based-at',
                'label' => 'Now Based at',
                'type' => 'text',
                'required' => 1,
                'placeholder' => 'Location of Author',
            ),
            array(
                'id' => 'location-of-author',
                'label' => 'Location of Author',
                'type' => 'text',
            ),
            array(
                'id' => 'mostly-writes',
                'label' => 'Mostly Writes',
                'type' => 'text',
                'required' => 1,
                'placeholder' => 'For E.g. Poems and Articles',
            ),
            array(
                'id' => 'about-author',
                'label' => 'About Author',
                'type' => 'wysiwyg',
                'required' => 1,
                'tabs' => 'all',
                'toolbar' => 'basic',
                'media_upload' => 0,
            ),
            array(
                'id' => 'magazines',
                'label' => 'Magazines',
                'type' => 'gallery',
                'min' => '',
                'max' => '',
                'preview_size' => 'thumbnail',
                'library' => 'all',
                'min_width' => '',
                'min_height' => '',
                'min_size' => '',
                'max_width' => '',
                'max_height' => '',
                'max_size' => '',
                'mime_types' => '',
            ),
            array(
                'id' => 'publication',
                'label' => 'Publication',
                'type' => 'gallery',
                'min' => '',
                'max' => '',
                'preview_size' => 'thumbnail',
                'library' => 'all',
                'min_width' => '',
                'min_height' => '',
                'min_size' => '',
                'max_width' => '',
                'max_height' => '',
                'max_size' => '',
                'mime_types' => '',
            ),
            array(
                'id' => 'gallery',
                'label' => 'Gallery',
                'type' => 'gallery',
                'min' => '',
                'max' => '',
                'preview_size' => 'thumbnail',
                'library' => 'all',
                'min_width' => '',
                'min_height' => '',
                'min_size' => '',
                'max_width' => '',
                'max_height' => '',
                'max_size' => '',
                'mime_types' => '',
            ),
            array(
                'id' => 'author-s-website',
                'label' => 'Author\'s Website',
                'type' => 'url',
                'placeholder' => 'http://',
            ),
            array(
                'id' => 'author-s-email',
                'label' => 'Author\'s Email',
                'type' => 'email',
            ),
            array(
                'id' => 'author-s-phone',
                'label' => 'Author\'s Phone',
                'type' => 'number',
                'placeholder' => 'For E.g +91-9090404040',
            ),
        );

    /**
     * Class construct method. Adds actions to their respective WordPress hooks.
     */
    public function __construct() {

        add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
        add_action( 'admin_footer', array( $this, 'admin_footer' ) );
        add_action( 'save_post', array( $this, 'save_post' ) );
    }

    /**
     * Hooks into WordPress' add_meta_boxes function.
     * Goes through screens (post types) and adds the meta box.
     */
    public function add_meta_boxes() {
        foreach ( $this->screens as $screen ) {
            add_meta_box(
                'cnslounge',
                __( 'CNSLounge', 'rational-metabox' ),
                array( $this, 'add_meta_box_callback' ),
                $screen,
                'advanced',
                'default'
            );
        }
    }

    /**
     * Generates the HTML for the meta box
     * 
     * @param object $post WordPress post object
     */
    public function add_meta_box_callback( $post ) {
        wp_nonce_field( 'cnslounge_data', 'cnslounge_nonce' );
        echo 'CNSLounge Author Meta Information';
        $this->generate_fields( $post );
    }

    /**
     * Hooks into WordPress' admin_footer function.
     * Adds scripts for media uploader.
     */
    public function admin_footer() {
        ?><script>
            // https://codestag.com/how-to-use-wordpress-3-5-media-uploader-in-theme-options/
            jQuery(document).ready(function($){
                if ( typeof wp.media !== 'undefined' ) {
                    var _custom_media = true,
                    _orig_send_attachment = wp.media.editor.send.attachment;
                    $('.rational-metabox-media').click(function(e) {
                        var send_attachment_bkp = wp.media.editor.send.attachment;
                        var button = $(this);
                        var id = button.attr('id').replace('_button', '');
                        _custom_media = true;
                            wp.media.editor.send.attachment = function(props, attachment){
                            if ( _custom_media ) {
                                $("#"+id).val(attachment.url);
                            } else {
                                return _orig_send_attachment.apply( this, [props, attachment] );
                            };
                        }
                        wp.media.editor.open(button);
                        return false;
                    });
                    $('.add_media').on('click', function(){
                        _custom_media = false;
                    });
                }
            });
        </script><?php
    }

    /**
     * Generates the field's HTML for the meta box.
     */
    public function generate_fields( $post ) {
        $output = '';
        foreach ( $this->fields as $field ) {
            $label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
            $db_value = get_post_meta( $post->ID, 'advanced_options_' . $field['id'], true );
            switch ( $field['type'] ) {
                case 'media':
                    $input = sprintf(
                        '<input class="regular-text" id="%s" name="%s" type="text" value="%s"> <input class="button rational-metabox-media" id="%s_button" name="%s_button" type="button" value="Upload" />',
                        $field['id'],
                        $field['id'],
                        $db_value,
                        $field['id'],
                        $field['id']
                    );
                    break;
                case 'textarea':
                    $input = sprintf(
                        '<textarea class="large-text" id="%s" name="%s" rows="5">%s</textarea>',
                        $field['id'],
                        $field['id'],
                        $db_value
                    );
                    break;
                default:
                    $input = sprintf(
                        '<input %s id="%s" name="%s" type="%s" value="%s">',
                        $field['type'] !== 'color' ? 'class="regular-text"' : '',
                        $field['id'],
                        $field['id'],
                        $field['type'],
                        $db_value
                    );
            }
            $output .= $this->row_format( $label, $input );
        }
        echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
    }

    /**
     * Generates the HTML for table rows.
     */
    public function row_format( $label, $input ) {
        return sprintf(
            '<tr><th scope="row">%s</th><td>%s</td></tr>',
            $label,
            $input
        );
    }
    /**
     * Hooks into WordPress' save_post function
     */
    public function save_post( $post_id ) {
        if ( ! isset( $_POST['cnslounge_nonce'] ) )
            return $post_id;

        $nonce = $_POST['cnslounge_nonce'];
        if ( !wp_verify_nonce( $nonce, 'cnslounge_data' ) )
            return $post_id;

        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return $post_id;

        foreach ( $this->fields as $field ) {
            if ( isset( $_POST[ $field['id'] ] ) ) {
                switch ( $field['type'] ) {
                    case 'email':
                        $_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
                        break;
                    case 'text':
                        $_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
                        break;
                }
                update_post_meta( $post_id, 'cnslounge_' . $field['id'], $_POST[ $field['id'] ] );
            } else if ( $field['type'] === 'checkbox' ) {
                update_post_meta( $post_id, 'cnslounge_' . $field['id'], '0' );
            }
        }
    }
}
new Rational_Meta_Box;