WordPress Cuztom Helper

Post on 08-Jul-2015

256 views 0 download

description

A helper to make the life of Wordpress developers easier. This helper can be used to quickly register Custom Post Types, Taxonomies, Meta Boxes, Menu Pages and Sidebars within your Wordpress projects. Please comment, review, watch, fork and report bugs.

Transcript of WordPress Cuztom Helper

WORDPRESS CUZTOM HELPER- A helper to make the life of Wordpress developers easier.

_ Ante Primorac (@anteprimorac)

_ Web Developer @ Slicejack

_ 2nd WordPress Meetup Split

WHAT DO WE USE CUZTOM FOR?

_ Post types _ Taxonomies _ Meta Boxes

_ Post meta _ Term meta _ User meta

HOW TO USE CUZTOM?

slicejack:inc ante$ git clone https://github.com/gizburdt/wp-cuztom.git cuztom

1 CLONE REPOSITORY TO INC/CUZTOM/

<?php

define( 'CUZTOM_TEXTDOMAIN', 'slicejack' ); define( 'CUZTOM_URL', get_template_directory_uri() . '/inc/cuztom' );

require( 'cuztom/cuztom.php' );

2 CREATE CUZTOM.PHP AND LET THE MAGIC BEGIN

BOOKS LIBRARY

$books = new Cuztom_Post_Type( 'book', array( 'has_archive' => true, 'rewrite' => array( 'slug' => 'books' ), 'supports' => array( 'title', 'editor', 'thumbnail', 'revisions' ) ) );

1 CREATE CUSTOM POST TYPE - BOOK

BOOKS LIBRARY

$book_category = new Cuztom_Taxonomy( 'Book Category', 'book' );

2 CREATE CUSTOM TAXONOMY - BOOK CATEGORY

$books->add_meta_box( 'info', 'Info', array( array( 'name' => 'author', 'label' => 'Author', 'type' => 'text' ), array( 'name' => 'date', 'label' => 'Date', 'type' => 'date' ), array( 'name' => 'price', 'label' => 'Price', 'type' => 'text' ), array( 'name' => 'number_of_pages', 'label' => 'Number of pages', 'type' => 'text' ),

BOOKS LIBRARY

3 CREATE METABOX - INFO

array( 'name' => 'bind_type', 'label' => 'Bind type', 'type' => 'select', 'options' => array( 'hardcover' => 'Hardcover', 'double-wire' => 'Double wire', 'spiral' => 'Spiral' ), 'default_value' => 'hardcover' ), array( 'name' => 'size', 'label' => 'Size', 'type' => 'select', 'options' => array( 'a5' => 'A5', 'a4' => 'A4', 'a3' => 'A3' ), 'default_value' => 'a4' ) ));

FIELD TYPES

_ text - a simple text field _ textarea - a large textarea for multiple lines of text _ checkbox - a simple boolean entry _ yesno - allows choice of yes/no (pre-made extension of radio) _ select - combo box selection _ multi_select - multi select selection _ checkboxes - a checkbox group _ radios - a radio group _ wysiwyg - an editor with the same controls as the standard WordPress editor _ image - image upload box _ file - file upload box _ date - date selection _ datetime - date and time (hour+minutes) selection _ time - time (hour+minutes) selection _ color - color selection _ post_select - allows inner-post references via a combo-box _ post_checkboxes - allows inner-post references via a checkbox group _ term_select - allows category references via a combo-box _ term_checkboxes - allows category references via a checkbox group _ hidden - a hidden field (osten used to store information not visible to the user).

BOOKS LIBRARY

<div class="entry-content"> <?php $bind_types = array( 'hardcover' => __( 'Hardcover', 'slicejack' ), 'double-wire' => __( 'Double wire', 'slicejack' ), 'spiral' => __( 'Spiral', 'slicejack' ) ); $sizes = array( 'a5' => __( 'A5', 'slicejack' ), 'a4' => __( 'A4', 'slicejack' ), 'a3' => __( 'A3', 'slicejack' ) ); ?> <strong><?php _e( 'Author:', 'slicejack' ); ?></strong> <?php echo get_post_meta( get_the_id(), '_info_author', true ); ?><br /> <strong><?php _e( 'Date:', 'slicejack' ); ?></strong> <?php echo date( 'd.m.Y.', get_post_meta( get_the_id(), '_info_date', true ) ); ?><br /> <strong><?php _e( 'Price:', 'slicejack' ); ?></strong> $<?php echo get_post_meta( get_the_id(), '_info_price', true ); ?><br /> <strong><?php _e( 'Number of pages:', 'slicejack' ); ?></strong> <?php echo get_post_meta( get_the_id(), '_info_number_of_pages', true ); ?><br /> <strong><?php _e( 'Bind type:', 'slicejack' ); ?></strong> <?php echo $bind_types[ get_post_meta( get_the_id(), '_info_bind_type', true ) ]; ?><br /> <strong><?php _e( 'Size:', 'slicejack' ); ?></strong> <?php echo $sizes[ get_post_meta( get_the_id(), '_info_size', true ) ]; ?> </div><!-- .entry-content -->

4 TEMPLATE

if ( is_admin() ) : $pid = 0; if( array_key_exists( 'post', $_GET ) ) $pid = $_GET['post']; elseif ( array_key_exists( 'post_ID', $_POST ) ) $pid = $_POST['post_ID'];

$page_template = basename( get_post_meta( $pid, '_wp_page_template', true ) );

if ( 'featured-library.php' == $page_template ) : $cpage = new Cuztom_Post_Type( 'page' ); $cpage ->remove_post_type_support( array( 'editor', 'thumbnail' ) ) ->add_meta_box( 'library', 'Library', array( 'bundle', array( array( 'name' => 'book', 'label' => 'Book', 'type' => 'post_select', 'args' => array( 'post_type' => 'book', 'show_option_none' => 'None' ) )

FEATURED LIBRARY

1 ADD METABOX TO PAGE TEMPLATE

FEATURED LIBRARY

<?php // Start the Loop. while ( have_posts() ) : the_post(); ?> <?php the_title( '<header class="entry-header"><h1 class="entry-title">', '</h1></header><!-- .entry-header -->' ); ?> <?php

$library = get_post_meta( get_the_id(), '_library', true );

foreach( $library as $book ) : $post = get_post( $book['_book'] ); setup_postdata( $post ); get_template_part( 'book' ); endforeach;

wp_reset_postdata();

endwhile; ?>

2 PAGE TEMPLATE

<?php if( ! defined( 'ABSPATH' ) ) exit;

class Cuztom_Field_Ip extends Cuztom_Field { var $_supports_repeatable = true; var $_supports_bundle = true; var $_supports_ajax = true;

var $css_classes = array( 'cuztom-input', 'cuztom-ip' );

function _output( $value ) { $output = '';

$output .= '<input type="text" ' . $this->output_name() . ' ' . $this->output_id() . ' ' . $this->output_css_class() . ' value="' . ( ! empty( $value ) ? $value : '0.0.0.0' ) . '" />';

$output .= $this->output_explanation();

return $output; }

CUSTOM FIELD TYPE

1 CREATE YOURFIELD.CLASS.PHP AND SAVE IT TO INC/CUZTOM/CLASSES/FIELDS

function save_value( $value ) { return filter_var( $value, FILTER_VALIDATE_IP ) ? $value : '0.0.0.0'; } }

CUSTOM FIELD TYPE

1 CREATE YOURFIELD.CLASS.PHP AND SAVE IT TO INC/CUZTOM/CLASSES/FIELDS

CUSTOM FIELD TYPE

private function includes() { /*...*/ include( CUZTOM_DIR . 'classes/fields/ip.class.php' ); }

2 INCLUDE FIELD TYPE CLASS

CONTRIBUTE ON GITHUB

https://github.com/gizburdt/wp-cuztom

THANK YOU!

_ Ante Primorac (@anteprimorac)

_ Web Developer @ Slicejack

_ 2nd WordPress Meetup Split