zanematthew / zm-easy-cpt

A library used to ease the creation of Custom Taxonomies, Custom Post Types and Custom Meta Fields in WordPress.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Description

Organize and standarize the creation of Custom Post Types and Custom Taxonomies in WordPress.

Creates an interface, seperates concerns, add structure, yet be able to break out of it...

Features

  • Meta field support
  • Auto enqued front-end of CSS (per post_type)
  • Auto enqued front-end of JavaScript (per post_type)
  • Auto enqued admin CSS files (per post_type)
  • Auto enqued admin JavaScript files (per post_type)
  • Auto requiring of PHP files
  • Ajax form submission
  • Ajax Validation
  • Ajax security validation
  • Automatic file creation (CSS and JS)

Usage

  1. Download to wp-content/plugins/
  2. Activate the Plugin
  3. Start developing...

Requirements

  • WordPress (Latest)

Documentation

Including the library

We require the core plugin file and then run the function zm_easy_cpt_reqiure(), which will auto-require the needed files. 1.

php
<?php
/**
 * Auto load our events.php, events_controller.php, etc.
 * and enqueue our admin and front end asset files.
 */
require_once plugin_dir_path( __FILE__ ) . '../zm-easy-cpt/plugin.php';
if ( ! function_exists( 'zm_easy_cpt_reqiure' ) ) return;
zm_easy_cpt_reqiure( plugin_dir_path(__FILE__) );
?>
  • Optional We can add the following code below the WordPress plugin headers to ensure that zM Easy Custom Post Types is succesfully installed.
php
<?php
/**
 * Check if zM Easy Custom Post Types is installed. If it
 * is NOT installed we display an admin notice and return.
 */
if ( ! get_option('zm_easy_cpt_version' ) ){
    function zm_ev_admin_notice(){
        echo '<div class="updated"><p>This plugin requires <strong>zM Easy Custom Post Types</strong>.</p></div>';
    }
    add_action('admin_notices', 'zm_ev_admin_notice');
    return;
}
?>

Creating a Custom Post Type

We are going to seperate our post type into two files. The first file will include out arguments for our post_type and the second the post_type functions. Please refer to register_post_type() in the Codex for the list of arguments.

  1. Create a folder named post_types
  2. Create a file inside of post_types named NAME_OF_YOUR_POST_TYPE.php with the following:
php
<?php
$books = New Books();
$books->post_type = array(
    array(
        'name' => 'Books',
        'type' => 'books',
        'rewrite' => array(
            'slug' => 'books'
            ),
        'supports' => array(
            'title',
            'editor',
        )
    )
);
?>
  1. Advanced approach -- By using the code below we can derive everything from the name of our file
php
<?php

$file_name = basename( __FILE__, ".php" );

$my_cpt['name'] = ucfirst( $file_name );
$my_cpt['type'] = $file_name;
$my_cpt['slug'] = strtolower( $file_name );

$plugin = new $my_cpt['name']();
$plugin->post_type = array(
    array(
        'name' => $my_cpt['name'],
        'type' => $my_cpt['type'],
        'menu_name' => $my_cpt['name'],
        'rewrite' => array(
            'slug' => $my_cpt['slug']
            ),
        'supports' => array(
            'title',
            'editor'
        )
    )
);
?>
  1. Create a folder called functions
  2. Create a file named {my_post_type}_functions.php with the following:
<?php
Class Books extends zMCustomPostTypeBase {

    private $my_cpt;

    public function __construct(){
        /**
         * Run the parent construct method.
         *
         * Our parent construct has the init's for register_post_type
         * register_taxonomy and many other usefullness.
         */
        parent::__construct();
    }
}
?>
  1. (Bonus) If you want to include css and js files for each post_type and have them load ONLY on the admin post type pages then add the code below to your activation hook and re-activate the plugin.

Note you may have to change permissions in order for the files to be created.

do_action( 'zm_easy_cpt_create_assets', array('post_type_name_a','post_type_name_b'), plugin_dir_path(__FILE__) );

Creating Taxonoimes

In order to create a taxonomy we must create the post type first, see Creating Post Types. Once the post type is created we can pass in our arguments as an array. The argument is an array of arrays where each array is the taxonomy you want to create. Please refer to register_taxonomy() in the Codex for the list of arguments.

  1. Add the following into the file NAME_OF_YOUR_POST_TYPE.php below where we placed the post type.
php
<?php
$books->taxonomy = array(
    array(
        'name' => 'type'
        'post_type' => 'books',
        ),
    array(
        'name' => 'author',
        'post_type' => 'books',
        'menu_name' => 'Author',
        'slug' => 'books-author',
        'hierarchical' => false
        )
);
?>

Creating Meta Fields

In order to create a meta field we pass in our arguments into the variable $meta_sections['my_meta_field'].

  1. Add the following into the file NAME_OF_YOUR_POST_TYPE.php below where we placed the post type.
<?php
$books->meta_sections['date'] = array(
    'name' => 'date',
    'label' => __('Event Date'),
    'fields' => array(
        array(
            'type' => 'text',
            ),
        array(
            'label' => 'End Date',
            'type' => 'text',
            'class' => 'datetime-picker-end',
            'placeholder' => 'yyyy-mm-dd',
            'name' => 'form-field-name'
            )
    )
);
?>

Adding Functions

TBD

Themeing

TBD

Front-end Ajax Form Submisson

TBD

Known Issues

  • Capbilities are not fully supported

Where To Get Help

Sites Using zM Easy CPT

Contributing

  • Found a Bug? Please add it to the GitHub Issue tracker
  • Have a Enhancment request? Please add it to the GitHub Issue tracker
  • Want to write code? Fork/Pull request
  • Star it

Contribution Guidelines

  1. [S.O.L.I.D](http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)
  2. PHP code should be e-notice compliant
  3. WordPress CSS Standards
  4. WordPress Coding Standars
  5. I practice a "seperation of concerns" as much as possible, i.e. seperate your business logic from presentational logic.

Author

Zane Matthew

Inspiration, Alternatives

Copyright and license

Copyright 2013 Zane Matthew

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

About

A library used to ease the creation of Custom Taxonomies, Custom Post Types and Custom Meta Fields in WordPress.

License:GNU General Public License v2.0


Languages

Language:PHP 93.0%Language:JavaScript 7.0%