Skip to content

hokoo/wpPostAble

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

What is wpPostAble

Library provides a functionality for associating your classes with WordPress class WP_Post. Once you create the instance, wpPostAble creates the WP_Post object and stores this in your instance.

You can manage your instance with such methods as

  • $instance->getPost();
  • $instance->getPostType();
  • $instance->savePost();
  • $instance->loadPost();
  • $instance->getStatus();
  • $instance->setStatus();
  • $instance->getTitle();
  • $instance->setTitle();

and others.

How to use

Preparing step by step.

  1. Create your own class based on wpPostAble interface

    use iTRON\wpPostAble;
    
    class Item implements wpPostAble{
        use wpPostAbleTrait;
    }
  2. Call wpPostAble() method in the beginning __construct() of your class.

    Pass to it two parameters

    $post_type string WP post type, associated with your class

    $post_id int Post ID for existing post, or nothing for creating new post

       /**
        * @throws Exception\wppaLoadPostException
        * @throws Exception\wppaCreatePostException
        */
       public function __construct() {
          $this->wpPostAble( POST_TYPE, $post_id );
          
          // Do anything you need
       }

Now you are able to use your class

$item = new Item();

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();

About

Create your own classes adaptive with WP_Post

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages