PHP code example of gturpin / post-type-handler

1. Go to this page and download the library: Download gturpin/post-type-handler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gturpin / post-type-handler example snippets


// don't forget to import your autoloader
e_handler = new PostType( 'Book' );
$post_type_handler->register();

$post_type_handler->set_icon( 'dashicons-book' );
// Just the dashicon name also works
$post_type_handler->set_icon( 'book' );

$labels = [
	'add_new'   => __( 'my add_new', 'context' ),
	'all_items' => __( 'my all_items', 'context' ),
];

$options = [
	'public'    => true,
	'menu_icon' => 'dashicons-admin-post',
	'rewrite'   => [
		'slug' => 'my-post-type',
	],
];

$post_type_handler = new PostType( 'Book', $options, $labels );
$post_type_handler->register();

$post_type_handler = new PostType( 'Book' );

// add multiple taxonomies
$post_type_handler->set_taxonomies( [ 'custom-taxonomy', 'post_tag' ] );

// or add a single taxonomy
$post_type_handler->set_taxonomies( 'custom-taxonomy' );

$post_type_handler->register();

use PostTypeHandler\Taxonomy;
// Register the Taxonomy
$taxonomy_handler = new Taxonomy( 'custom-taxonomy' );
$taxonomy_handler->register();

// Add it to the PostType in PostType declaration
$post_type_handler = new PostType( 'Book' );
$post_type_handler->set_taxonomies( 'custom-taxonomy' );
$post_type_handler->register();

$taxonomy_handler = new Taxonomy( 'custom-taxonomy' );
// This is a post type slug and must be lower case!
// Also works with an array and/or variable: [ 'book', $post_type_handler ]
$taxonomy_handler->set_post_types( 'book' );
$taxonomy_handler->register();

$taxonomy_handler = new Taxonomy( 'custom-taxonomy' );
$taxonomy_handler->register();

$post_type_handler = new PostType( 'Book' );
// Also works with an array : [ 'post_tag', $taxonomy_handler ]
$post_type_handler->set_taxonomies( $taxonomy_handler );
$post_type_handler->register();

$posts = new PostType( 'Post' );
$posts->set_taxonomies( 'custom-taxonomy' );
$posts->register();

$posts = new PostType( 'Post' );
$posts->remove_taxonomy( 'post_tag' );
$posts->register();

// Call the columns function to get access to the column manager and add a new column
$post_type_handler->columns()->add( [
	'custom-slug' => __( 'Custom label', 'context' ),
	'year'        => __( 'Year', 'context' ),
] );

// You can also pass only one slug and label
$post_type_handler->columns()->add( 'custom-slug', 'Custom label' );

// Call the columns function to get access to the column manager and hide a built-in column or a custom one
$post_type_handler->columns()->hide( [
	'custom-slug',
	'date'
] );

// You can also hide only one column
$post_type_handler->columns()->hide( 'year' );

// Call the columns function to get access to the column manager and set all columns
$post_type_handler->columns()->set( [
	'custom-slug' => __( 'Custom label', 'context' ),
	'year'        => __( 'Year', 'context' ),
] );

// Call the columns function to get access to the column manager and populate a column
$post_type_handler->columns()->populate( 'custom-slug', function( $column, $post_id ) {
	echo get_the_title( $post_id );
} );
$post_type_handler->columns()->populate( 'year', function( $column, $post_id ) {
	echo get_the_date( 'Y', $post_id );
} );

$posts = new PostType( 'Post' );
$posts->columns()->add( 'id' );
$posts->columns()->populate( 'id', function( $column, $post_id ) {
	echo $post_id;
} );
$posts->register();

// Call the columns function to get access to the column manager and make a column sortable
$post_type_handler->columns()->sortable( [
	'rating' => 'rating',
	// You can add true to make the sort by numeric order
	// or false to make it by alphabetical order which is the default
	'year'   => [ 'year', true ],
] );

$post_type_handler->columns()->order( [
	// Reorder the native columns
	'title'       => 5,
	// Use large numbers to be sure that the column will be at the end
	'cb'          => 15,
	'custom-slug' => 1,
	'rating'      => 3,
	'author'      => 8,
] );

$post_type_handler->set_taxonomy_filters( [
	'custom-taxonomy',
] );