Download the PHP package devuri/cpt-meta-box without Composer
On this page you can find all versions of the php package devuri/cpt-meta-box. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package cpt-meta-box
Simple Implemetation for custom meta boxes and fields for WordPress custom post types
This is a PHP Composer package that can help you create WordPress Meta Boxes and Meta Fields. It also includes a Data
class that you can use to retrieve the saved meta data.
Installation
To install this package, run the following command in your terminal:
MetaBox and Settings
This is a set of PHP code snippets for creating a WordPress meta box and its related settings using the MetaBox
and Settings
classes. These classes provide functionality to define and handle custom meta boxes and their settings in WordPress.
MetaBox Class
The MetaBox
class allows you to create a custom meta box in WordPress. It provides the following features:
- Automatic creation and rendering of the meta box based on settings.
- Saving and updating the meta data when the post is saved.
- Integration with the
Settings
class for handling the meta box settings.
Usage
To use the MetaBox
class, follow these steps:
- Include the code in your WordPress project.
- Create an instance of the
Settings
subclass, such as theDetails
class in the provided example, passing the relevant post type as a parameter. - Implement the
settings()
method in yourSettings
subclass to define the meta box settings. This method defines the fields to be displayed in the meta box. - Implement the
data()
method in yourSettings
subclass to handle the data submitted from the meta box fields. This method sanitizes and prepares the data before saving it. - Customize the
settings()
anddata()
methods by adding fields and defining their settings within thesettings()
method. - Create an instance of the
MetaBox
class, passing theSettings
object and optional arguments. This will automatically create and display the meta box on the relevant post types. - The entered data will be saved when the post is updated.
Settings Class
The Settings
abstract class provides a foundation for defining and handling settings related to a specific post type. It works in conjunction with the MetaBox
class to define the settings for the meta box.
Usage
To use the Settings
class, follow these steps:
- Create a subclass that extends the
Settings
class. - Implement the
settings()
method to define the meta box settings. - Implement the
data()
method to handle the data submitted from the meta box fields (be sure to sanitize). - Customize the
Settings
subclass by adding fields and defining their settings within thesettings()
method. - Instantiate the
Settings
subclass and pass it to theMetaBox
class constructor to create and display the meta box.
Example Usage
The following example demonstrates the usage of the MetaBox
and Settings
classes:
This example demonstrates how to create a meta box for the 'vehicle' post type using the MetaBox
and Settings
classes. The Details
class is a subclass of Settings
and defines the meta box settings and data handling. The MetaBox
class is used to create and display the meta box with various customization options.
Data Class
The Data
class provides various utility methods for working with data in WordPress. It includes functions for retrieving and manipulating post-related data such as post meta, post items, and generating custom edit links.
Usage
To use the Data
class, follow these steps:
- Create an instance of the
Data
class, optionally passing a post type as a parameter. If no post type is specified, the default post type 'post' will be used. - Utilize the available methods of the
Data
class to perform data-related operations.
Methods
The Data
class provides the following methods:
__construct($post_type = null)
: Initializes theData
object with an optional post type parameter. If no post type is provided, the default post type 'post' is used.init($post_type)
: Static method to create a new instance of theData
class with a specified post type. Returns aData
object.edit(int $id, $class = '')
: Generates an edit link for a given post ID. Returns the edit link HTML as a string. This method checks if the current user has the capability to edit posts before generating the link.list()
: Retrieves a list of post items as key-value pairs. The keys are the post IDs, and the values are the post titles. Returns an array.getkey($key, $data)
: Retrieves a value from an array by its key. If the key is not set,null
is returned. If the data is not an array,false
is returned.meta($ID, $name = null)
: Retrieves the post meta data for a given post ID. The meta data is retrieved using the specified meta name, or if not provided, the post type is used as the meta name. Returns an array of meta data, including the post ID and thumbnail ID.items($n = -1)
: Retrieves an array of the latest posts or posts matching the given criteria. The number of posts to retrieve can be specified with the$n
parameter. Returns an array of post objects or post IDs.
Example Usage
Here's an example of how you can utilize the Data
class:
In this example, we create a Data
object for the 'vehicle' post type and use its methods to retrieve post items, post meta data, generate an edit link, get a value from an array, and retrieve the latest posts. You can adapt these examples to suit your specific needs.
Conclusion
This package provides a simple and easy-to-use way to create MetaBoxes and meta fields in WordPress. If you have any questions or issues, please feel free to submit an issue.