Download - WordPress Cuztom Helper

Transcript
Page 1: 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

Page 2: WordPress Cuztom Helper

WHAT DO WE USE CUZTOM FOR?

_ Post types _ Taxonomies _ Meta Boxes

_ Post meta _ Term meta _ User meta

Page 3: WordPress Cuztom Helper

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

Page 4: WordPress Cuztom Helper

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

Page 5: WordPress Cuztom Helper
Page 6: WordPress Cuztom Helper

BOOKS LIBRARY

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

2 CREATE CUSTOM TAXONOMY - BOOK CATEGORY

Page 7: WordPress Cuztom Helper
Page 8: WordPress Cuztom Helper

$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' ) ));

Page 9: WordPress Cuztom Helper

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).

Page 10: WordPress Cuztom Helper
Page 11: WordPress Cuztom Helper

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

Page 12: WordPress Cuztom Helper
Page 13: WordPress Cuztom Helper

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

Page 14: WordPress Cuztom Helper

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

Page 15: WordPress Cuztom Helper

<?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

Page 16: WordPress Cuztom Helper

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

Page 17: WordPress Cuztom Helper

CUSTOM FIELD TYPE

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

2 INCLUDE FIELD TYPE CLASS

Page 18: WordPress Cuztom Helper

CONTRIBUTE ON GITHUB

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

Page 19: WordPress Cuztom Helper

THANK YOU!

_ Ante Primorac (@anteprimorac)

_ Web Developer @ Slicejack

_ 2nd WordPress Meetup Split