PHP code example of devuri / cpt-meta-box

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 DevUri\PostTypeMeta\MetaBox;
use DevUri\PostTypeMeta\Settings;

// Implement meta fields
class Details extends Settings
{
    // The metabox settings
    public function settings(): void
    {
        // Basic input field
        echo self::form()->input('Title', $this->get_meta('title'));

        // Regular textarea
        echo self::form()->textarea('Description', $this->get_meta('description'));

        // Editor using a simplified version of wp_editor
        echo self::editor('Description', $this->get_meta('description'));
    }

    // The data, is $post_data $_POST and needs to be sanitized
    public function data($post_data): array
    {
        return [
            'title' => sanitize_textarea_field($post_data['title']),
            'description' => sanitize_textarea_field($post_data['description_textarea']),
        ];
    }
}

// Create a new instance of the Details class for the 'vehicle' post type
$details = new Details('vehicle');

// Create a meta box without stripes
new MetaBox($details);

// Create a meta box with zebra table
new MetaBox($details, true);

// Create a meta box with 	NO zebra table
new MetaBox($details, false);

// Create a meta box with a custom label 'Vehicle Details'
// and the meta key will be `vehicle-details_cpm`
new MetaBox($details, ['name' => 'Vehicle Details']);

// Create a meta box with a custom label 'Vehicle Details' and zebra stripes
new MetaBox($details, [
    'name' => 'Vehicle Details',
    'zebra' => true,
]);

// Zebra styles are applied by default, this will also use zebra style
new MetaBox($details, ['name' => 'Vehicle Details']);

// Or instantiate directly, in this case the metabox will be `Details` based on the class name
// and the meta key will be `details_cpm`
new MetaBox(new Details('vehicle'));

use DevUri\PostTypeMeta\Data;

// Create a new instance of the Data class for the 'vehicle' post type
$data = Data::init('vehicle');

// Get a list of post items
$postItems = $data->list();

// If you are not using the post type name as meta name you need to pass in the name in this example `details_cpm`:
$metaData = $data->meta(123, 'details_cpm);

// Retrieve the meta data for a specific post
$metaData = $data->meta(123);

// Generate an edit link for a post
$editLink = Data::edit(123);

// Get a value from an array by its key
$value = Data::getkey('key_name', $dataArray);

// Retrieve the latest 10 posts
$latestPosts = $data->items(10);

// Retrieve the latest 10 posts,
// you can pass in array of arguments to filter retrieved posts.
// See WP_Query::parse_query() for all available arguments.
$latestPosts = $data->items(10, [ 'orderby' => 'date' ]);

// if no arguments are provided these will be used.
$defaults = [
	'numberposts'      => 5,
	'category'         => 0,
	'orderby'          => 'date',
	'order'            => 'DESC',
	'