-
Notifications
You must be signed in to change notification settings - Fork 10
Supports Faker Press
FakerPress is a plugin, which helps you to generate fake posts content. It does pretty similar thing as "Fixtures" or "Seeds" in modern PHP Frameworks.
By using this extension you can specify patterns to generate post content and JCF or ACF fields. Once specified any developer or QA engineer can generate content without deep knowledge of custom fields structure directly from admin interface.
Imagine that you have a post type for Properties with a lot of custom fields. You need to test search, map pins grouping, etc. In this case you will need good and valid data populated inside the DB. Manually creating such entries will take for a long time. Our extension can help you with that.
Step 1: Install and activate FakerPress plugin
If you use composer package manager you can run:
composer require wpackagist-plugin/fakerpress
* if you use our WordPress Starter kit it's pre-installed.
Otherwise you need to download FakerPress from an admin panel or from a WordPress website.
After that activate a plugin.
Inside your Post_Type
class create a faker
method. It is executed each time new fake post is created with FakerPress. This method should return an array of Post Meta key-value pairs. There are also several pre-defined keys, which we will check below.
Post_Type\Employee.php
<?php
...
public function faker() {
return array(
'meta_key1' => 'value1',
'meta_key2' => 'value2',
);
}
Pre-defined keys are:
-
post_title
- re-write standard post title, which is generated by FakerPress plugin -
post_content
- re-write standard post content, which is generated by FakerPress plugin -
post_featured_image
- ability to generate and set post featured image
To make it easier to generate data you can create an instance of Faker generator class. In this way it will generate new fake data each time you call faker()
method.
<?php
...
public function faker() {
$fkr = \JustCoded\WP\Framework\Supports\FakerContent::instance();
return array(
'meta_key1' => $fkr->words(3),
'meta_key2' => $fkr->email(),
...
);
}
- Open WP Dashboard
- Navigate to FakerPress - Posts
- Specify main settings for your fake post.
- Skip meta fields settings in interface (because we already set all structure inside the code)
- Press "Generate" button.
After that all newly created posts will have meta data for all your custom fields, which you specified in your pattern.
Method | Description |
---|---|
text($max_chars = 200) | Text generatorwith max allowed charecters limit |
html_text( $content_size, $html_tags ) |
HTML content generator for wysiwyg editor. |
words($qty) | Return several random words |
image_attachment( $w, $h, $type ) |
Download image from placehold.it and insert it as attachment. |
number( $min, $max ) |
Number generator in a range. |
percent() | Percent number generator. |
date($format) | Date generator. |
person() | First and Last name generator. |
email() | Email generator. |
company() | Company name generator. |
job_title() | Job title generator. |
repeater( $qty, $callable ) |
Generate repeatable groups of data (see example below). |
flexible_content() | ACF Flexible Content data wrapper generator (see example below). |
flexible_layout() | ACF Flexible Content Layout data generator. Should be used inside flexible_content() method. |
faker->{native_method} | You can run native faker methods from public Faker library. |
<?php
namespace Boilerplate\Theme\Post_Type;
use JustCoded\WP\Framework\Objects\Post_Type;
use JustCoded\WP\Framework\Supports\FakerContent;
/**
* Custom post type Employee to illustrate single/archive features
*/
class Employee extends Post_Type {
/**
* ID
*
* @var string
*/
public static $ID = 'employee';
/**
* Rewrite URL part
*
* @var string
*/
public static $SLUG = 'employee';
/**
* Registration function
*/
public function init() {
...
}
/**
* Generate faker content
*/
public function faker() {
$fkr = FakerContent::instance();
return [
// Post object fields.
'post_title' => $fkr->person(),
'post_content' => $fkr->html_text( [ 3, 5 ] ),
'post_featured_image' => $fkr->image_attachment( 1280, 920 ),
// Simple meta data.
'position' => $fkr->job_title(),
'bio' => $fkr->text( 200 ),
// Collection example.
'gallery' => $fkr->repeater(
[ 1, 5 ], // generate from 1 to 5 items.
function () use ( $fkr ) { // item pattern.
return [
'image' => $fkr->image_attachment( 480, 240 ),
];
}
),
// ACF Flexible Content generator and fields examples.
'flex_content' => $fkr->flexible_content( [
$fkr->flexible_layout( 'module_A', [
'words' => $fkr->words( 5 ),
'number' => $fkr->number(),
'date' => $fkr->date(),
'person_name' => $fkr->person(),
'company_name' => $fkr->company(),
'job_title' => $fkr->job_title(),
'email' => $fkr->email(),
'domain_name' => $fkr->faker->domainName,
] ),
$fkr->flexible_layout( 'module_B', [
'text_field' => $fkr->words( 5 ),
] ),
] ),
];
}
}