Introduction
This is the documentation for Papi 1.x
Go to documentation for Papi 2.x
Papi has a different approach on how to work with fields and page types in WordPress. The idea is coming from how Page Type Builder in EPiServer works and has been loved by the developers.
So we though why don’t use the same approach in WordPress? Papi is today running in production and has been easy to work with when it came to add new fields. Papi don’t have any admin user interface where should add all fields, we use classes in PHP, where one class represents one page type and in your class you add all fields you need. It’s that easy!
Papi is completely open-source. You can use it however you like. If you want to help with its development you can submit your suggestions or improvements on in the Github repository.
Installation
If you’re using Composer to manage WordPress, add Papi to your project’s dependencies. Run:
composer require wp-papi/papi
Or manually add it to your composer.json
:
{
"require": {
"php": ">=5.3",
"wordpress": "~4.2",
"wp-papi/papi": "~1.2"
}
}
Page Type Directory
Papi does require a directory in your theme, plugin or somewhere in your WordPress site where your page types exists. You can add multiplied directories.
<?php
/**
* Register page types directory with Papi.
*/
add_filter('papi/settings/directories', function () {
return __DIR__ . '/includes/page-types';
});
Actions
Include plugins or properties build for Papi
action papi/include
<?php
/**
* Example of `papi/include` action that will include a custom property.
*/
add_action('papi/include', function () {
require_once 'class-papi-property-kvack.php';
});
Description
With this action you can include plugins or properties build for Papi. Third party properties should use this action when they load there custom property in the plugin.
API
current_page
<?php
/**
* Example of `current_page` function.
*/
echo current_page()->twitter_url;
// will be the same as
echo papi_field('twitter_url');
Get the current Papi page with all fields.
Parameters
No parameters exists.
papi_field
<?php
/**
* Example of `papi_field` function.
*/
echo papi_field('twitter_url');
Description
The post id will be added automatic if you are one a post, page or custom post type page.
When fetching values from another post this argument will be required.
You can also use the_papi_field to display it without echo
Parameters
Parameter | Default | Required | Description |
---|---|---|---|
$post_id | null | no | The post id will be added automatic if you are one a post, page or custom post type page. When fetching values from another post this argument will be required |
$slug | yes | The property slug to fetch value from | |
$default | null | no | When a default value is passed as argument it will use that value as return value and echo it if the property value is empty or don’t exists |
papi_fields
<?php
/**
* Example output from `papi_fields` function.
*/
array(
'Content' => array(
'top_module',
'feature_module',
'show_feature_module'
)
);
Description
Papi fields will return a array with meta box title as keys and array of all Papi field slugs in that meta box.
Parameters
No parameters exists.
papi_get_page
<?php
/**
* Example of `papi_get_page` function.
*/
echo papi_get_page()->twitter_url;
// with post id
echo papi_get_page(2)->twitter_url;
Description
When given a post id it will fetch the Papi page for that post id instead.
The papi page function will do the same as current page function if no post id is used as a argument.
Parameters
Parameter | Default | Required | Description |
---|---|---|---|
$post_id | null | no | The post id will be added automatic if you are one a post, page or custom post type page. When fetching values from another post this argument will be required |
papi_property
<?php
/**
* Example of `papi_property` function.
*/
// my-page-type.php
$this->box('My meta box', [
papi_property('properties/my-image.php', [
'slug' => 'my_meta_box_image'
])
]);
// properties/my-image.php
return papi_property([
'type' => 'image',
'title' => 'Image',
'slug' => 'custom_image_slug'
]);
Description
This argument is only used when you load a template file, the array will be used to override property options from the template file.
This function will be the same as $this->property
method on a page type.
When using a shared property file or another file that the property are stored in you can used it as a template file.
This is great to use when you will share properties over many page types and it will be less code to write.
Parameters
Parameter | Default | Required | Description |
---|---|---|---|
$file_or_options | yes | File path or a array containing property options | |
$values | array() | no | The values to override the template values with |
papi_tab
<?php
/**
* Example of `papi_tab` function.
*/
// my-page-type.php
$this->box('My meta box', [
$this->tab('tabs/image-tab.php', [
'title' => 'Background'
])
]);
// tabs/image.php
return papi_tabs([
'title' => 'Images',
'slug' => 'custom_image_slug'
], [
papi_property('properties/my-image.php'),
papi_property([
'type' => 'string',
'title' => 'Name',
'slug' => 'name'
])
]);
Description
This argument is only used when you load a template file, the array will be used to override tab options from the template file.
This function will be the same as $this->tab
method on a page type.
When using a shared tab file or another file that the tab are stored in you can used it as a template file.
This is great to use when you will share tabs over many page types and it will be less code to write.
Parameters
Parameter | Default | Required | Description |
---|---|---|---|
$file_or_options | yes | Title, file path or a array containing tab options | |
$properties | array() | no | The values to override the template values with or properties |
papi_template
<?php
/**
* Example of `papi_template` function.
*/
// my-page-type.php
$this->box('My meta box', [
$this->property([
'type' => 'dropdown',
'title' => 'Dropdown',
'slug' => 'my_dropdown',
'settings' => papi_template('settings/dropdown.php')
])
]);
// settings/dropdown.php
return [
'items' => [
'White' => '#ffffff',
'Black' => '#000000'
]
];
Description
papi_property
function uses this function load template file. This function can be used to load template files that returns arrays. It can be handy when you will have to repeater or dropdown values in another file.
Parameters
Parameter | Default | Required | Description |
---|---|---|---|
$file_or_options | yes | File path to template file | |
$properties | array() | no | The values to override the template values with |
the_papi_field
<?php
/**
* Example of `the_papi_field` function.
*/
the_papi_field('twitter_url')
// with post id
the_papi_field(1, 'twitter_url')
// with default value
the_papi_field(1, 'twitter_url', 'http://twitter.com/frozzare');
Description
This function will echo the value of a property using the property slug.
You can also use papi_field to fetch the value into a variable.
Parameters
Parameter | Default | Required | Description |
---|---|---|---|
$post_id | null | no | The post id will be added automatic if you are one a post, page or custom post type page. When fetching values from another post this argument will be required |
$slug | yes | The property slug to fetch value from | |
$default | null | no | When a default value is passed as argument it will use that value as return value and echo it if the property value is empty or don’t exists |
Box
<?php
/**
* Example of the default options
*/
$this->box([
'capabilities' => array(),
'context' => 'normal',
'mode' => 'standard',
'post_type' => 'page',
'priority' => 'default',
'sort_order' => 1000,
'title' => ''
]);
Documentation of the box
method. box
is a short name for metabox
in Papi.
Options
Key | Default |
---|---|
capabilities | array() |
context | ‘normal’ |
mode | 'standard’ |
post_type | 'page’ |
priority | 'default’ |
sort_order | 1000 |
Numeric value, lowest value in the meta box will be at the top and the highest value at the bottom
title | empty string | The title of the meta box. This can’t be empty, seamless mode will not show the title. When passing a string as the first argument for box method it will become array('title' => 'the title')
automatic
Callable method
Since version 1.2.0 it’s possible to use callable method as a second argument in your box method that returns a array with properties or tabs.
<?php
/**
* Example of using the callable parameter.
*/
public function register() {
$this->box('Content', array($this, 'content_box'));
}
public function content_box() {
return [
$this->property([
'type' => 'string',
'title' => 'Name'
]),
$this->property([
'type' => 'text',
'title' => 'About'
])
];
}
Create your own property
It’s easy to create your own property with Papi. You can use our Yo generator
It’s important that the class follow the Papi class name.
<?php
// Bad
class PropertyStringx extends Papi_Property {}
class Property_Stringx extends Papi_Property {}
class Papi_Property_String_x extends Papi_Property {}
// Good
class Papi_Property_Stringx extends Papi_Property {}
Example bootstrap and property class:
The html method is the important one, with out that there aren’t going to be any property output in WordPress admin.
You can check how the properties are to figure out how to do your custom properties.
<?php
// bootstrap.php
add_action('papi/include', function () {
require_once 'class-papi-property-stringx.php';
});
// class-papi-property-stringx.php
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/**
* Papi - Custom property string
*
* @package Papi
* @version 1.0.0
*/
class Papi_Property_Stringx extends Papi_Property {
/**
* The default value.
*
* @var string
* @since 1.0.0
*/
public $default_value = '';
/**
* Generate the HTML for the property.
*
* @since 1.0.0
*/
public function html() {
$options = $this->get_options();
$value = $this->get_value();
?>
<input type="text" name="<?php echo $options->slug; ?>" value="<?php echo $value; ?>" />
<?php
}
}
Page type
<?php
/**
* Example of a page type with no properties.
*/
class Video_Page_Type extends Papi_Page_Type {
/**
* The page type meta options.
*
* @return array
*/
public function page_type () {
return [
'name' => 'Video page',
'description' => 'A page where you can embed videos',
'template' => 'pages/video-page.php'
];
}
}
The page_type()
is a required method of the page type class. It should return an array containing the required keys.
Options | Required | Description |
---|---|---|
name | yes | The name of the page type |
description | no | The description of the page type |
labels | no | Since version 1.2.0 all post type can handle the labels object that exists on a post type. So this means that you can change “Add New Page” for every page type and have something like “Add New Startpage”. Just create a array with the labels keys and values on your page type meta array |
post_type | no | Array of post types that the page type should be registered on. Default is page |
sort_order | no | The sort order number of the page type |
template | no | The template file to render |
thumbnail | no | The thumbnail image that should apper on the add new page |
/**
* Example of template structure.
*/
themes/
my-theme/
pages/
about-page.php
contact-page.php
video-page.php
When you using template
key you can get a better structure of all pages type you are using and which template file it used.
When creating a new page you will get a new view before you get the edit view for your page where you should choose which page type to use. This will happen for all post types that uses page types.
Box method
This method is used to register all properties, tabs and remove meta boxes as described above.
Since version 1.2.0 the box method can has callable method as the second argument that returns a array with properties or tabs.
Read more about that under box section.
Namespaces
<?php
namespace Foo\Bar;
class Test_Page_Type extends \Papi_Page_Type {}
Since version 1.2.0 Papi has no problem to read page types that have a namespace.
Remove meta box
<?php
public function register() {
// A single metabox
$this->remove( 'comments' );
// Multiple metaboxes
$this->remove( array( 'comments', 'editor' ) );
}
It’s easy to remove metaboxes with the remove
method.
You can remove both post type support and meta boxes with remove.
Check out remove_post_type_support for right post type support or remove_meta_box for meta box slugs.
Properties
<?php
/**
* Example of the default options.
*/
papi_property([
'capabilities' => array(),
'default' => '',
'description' => '',
'disabled' => false,
'lang' => false,
'raw' => false,
'required' => false,
'settings' => array(),
'sidebar' => true,
'slug' => '',
'sort_order' => 100,
'title' => '',
'type' => ''
])
Papi does support 19 core properties (a field is a property in the page type) to start with and you can easy create your own using our Yeoman generator. The are several keys that all properties have.
Key | Default | Description |
---|---|---|
capabilities | array() | Can be a string with a role or capability or a array with many values |
default | empty string | The default value that are presented in the property |
description | empty string | The introduction text that will appear below the title text of the property. You could write your help text here. Since version 1.2.0 you can have “\n” to create new lines in the description |
disabled | false | Disable the property, won’t show in WordPress admin |
lang | false | When using this key you can specify which language will show the property |
raw | false | This will render the property without a table, good to use when creating a custom property that uses other properties |
required | false | By default all fields are non required in Papi but this can be changed with required option |
sidebar | true | Boolean that shows the sidebar on each property. If false the sidebar won’t show |
settings | array() | Array with custom settings for the property |
sort_order | 1000 | Numeric value, lowest value in the meta box will be at the top and the highest value at the bottom |
slug | empty string | The slug of property. If empty or not used the title will be generated to slug value |
title | empty string | The title of the property. Can be empty for blank title |
type | empty string | The property type (lower case is preferred to use) |
Note: be sure to have different slug for each properties on a page type, the same slug will not work great and you will lose data if you are using same slug for multiple properties.
Bool
type bool
<?php
/**
* Example of bool.
*/
papi_property([
'title' => 'True or false?',
'slug' => 'my_true_or_false_slug',
'type' => 'bool'
])
/**
* Example output.
*/
boolean true
Description
The property output a single checkbox, when clicked the output value will be true otherwise false.
Settings
No settings exists.
Checkbox
type checkbox
<?php
/**
* Example of checkbox.
*/
papi_property([
'title' => 'Categories',
'slug' => 'my_categories_slug',
'type' => 'checkbox',
'settings' => [
'items' => [
'White' => '#ffffff',
'Black' => '#000000'
]
]
])
/**
* Example output.
*/
Array
(
[0] => '#ffffff'
)
Description
With this property you can add multiple checkboxes. The key is the value that the user will se in the WordPress admin and the value is the value saved in the database.
Settings
Key | Default | Description |
---|---|---|
items | array() | Array with checkboxes, value or key/value |
selected | array() | The seleceted key or array of keys |
Color
type color
<?php
/**
* Example of color.
*/
papi_property([
'title' => 'Color',
'slug' => 'my_color_slug',
'type' => 'color'
])
/**
* Example output.
*/
string '#ffffff'
Description
The property output the WordPress color picker.
Settings
Key | Default | Description |
---|---|---|
show_input | true | Show the text input |
palettes | array() | Array with hex colors |
Datetime
type datetime
<?php
/**
* Example of datetime.
*/
papi_property([
'title' => 'Datetime',
'slug' => 'my_datetime_slug',
'type' => 'datetime'
])
/**
* Example output.
*/
string '2014-11-21'
Description
The date property has a picker build in using Pikaday with time support.
Settings
Key | Default | Description |
---|---|---|
format | YYYY-MM-DD hh:mm:ss | Show the text input |
show_seconds | false | Array with hex colors |
show_time | true | Array with hex colors |
use_24_hours | false | Array with hex colors |
Divider
type divider
<?php
/**
* Example of divider.
*/
papi_property([
'title' => 'Divider',
'type' => 'divider',
'description' => 'Non volutpat ultricies bibendum odio luctus.'
])
/**
* Example output.
*/
string '#ffffff'
Description
This property don’t output any form or something like that. It outputs a h3 tag with the title inside and the description below.
Settings
No settings exists.
Dropdown
type dropdown
<?php
/**
* Example of select.
*/
papi_property([
'title' => 'Dropdown',
'slug' => 'my_dropdown_slug',
'type' => 'dropdown',
'settings' => [
'items' => [
'White' => '#ffffff',
'Black' => '#000000'
]
]
])
/**
* Example output.
*/
string '#ffffff'
Description
With this property you can add a dropdown. The key is the value that the user will se in the WordPress admin and the value is the value saved in the database.
Settings
Key | Default | Description |
---|---|---|
items | array() (no limit) | Array with options, value or key/value |
selected | empty string | The select item that will be selected from start. The value should match a key of your items |
Editor
type editor
since 1.2.0
<?php
/**
* Example of Property Editor.
*/
papi_property([
'title' => 'Editor',
'slug' => 'my_editor_slug',
'type' => 'editor'
])
/**
* Example output.
*/
// String of text.
Description
The WordPress editor.
Settings
No settings exists.
Gallery
type gallery
since 1.2.0
<?php
/**
* Example of gallery.
*/
papi_property([
'title' => 'Gallery',
'slug' => 'my_gallery_slug',
'type' => 'gallery'
])
/**
* Example output.
*/
Array(
stdClass Object
(
[width] => 800
[height] => 600
[file] => '2014/09/cube.jpg'
[sizes] => Array
(
[thumbnail] => Array
(
[file] => 'cube-150x150.jpg'
[width] => 150
[height] => 150
[mime-type] => 'image/jpeg'
)
[medium] => Array
(
[file] => 'cube-300x225.jpg'
[width] => 300
[height] => 225
[mime-type] => 'image/jpeg'
)
)
[image_meta] => Array
(
[aperture] => 0
[credit] =>
[camera] =>
[caption] =>
[created_timestamp] => 0
[copyright] =>
[focal_length] => 0
[iso] => 0
[shutter_speed] => 0
[title] =>
[orientation] => 1
)
[alt] => 'Alt text'
[caption] => 'Caption text'
[description] => 'Description text'
[id] => 6
[is_image] => 1
[title] => 'Title text'
[url] => 'http://site.com/wp-content/uploads/2014/09/cube.jpg'
)
)
Description
With this property you can add a multiple image from the WordPress media library.
Settings
No settings exists.
Html
type html
<?php
/**
* Example of html.
*/
papi_property([
'title' => 'Information',
'type' => 'html',
'settings' => [
'html' => '<p>Hello, world</p>'
]
])
/**
* Example output.
*/
string '#ffffff'
Description
The property output custom html row on the page type in WordPress admin.
Settings
Key | Default | Description |
---|---|---|
html | empty string | String with html or a callable function or method. |
Image
type image
<?php
/**
* Example of image.
*/
papi_property([
'title' => 'Image',
'slug' => 'my_image_slug',
'type' => 'image'
])
/**
* Example output.
*/
stdClass Object
(
[width] => 800
[height] => 600
[file] => '2014/09/cube.jpg'
[sizes] => Array
(
[thumbnail] => Array
(
[file] => 'cube-150x150.jpg'
[width] => 150
[height] => 150
[mime-type] => 'image/jpeg'
)
[medium] => Array
(
[file] => 'cube-300x225.jpg'
[width] => 300
[height] => 225
[mime-type] => 'image/jpeg'
)
)
[image_meta] => Array
(
[aperture] => 0
[credit] =>
[camera] =>
[caption] =>
[created_timestamp] => 0
[copyright] =>
[focal_length] => 0
[iso] => 0
[shutter_speed] => 0
[title] =>
[orientation] => 1
)
[alt] => 'Alt text'
[caption] => 'Caption text'
[description] => 'Description text'
[id] => 6
[is_image] => 1
[title] => 'Title text'
[url] => 'http://site.com/wp-content/uploads/2014/09/cube.jpg'
)
Description
With this property you can add a image from the WordPress media library. If the gallery setting is set to true the output will be a array with objects instead of just one object.
Settings
Key | Default | Description |
---|---|---|
html | empty string | String with html or a callable function or method. |
Number
type number
<?php
/**
* Example of number.
*/
papi_property([
'title' => 'Number',
'slug' => 'my_number_slug',
'type' => 'number'
])
/**
* Example output.
*/
int 10
Description
The number property is the HTML5 number input field. No custom validation exists only the browsers validation will kick in. The value is saved as a string and the output is a string.
Settings
No settings exists.
Post
type post
<?php
/**
* Example of post.
*/
papi_property([
'title' => 'Post',
'slug' => 'my_post_slug',
'type' => 'post'
])
/**
* Example output.
*/
WP_Post Object
(
[ID] => 203
[post_author] => 1
[post_date] => 2014-11-18 22:07:38
[post_date_gmt] => 2014-11-18 22:07:38
[post_content] =>
[post_title] => 'The post title'
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => 'the_post_title'
[to_ping] =>
[pinged] =>
[post_modified] => 2014-11-18 22:09:05
[post_modified_gmt] => 2014-11-18 22:09:05
[post_content_filtered] =>
[post_parent] => 0
[guid] => 'http://site.com/?page_id=203'
[menu_order] => 0
[post_type] => 'page'
[post_mime_type] =>
[comment_count] => 0
[filter] => 'raw'
)
Description
With this property you can add reference to another post. It can’t handle multiple references like relationship
Since version 1.2.0 the post property by default include a blank row in the dropdown, this can be disabled by include_blank
Settings
Key | Default | Description |
---|---|---|
blank_text | empty string | Change the blank text to something else |
include_blank | true | Include the blank row or not |
post_type | ‘post’ | The post type that the property will load posts from. Can only be one post type |
query | array() | Append a WP_Query on all post types. Gist reference over WP_Query . Note that post_type in query will always be removed |
text | 'Select Post’ | The text above the dropdown. If this text is a empty string the p tag will be hidden |
Radio buttons
type radio
<?php
/**
* Example of radio buttons.
*/
papi_property([
'title' => 'Colors',
'slug' => 'my_radio_slug',
'type' => 'radio',
'settings' => [
'items' => [
'White' => '#ffffff',
'Black' => '#000000'
]
]
])
/**
* Example output.
*/
string '#ffffff'
Description
With this property you can create a list of radio buttons. The key is the value that the user will se in the WordPress admin and the value is the value saved in the database.
Settings
Key | Default | Description |
---|---|---|
items | array() | Array with radio buttons, value or key/value |
selected | empty string | The radio button that will be selected from start. The value should match a key of your items |
Reference
type reference
since 1.2.0
<?php
/**
* Example of reference.
*/
papi_property([
'title' => 'References',
'type' => 'reference',
'settings' => [
'slug' => 'top_module',
'page_type' => 'start-page-type'
]
])
/**
* Example output:
* The reference property does not save any values.
*/
Description
When using Post or Relationship property to load modules or something like that. You may what to check which pages are loading the module. This is where the reference property comes in hand, it will show which pages that has a reference to the module.
Settings
Key | Default | Description |
---|---|---|
slug | array() | String or array of slugs to look for references |
page_type | empty string | String or array of page types (the file name of the page type) to check |
Relationship
type relationship
<?php
/**
* Example of relationship.
*/
papi_property([
'title' => 'Relationship',
'slug' => 'my_relationship_slug',
'type' => 'relationship',
'settings' => [
'choose_max' => 3,
'post_type' => ['post', 'pages', 'my-custom-post-type']
]
])
/**
* Example output:
*/
Array
(
[0] => WP_Post Object
(
[ID] => 203
[post_author] => 1
[post_date] => 2014-11-18 22:07:38
[post_date_gmt] => 2014-11-18 22:07:38
[post_content] =>
[post_title] => 'The post title'
[post_excerpt] =>
[post_status] => 'publish'
[comment_status] => 'closed'
[ping_status] => 'closed'
[post_password] =>
[post_name] => 'the_post_title'
[to_ping] =>
[pinged] =>
[post_modified] => 2014-11-18 22:09:05
[post_modified_gmt] => 2014-11-18 22:09:05
[post_content_filtered] =>
[post_parent] => 0
[guid] => 'http://site.com/?page_id=203'
[menu_order] => 0
[post_type] => 'page'
[post_mime_type] =>
[comment_count] => 0
[filter] => 'raw'
)
)
Description
With this property you can link posts, pages or custom post types together. With the post_type
setting you can diced witch post types to use, default is page.
Settings
Key | Default | Description |
---|---|---|
choose_max | -1 (no limit) | Prevent how many post references that can be added |
post_type | 'page’ | Change which post types it loads post objects from |
query | array() | Append a WP_Query on all post types. Gist reference over WP_Query . Note that post_type in query will always be removed |
show_sort_by | true | Show the sort by dropdown or not. |
Filters
<?php
/**
* Example of `papi/property/relationship/sort_options` filter.
*/
add_filter('papi/property/relationship/sort_options', function ($not_allowed) {
return array_merge($not_allowed, [
'Name (alphabetically)' => function ($a, $b) {
return strcmp(strtolower($a->post_title), strtolower($b->post_title));
}
]);
});
Filter | Description |
---|---|
papi/property/relationship/sort_options | Add more sort options to property relationship. The array key is the name and the value that is saved as the sort order identification |
Repeater
type repeater
<?php
/**
* Example of repeater.
*/
papi_property([
'title' => 'Repeater',
'slug' => 'my_repeater_slug',
'type' => 'Repeater',
'settings' => [
'items' => [
[
'type' => 'string',
'title' => 'Title',
'slug' => 'my_string_slug'
],
[
'type' => 'dropdown',
'title' => 'Color',
'slug' => 'my_dropdown_slug',
'settings' => [
'items' => [
'White' => '#ffffff',
'Black' => '#000000'
]
]
]
]
]
])
/**
* Example output:
*/
Array
(
[0] => Array
(
[my_string_slug] => 'Test 1'
[my_dropdown_slug] => '#ffffff'
)
[1] => Array
(
[my_string_slug] => 'Test 2'
[my_dropdown_slug] => '#000000'
)
)
Description
The repeater property can create a repeater of sub fields which can be repeated again and again.
Settings
Key | Default | Description |
---|---|---|
items | array() | The array of properties, the same key/values as the $this->property method or papi_property function has. You can’t use property repeater inside a repeater. |
Filters
<?php
/**
* Example of `papi/property/repeater/exclude` filter.
*/
add_filter('papi/property/repeater/exclude', function ($exclude) {
return array_merge($exclude, [
'string'
]);
});
Filter | Description |
---|---|
papi/property/repeater/exclude | Prevent properties from render and working in repeater |
String
type string
<?php
/**
* Example of string.
*/
papi_property([
'title' => 'Name',
'slug' => 'my_name_slug',
'type' => 'string'
])
/**
* Example output.
*/
string 'Fredrik'
Description
The string property is just a text input field. The value is saved as a string and the output is a string.
Settings
No settings exists.
Text
type text
<?php
/**
* Example of text.
*/
papi_property([
'title' => 'Text',
'slug' => 'my_text_slug',
'type' => 'text'
])
/**
* Example output.
*/
string 'Fredrik'
Description
This property will output the textarea tag and the output will be a string with all text from the textarea.
Settings
No settings exists.
Url
type url
<?php
/**
* Example of url.
*/
papi_property([
'title' => 'Url with button',
'slug' => 'my_url_slug',
'type' => 'url',
'settings' => [
'mediauploader' => true
]
])
/**
* Example output.
*/
string 'http://site.com/wp-content/uploads/2014/11/image.png'
Description
This property will output the textarea tag and the output will be a string with all text from the textarea.
Settings
Key | Default | Description |
---|---|---|
mediauploader | false | When this is true a button will show next to the input field where you can open the WordPress media library |
Property filters
Format Value
filter papi/format_value/{$property_type}
<?php
/**
* Example of `papi/format_value/{$property_type}` filter.
*/
add_filter('papi/format_value/string', function ($value, $slug, $post_id) {
// do some magic with the value and return it.
return $value;
}, 10, 3);
Description
Format the value of the property before it’s returned to the theme.
Load Value
filter papi/load_value/{$property_type}
<?php
/**
* Example of `papi/load_value/{$property_type}` filter.
*/
add_filter('papi/load_value/string', function ($value, $slug, $post_id) {
// do some magic with the value and return it.
return $value;
}, 10, 3);
Description
This filter is applied after the value is loaded in the database.
Update Value
filter papi/update_value/{$property_type}
<?php
/**
* Example of `papi/update_value/{$property_type}` filter.
*/
add_filter('papi/update_value/string', function ($value, $slug, $post_id) {
// do some magic with the value and return it.
return $value;
}, 10, 3);
Description
This filter is applied before the value is saved in the database.
Setting filters
Change default sort order
filter papi/settings/sort_order
<?php
/**
* Example of `papi/settings/sort_order` filter.
*/
add_filter('papi/settings/sort_order', function () {
return 1;
});
Description
Change the default sort order for page types, meta boxes, tabs and properties.
Default sort order is 1000
Change standard page description for post type
filter papi/settings/standard_page_description_{$post_type}
since 1.2.0
<?php
/**
* Example of `papi/settings/standard_page_description_{$post_type}` filter.
*/
add_filter('papi/settings/standard_page_description_post', function ($page_type) {
return __('The standard blog post', 'my_theme');
});
Description
This filter is used to change the standard page description for a post type.
Default value is the translation of Just the normal WordPress page
Change standard page thumbnail for post type
filter papi/settings/standard_page_thumbnail_{$post_type}
since 1.2.0
<?php
/**
* Example of `papi/settings/standard_page_thumbnail_{$post_type}` filter.
*/
add_filter('papi/settings/standard_page_thumbnail_post', function () {
return '/path/to/thumbnail.png';
});
Description
This filter is used to change the standard page thumbnail for a post type.
Default value is the translation of empty string
Load page type from post id query string
filter papi/settings/page_type_from_post_qs
since 1.2.0
<?php
/**
* Example of `papi/settings/page_type_from_post_qs` filter.
*/
add_filter('papi/settings/page_type_from_post_qs', function () {
return 'parent_post';
});
Description
This filter is used to load a page type from a query string that have another post id.
When using plugins as Polylang the query string from_post
is used to tell which post the current post was created from.
Papi will not now which page type to load then and it should be the same as the from_post
. So that’s when this filter will be useful.
This filter is not post type specific and will work on every post type.
Default value is from_post
.
Only page type for post type
filter papi/settings/only_page_type_{$post_type}
since 1.2.0
<?php
/**
* Example of `papi/settings/only_page_type_{$post_type}` filter.
*/
add_filter('papi/settings/only_page_type_post', function () {
return 'post-page-type';
});
Description
This filter is used to register all page type directories that Papi should look for page types in.
You can return a string or a array of strings.
Page type directories
filter papi/settings/directories
since 1.2.0
<?php
/**
* Example of `papi/settings/directories` filter.
*/
add_filter('papi/settings/directories', function () {
return false;
});
Description
This filter is used to register all page type directories that Papi should look for page types in.
You can return a string or a array of strings.
Default value is empty array
.
Show page type on add new page type view
filter papi/settings/show_page_type_{$post_type}
since 1.2.0
<?php
/**
* Example of `papi/settings/show_page_type_{$post_type}` filter.
*/
add_filter('papi/settings/show_page_type_post', function ($page_type) {
if ($page_type === 'start-page-type') {
return false;
}
return true;
});
Description
This filter is used to filter which page types that can be listed on the add new page type view.
The function will send in the file name of post types as a argument.
Returning false on a page type will hide the page type on the add new page type view.
Default value for every page type is true
Show standard page type for post type
filter papi/settings/standard_page_type_{$post_type}
since 1.2.0
<?php
/**
* Example of `papi/settings/standard_page_type_{$post_type}` filter.
*/
add_filter('papi/settings/directories', function () {
return __DIR__ . '/page-types';
});
Description
This filter allows you to hide the standard page on the “add new page type” view.
Default value for every post type is true