Skip to content

trendwerk/faker

Repository files navigation

Faker

Fake post data with wp-cli. Made for WordPress.

Build Status

Relies on nelmio/alice and fzaninotto/Faker.

Extended with user capabilities by 5queezer/faker.

Install

wp package install trendwerk/faker

Requires wp-cli >= 2.0.

Usage

wp faker fake <files>...

Options

Parameter Default Required Description
<file> null Yes Location to an Alice YAML file

Delete data

wp faker delete

Deletes all fake data.

Support

The YAML file supports:

Fields Description
WP_Post All properties from wp_insert_post
WP_User All properties from wp_insert_user
meta Post meta
terms Terms for taxonomies, see Terms
acf Advanced Custom Fields fields, see ACF

YAML / Faker

Your YAML file(s) could look like any the examples below.

For more understanding of the internals:

Basic

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_content: <paragraphs(4, true)>
    post_title: '<sentence()>'

Generates 100 posts with a title and content.

Post Type

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_title: '<sentence()>'
    post_type: 'testimonials'

Generates 100 posts from the post type testimonials with a title.

Meta

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_title: '<sentence()>'
    post_type: 'testimonials'
    meta:
      name: '<name()>'
      address: '<address()>'

Generates 100 testimonials with a title and a custom field called name and one called address.

Terms

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_content: <paragraphs(3, true)>
    post_title: '<sentence()>'
    terms:
      category: <terms('category', 1)>
      post_tag: <terms('post_tag', 7)>

Generates 100 posts with a title, content, 1 random category and 7 random tags.

Using <terms> is not required. You could also provide an array of integers yourself or use randomElements.

Options

Parameter Default Required
taxonomy null Yes
amount 1 No

ACF

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_content: <paragraphs(3, true)>
    post_title: '<sentence()>'
    acf:
      name: '<name()>'
      address: '<address()>'

Generates 100 posts with a title, content, and two filled ACF fields: name and address.

Duplicate field names

In ACF, it is possible to have multiple fields with the same name. This could cause formatting conflicts when faking data with this library. If you have two fields with the same name, using the unique field key is recommended:

Trendwerk\Faker\Entity\Post:
  post{1..100}:
    post_content: <paragraphs(3, true)>
    post_title: '<sentence()>'
    acf:
      field_56cf2f782e9b1: '<name()>' # Name
      address: '<address()>'

Users

Trendwerk\Faker\Entity\User:
  user{1..10}:
    user_login: '<username()>'
    user_pass: '<username()>'
    first_name: '<firstName()>'
    last_name: '<lastName()>'
    display_name: '<firstName()> <lastName()>'
    user_email: '<email()>'
    role: 'author'

Generates 10 users with the role of an author.

Attachments

Currently the only type of supported attachments are images.

Images

Trendwerk\Faker\Entity\Image:
  image{1..3}:
    data: '<image()>'

Generates 3 image attachments. Images are provided by Faker, which in turn are provided by LoremPixel.

Post + (Featured) Image + User

# image.yml
Trendwerk\Faker\Entity\Image:
  image{1..3}:
    data: '<image()>'
# user.yml
Trendwerk\Faker\Entity\User:
  user{1..10}:
    user_login: '<username()>'
    user_pass: '<username()>'
    user_email: '<email()>'
    role: 'author'
# post.yml
Trendwerk\Faker\Post:
  post{1..1}:
    post_content: <paragraphs(3, true)>
    post_title: '<sentence()>'
    post_author: '@user*->id'
    meta:
      _thumbnail_id: '@image*->id'

You can now supply all three files to wp faker fake:

wp faker fake image.yml user.yml post.yml

Make sure you load the file that contains the referenced objects first.