Fake post data with wp-cli. Made for WordPress.
Relies on nelmio/alice and fzaninotto/Faker.
Extended with user capabilities by 5queezer/faker.
wp package install trendwerk/faker
Requires wp-cli >= 2.0.
wp faker fake <files>...
Parameter | Default | Required | Description |
---|---|---|---|
<file> |
null |
Yes | Location to an Alice YAML file |
wp faker delete
Deletes all fake data.
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 |
Your YAML file(s) could look like any the examples below.
For more understanding of the internals:
- The YAML file is interpreted by nelmio/alice;
- Any of the functions from fzaninotto/Faker are available;
- Additionaly, the
<terms>
function is provided by this library.
Trendwerk\Faker\Entity\Post:
post{1..100}:
post_content: <paragraphs(4, true)>
post_title: '<sentence()>'
Generates 100 posts with a title and content.
Trendwerk\Faker\Entity\Post:
post{1..100}:
post_title: '<sentence()>'
post_type: 'testimonials'
Generates 100 posts from the post type testimonials
with a title.
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
.
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.
Parameter | Default | Required |
---|---|---|
taxonomy |
null |
Yes |
amount |
1 |
No |
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
.
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()>'
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.
Currently the only type of supported attachments are 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.
# 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.