1. Go to this page and download the library: Download devuri/cpt-meta-box 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/ */
devuri / cpt-meta-box example snippets
use Urisoft\PostMeta\Settings;
/**
* Plugin Name: Vehicle Management
* Plugin URI: https://example.com/plugins
* Description: An example plugin using the `cpt-meta` library to manage vehicles in WordPress.
* Version: 1.0
* Requires at least: 4.0
* Requires PHP: 7.4
* Author: Your Name
* Author URI: https://example.com
* Text Domain: wp-vehicle-management
* License: GPLv2
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if ( ! \defined( 'ABSPATH' ) ) {
exit;
}
// Autoload `cpt-meta` library
use Urisoft\PostMeta\PostType;
// Create and configure a custom post type: "Vehicle"
$vehiclePostType = new PostType(
'vehicle', // post type slug
'Vehicle', // singular name
'Vehicles', // plural name
[
'menu_icon' => 'dashicons-car',
'supports' => ['title', 'thumbnail'],
]
);
// Register the post type with WordPress
$vehiclePostType->register();
use Urisoft\PostMeta\Settings;
class VehicleSettings extends Settings
{
/**
* Override the settings() method to define your form fields.
*/
public function settings(): void
{
// Simple text input
$this->input('Vehicle Name', [
'placeholder' => 'Enter the vehicle name',
]);
// Textarea
$this->textarea('Description');
// Select dropdown (car, truck, motorcycle)
$this->select('Type', [
'car' => 'Car',
'truck' => 'Truck',
'motorcycle' => 'Motorcycle',
'selected' => $this->getMeta('type'), // set currently saved value as selected
]);
// Number input
$this->input('Top Speed (mph)', [
'type' => 'number',
'placeholder' => 'Enter top speed in mph',
]);
// Color field
$this->input('Available Colors', [
'type' => 'color',
'placeholder' => 'Choose a color',
]);
}
}
use Urisoft\PostMeta\MetaBox;
// Instantiate VehicleSettings for the "vehicle" post type
$vehicleSettings = new VehicleSettings('vehicle');
// Create a MetaBox for these settings
$vehicleMetaBox = new MetaBox($vehicleSettings, [
'name' => 'Vehicle Details', // The meta box title in the WP admin
'zebra' => true, // Zebra striping for table rows
]);
// Register the meta box so it appears in the edit screen
$vehicleMetaBox->register();
use Urisoft\PostMeta\Data;
// Initialize a Data instance for "vehicle" post type
$vehicleData = Data::init('vehicle');
// Retrieve meta for a specific Vehicle post (ID=100)
$info = $vehicleData->meta(100);
// Access custom fields
echo 'Vehicle Name: ' . esc_html($info['vehicle_name']);
echo 'Top Speed: ' . esc_html($info['top_speed']) . ' mph';
$vehiclePostType->addCustomRestEndpoint('/custom-details', function ($request) {
// Do something with $request...
return ['message' => 'Custom endpoint data!'];
}, 'GET');
$vehiclePostType->addAdminColumns(
function ($columns) {
$columns['type'] = 'Type';
$columns['speed'] = 'Top Speed';
return $columns;
},
function ($column, $post_id) {
switch ($column) {
case 'type':
echo esc_html(get_post_meta($post_id, 'type', true));
break;
case 'speed':
echo intval(get_post_meta($post_id, 'top_speed', true)) . ' mph';
break;
}
}
);
// Make the "Top Speed" column sortable:
$vehiclePostType->addSortableColumns(['speed' => 'top_speed']);
/**
* Plugin Name: Vehicle Management
*/
use Urisoft\PostMeta\PostType;
use Urisoft\PostMeta\Settings;
use Urisoft\PostMeta\MetaBox;
// 1. Register the post type
$vehicle = new PostType('vehicle', 'Vehicle', 'Vehicles', [
'menu_icon' => 'dashicons-car',
'supports' => ['title', 'thumbnail'],
]);
$vehicle->register();
// 2. Define settings fields
class VehicleSettings extends Settings
{
public function settings()
{
$this->input('Vehicle Name');
$this->textarea('Description');
$this->select('Type', [
'car' => 'Car',
'truck' => 'Truck',
'motorcycle' => 'Motorcycle',
'selected' => $this->getMeta('type'),
]);
$this->input('Top Speed (mph)', ['type' => 'number']);
}
}
// 3. Create a meta box for those settings
(new MetaBox(new VehicleSettings('vehicle'), [
'name' => 'Vehicle Details',
'zebra' => true,
]))->register();