Home / Register custom post type in WordPress from Class

Register custom post type in WordPress from Class

$var = new CreateThePostNow();
class CreateThePostNow {
	
	private $post_type          = 'post_type';  // Your Post Type
	private $post_name          = 'Post Name';  // Name
	private $post_singular_name = 'Post Names';  // Singular Name

	function __construct() {
		add_action( 'init', [$this, 'register_post_type'] ); // Call Register Post Type

		add_action( 'add_meta_boxes', [$this, 'post_meta_register'] ); // Call Register Post Type
		add_action( 'save_post', [$this, 'post_meta_save'] ); // Call Register Post Type

		add_action( 'init', [$this, 'custom_tax_reg'] ); // Register taxonomy
		add_action( 'current_screen', [$this, 'posts_columns'] ); // Register taxonomy
	}

	function register_post_type() {
		$args = array(
			'description'         => $this->post_name,
			'show_ui'             => true,
			'menu_position'       => 4,
			'menu_icon'           => 'dashicons-images-alt2',
			'exclude_from_search' => true,
			'labels'              => array(
				'name'               => $this->post_name,
				'singular_name'      => $this->post_singular_name,
				'add_new'            => 'Add New',
				'add_new_item'       => 'Add New',
				'edit'               => 'Edit',
				'edit_item'          => 'Edit',
				'new-item'           => 'New '.$this->post_singular_name,
				'view'               => 'View '.$this->post_singular_name,
				'view_item'          => 'View '.$this->post_singular_name,
				'search_items'       => 'Search '.$this->post_name,
				'not_found'          => 'No '.$this->post_singular_name.' Found',
				'not_found_in_trash' => 'No '.$this->post_singular_name.' Found',
				'parent'             => 'Parent '.$this->post_name
				),
			'public'          => true,
			'capability_type' => 'post',
			'hierarchical'    => false,
			'rewrite'         => true,
			'supports'        => array('title', 'editor', 'thumbnail', 'category')
			);
		register_post_type( $this->post_type , $args );
	}
	
	// Post meta data start
		function post_meta_register(){
			add_meta_box(
				'web_features',
				__('Web Features', 'prism'), // Title
				[$this, 'meta_items'], // Meta boxes
				$this->post_type, // Post-type
				'normal'
				);
		}

		function meta_items($post){
			$_features = get_post_meta($post->ID, '_features', true);
			?>
			<input name="Process" type="hidden" value="1">
			
			<div class="bootstrap-iso">
				<div class="table-responsive added_features p-relative">

					<!-- Start of Add Feature -->
						<div class="add_more_feature">
							<button type="button"
								data-toggle="modal" data-target="#AllFeaturesModal"
								class="btn btn-sm btn-primary">
								<i class="fa fa-plus-circle" aria-hidden="true"></i>
								ADD FEATURE
							</button>
						</div>
						<!-- Modal -->
						<div class="modal fade bd-example-modal-lg" id="AllFeaturesModal"
							tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
							<div class="modal-dialog" role="document">
								<div class="modal-content">
								<div class="modal-header">
									<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
									<button type="button" class="close" data-dismiss="modal" aria-label="Close">
										<span aria-hidden="true">×</span>
									</button>
								</div>

								<div class="modal-body all_features">
									<?php
										$AllFeatures = get_posts([
											'post_type'      => 'web_features',
											'posts_per_page' => -1,
											'exclude'        => $_features
										]);
										// echo '<PRE>'; print_r($AllFeatures); echo '</PRE>';
									?>
									<table id="mytable" class="table table-bordred table-striped ">

										<thead>
											<th><?php _e('Feature', 'prism'); ?></th>
											<th><?php _e('Type', 'prism'); ?></th>
											<th><?php _e('Time', 'prism'); ?></th>
											<th><?php _e('Price', 'prism'); ?></th>
										</thead>
										<tbody>
											
											<?php if($AllFeatures){ foreach ($AllFeatures as $Feature) { ?>
												<tr>
													<td>
														<div class="checkbox">
															<label>
																<input type  = "checkbox"
																	name  = "_features[]"
																	value = "<?php echo $Feature->ID; ?>">
																	<?php echo $Feature->post_title; ?>
															</label>
														</div>
													</td>
													<td>Page</td>
													<td>2 hour</td>
													<td>$12.00</td>
												</tr>
											<?php } } ?>

										</tbody>

									</table>
								</div>

								<div class="modal-footer">
									<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
									<!-- <button type="button" class="btn btn-primary">Save changes</button> -->
								</div>
								</div>
							</div>
						</div>
					<!-- End of Add Feature -->

					<?php if($_features){ ?>
						<table id="mytable" class="table table-bordred table-striped">

							<thead>
								<th col-span=""><?php _e('Feature', 'prism'); ?></th>
								<th><?php _e('Type', 'prism'); ?></th>
								<th><?php _e('Time', 'prism'); ?></th>
								<th><?php _e('Price', 'prism'); ?></th>
								<th col-span=""></th>
							</thead>

							<tbody>
								<?php foreach ($_features as $feature) { if ( get_post_status ( $feature ) ) {
									?>
									<tr>
										<td>
											<div class="checkbox">
												<label>
													<input type = "checkbox" checked name = "_features[]" value = "<?php echo $feature; ?>">
													<?php echo get_the_title($feature); ?>
												</label>
											</div>
										</td>
										<td>Page</td>
										<td>2 hour</td>
										<td>$12.00</td>
										<td class="text-right">
											<p data-placement="top" data-toggle="tooltip" title="Edit">
												<a href="<?php echo get_edit_post_link($feature); ?>" target="_blank">
													<button class="btn btn-primary btn-xs"
														type="button"
														data-title="Edit" data-toggle="modal" data-target="#edit">
														<i class="fa fa-pencil" aria-hidden="true"></i>
													</button>
												</a>
												<a href="<?php echo get_permalink($feature); ?>" target="_blank">
													<button class="btn btn-primary btn-xs"
														type="button"
														data-title="Edit" data-toggle="modal" data-target="#edit">
														
														<i class="fa fa-external-link" aria-hidden="true"></i>
													</button>
												</a>
											</p>
										</td>
									</tr>
								<?php } } ?>
							</tbody>

						</table>
					<?php } ?>
				</div>

				<hr>

				<div class="form-group">
					<label for="FeatureDemos"><?php _e('Demo URL\'s', 'prism'); ?></label>
					<label class="text-muted"><?php _e('Separate by new line', 'prism'); ?></label>
					<textarea id="FeatureDemos" rows="5" class="form-control" name="meta[demo_links]"><?php echo get_post_meta($post->ID, 'demo_links', true);?></textarea>
				</div>
			</div>
			<?php
		}

		function post_meta_save($post_id){
			if ( function_exists( 'get_current_screen' ) ){
				$screen = get_current_screen();
			};
			if ( $this->post_type == $screen->post_type ){

				if ($_POST['Process']) {
					if ($AllMetas = $_POST['meta']) {
						foreach ($AllMetas as $MetaKey => $MetaValue) {
							update_post_meta($post_id, $MetaKey, $MetaValue);
						}
					}
					if ($_features = $_POST['_features']) {
						$_features = array_unique($_features);
						update_post_meta($post_id, '_features', $_features);
					}else{
						update_post_meta($post_id, '_features', null);
					}
				}
			}
		}
	// End on Post meta data 

	// Register taxonomy
		function custom_tax_reg() {
			// Register now
			/* register_taxonomy(
				'Jononi',
				$this->post_type,
				array(
					'label'        => __( 'Jononi' ),
					'rewrite'      => array( 'slug' => 'Jononi' ),
					'hierarchical' => true,
					'sort' => true,
				)
			); */
		}
	// End of Register taxonomy

	// Action for custom fields on Admin
		function posts_column_items($defaults) {
			$defaults['other_info'] = 'Other info';
			return $defaults;
		}

		function posts_column_display($column_name, $post_ID) {
			if ($column_name == 'other_info') {
				if($demos_ = get_post_meta($post_ID, 'demo_links', true)){
					$demo_urls = explode(PHP_EOL, $demos_);
				}
				$total_demo = ($demo_urls) ? count($demo_urls) : false;


				if($_features = get_post_meta($post_ID, '_features', true)){
					$total_feature = count($_features);
				}
				if ($total_feature) {
					echo 'Features: '.$total_feature;
				}else{
					echo 'Features: 0';
				}
				echo '<br>';

				if ($total_demo) {
					echo 'Demo: '.$total_demo;
				}else{
					echo 'Demo: 0';
				}
			}
		}

		function posts_columns() {
			$currentScreen = get_current_screen();
			if ( $currentScreen->post_type == $this->post_type ) {
				add_filter('manage_posts_columns', [$this, 'posts_column_items']);
				add_action('manage_posts_custom_column', [$this, 'posts_column_display'], 10, 2);
			}

		}
	// End Action for custom fields on Admin
}

all rights reserved