a custom Field repository that transforms Wordpress custom fields into an Object Relational Mapper (ORM).
Technically, Loki provides an easy-to-use interface for generating, populating and reading custom fields based on custom PHP objects.
Once you have created an object with some special property annotations, the plugin will do the rest.
PHP 5.6+
Installation of Advanced Custom Fields Pro Plugin is recommended, but not neccessary
Install plugin like any other Wordpress plugin
The plugin maps objects with specific annotations to a set of custom fields. Configuration is done via annotation
<?php
/**
* @Field_Group(name="sales", title="Annual sales reports", provider="acf")
*/
class Sales_Report {
/**
* @var string
* @Field(name="report", type="text")
*/
private $report;
/**
* We need a valid getter for lazy loading
* @return string
*/
public function get_report() {
return $this->report;
}
/**
* We need a valid setter for saving data
* @param $report
*/
public function set_report( $report ) {
$this->report = $report;
}
}
Property | Values | Default |
---|---|---|
name | any | none, mandatory value |
title | any | none, optional |
provider | native, acf | native |
Property | Values | Default |
---|---|---|
name | any | none, mandatory value |
label | any | none, optional |
type | text, true_false | text |
default | any | text |
Loki CFR comes with support for two different custom field provider
Advanced Custom Fields Pro (recommended, but requires installed ACF Pro plugin)
@Field_Group(name="sales", title="Annual sales reports", provider="acf")
Wordpress native (default, no further plugin required)
@Field_Group(name="sales", title="Annual sales reports", provider="native")
After generating an annotated class we register the class(es) and let the plugin do all the work (generating fields, fieldgroups etc)
include_once get_stylesheet_directory() . '/models/Sales_Report.php';
add_action( 'init', 'register' );
function register() {
$repository = register_custom_field_repository([Sales_Report::class]);
}
add_action( 'init', 'write' );
function write() {
$repository = get_custom_field_repository();
$user = $repository->find(Sales_Report::class, $post_id);
$user->set_report('Some Report');
$repository->persist($user);
}
Data is fetched with lazy loading by using the getter methods
add_action( 'init', 'read' );
function read() {
$repository = get_custom_field_repository();
$user = $repository->find(Sales_Report:class, $post_id);
print $user->get_report();
}
- Daniel Steiner - Initial work - dsteiner23
This project is licensed under the GNU GENERAL PUBLIC LICENSE - see the LICENSE.md file for details