Skip to content

hokoo/wpPostAble

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 

Repository files navigation

What is wpPostAble

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

  1. 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';
    }
  2. Call wpPostAble() method in the beginning of the __construct() method 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( 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.

About

Create your own classes adaptive with WP_Post

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages