1. Go to this page and download the library: Download awethemes/wp-object 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/ */
awethemes / wp-object example snippets
use Awethemes\WP_Object\WP_Object;
class Product_Category extends WP_Object {
/**
* This is the name of this object type.
*
* @var string
*/
protected $object_type = 'product_category';
/**
* WordPress type for object, Ex: "post" and "term".
*
* @var string
*/
protected $wp_type = 'term';
/**
* Type of object metadata is for (e.g., term, post).
*
* @var string
*/
protected $meta_type = 'term';
/**
* The attributes for this object.
*
* Name value pairs (name + default value).
*
* @var array
*/
protected $attributes = [
'name' => '',
'description' => '',
'thumbnail' => 0,
];
/**
* An array of attributes mapped with metadata.
*
* @var array
*/
protected $maps = [
'thumbnail' => '_term_meta_thumbnail',
];
/**
* Setup the object attributes.
*
* @return void
*/
protected function setup() {
$this['name'] = $this->instance->name;
$this['description'] = $this->instance->description;
}
/**
* Run perform insert object into database.
*
* @see wp_insert_term()
*
* @return int|void
*/
protected function perform_insert() {
$inserted = wp_insert_term( $this['name'], $this->object_type, [
'description' => $this['description'],
]);
if ( is_wp_error( $inserted ) ) {
return;
}
return $inserted['term_id'];
}
/**
* Run perform update object.
*
* @see wp_update_term()
*
* @param array $dirty The attributes has been modified.
* @return bool|void
*/
protected function perform_update( array $dirty ) {
if ( ! $this->is_dirty( 'name', 'description' ) ) {
return true;
}
$updated = wp_update_term( $this->get_id(), $this->object_type, [
'name' => $this['name'],
'description' => $this['description'],
]);
return ! is_wp_error( $updated );
}
}
// And we have a `product_category` term with ID 10, created via admin dashboard.
// { name: "Keyboard", description: "Keyboard description" }
$product_category = new Product_Category( 10 );
// Get the $product_category data.
echo $product_category['name']; // Keyboard
echo $product_category['description']; // Keyboard description
// Modify properties
$product_category['name'] = 'New Keyboard';
$product_category->save();
echo $product_category['name']; // New Keyboard
// Then delete
$product_category->delete(); // Now `product_category` with term_ID 10 is gone.
$new_cate = new Product_Category;
$new_cate['name'] = 'Mouse';
$new_cate['description'] = 'Some desc';
$new_cate['thumbnail'] = 101; // `thumbnail` will mapping to `_term_meta_thumbnail` metadata and automatic save.
$new_cate->save();
echo $new_cate['thumbnail']; // 101
echo get_term_meta( $new_cate->get_id(), '_term_meta_thumbnail', true ); // 101
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.