hokoo/wppostable
最新稳定版本:0.6.2
Composer 安装命令:
composer require hokoo/wppostable
包简介
Library provides a functionality for associating your classes with WordPress class WP_Post.
README 文档
README
Library provides a functionality for associating your models with WordPress WP_Post model. Once you create the instance, wpPostAble creates the WP_Post object and stores it in your instance.
You can manage your instance with such methods as
$instance->getTitle();$instance->setTitle();$instance->getMetaField();$instance->setMetaField();$instance->getStatus();$instance->setStatus();$instance->getPost();$instance->getPostType();$instance->savePost();$instance->loadPost();$instance->publish();$instance->draft();
and others.
Use
$instance->getParam();$instance->setParam();
method to manage metafields, stored inside posts table using post_content_filtered field.
How to use
-
Create your own class based on wpPostAble interface
use iTRON\wpPostAble\wpPostAble; use iTRON\wpPostAble\wpPostAbleTrait; use iTRON\wpPostAble\Exceptions\wppaCreatePostException; use iTRON\wpPostAble\Exceptions\wppaLoadPostException; class Item implements wpPostAble { use wpPostAbleTrait; const POST_TYPE = 'item'; }
-
Call
wpPostAble()method in the beginning of the__construct()method of your class.Pass to it two parameters
$post_typestring WP post type, associated with your class$post_idint Post ID for existing post, or nothing for creating new post/** * @throws Exception\wppaLoadPostException * @throws Exception\wppaCreatePostException */ public function __construct( int|null $post_id = null ) { $this->wpPostAble( self::POST_TYPE, (int) $post_id ); // Do anything you need }
Now you are able to use your class
Create new post
$item = new Item();
or load from existing one
$item = new Item( $post_id );
Once you create an instance, wpPostAble creates new post in WordPress as a draft.
Let's try change the title
$item->setTitle('The best item');
Now you have set title, and let's try to save it in database
$item->savePost();
Maybe it's time to publish?
$item->publish();
You can do it by single line
$item->setTitle('The best item')->publish();
More options you can find in the description above and in the source code.
统计信息
- 总下载量: 276
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-19