Skip to content

Commit

Permalink
Update page and post type documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlambe committed Jan 8, 2017
1 parent 11053de commit 4895c97
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Changelog
- Add `js` and `css` asset types to identify external assets.
- Add inline code for any assets.
- New helpers functions.
- Add Page class a `menu` property in order to define a custom menu display title.
- Add PostType `instance()` method to retrieve WP_Post_Type instance.

#### Fixes

Expand Down
25 changes: 14 additions & 11 deletions page.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Page
Introduction
------------

The `Page` class allows you to either create custom administrative pages that leverages the View API or either options pages that uses the WordPress Settings API combines with the Themosis framework Field API.
The `Page` class allows you to either create custom administrative pages that leverage the View API or either options pages that use the WordPress Settings API combined with the Themosis framework Field API.

Custom page
-----------
Expand All @@ -34,39 +34,41 @@ Like other classes, it uses a `set()` method in order to register the page.

### Extra parameters

The `set()` method allows you to pass extra parameters for your page. The current parameters are: `capability`, `icon`, `position`, `tabs`.
The `set()` method allows you to pass extra parameters for your page. The current parameters are: `capability`, `icon`, `position`, `tabs`, `menu`.

```php
$custom = Page::make('my-page', 'Theme options')->set([
'capability' => 'manage_options',
'icon' => 'dashicons-admin-site',
'position' => 20,
'tabs' => true
'tabs' => true,
'menu' => __("Options")
]);
```
- **capability**: _string_ Use this parameter to change the capability a user need in order to **view** the page.
- **icon**: _string_ Allows you to define a URL to an icon for the page menu or a [dashicon name](https://developer.wordpress.org/resource/dashicons/).
- **position**: _int_ By default the page is displayed at the bottom. Specify a number/position to move your page menu up or down in the WordPress administration.
- **tabs**: _boolean_ Boolean value only. By default is set to `true`. This parameter is useful when you're using the WordPress Settings API. This parameter will tell to defined sections to behave like tabs or not.
- **capability** _string_: Use this parameter to change the capability a user need in order to **view** the page.
- **icon** _string_: Allows you to define a URL to an icon for the page menu or a [dashicon name](https://developer.wordpress.org/resource/dashicons/).
- **position** _int_: By default the page is displayed at the bottom. Specify a number/position to move your page menu up or down in the WordPress administration.
- **tabs** _bool_: Boolean value only. By default is set to `true`. This parameter is useful when you're using the WordPress Settings API. This parameter will tell to defined sections to behave like tabs or not.
- **menu** _string_: Allows you to define a custom administration menu title which is displayed on the left sidebar.

### Create a custom page

By default, the Page class is configured to create options pages that leverages the WordPress Settings API with a pre-defined View file.
By default, the Page class is configured to create options pages that leverage the WordPress Settings API with a pre-defined View file.

If you simply call the following code, you'll get a blank page with its title:

```php
Page::make('my-custom-page', 'A Custom Page')->set();
```

In order to create a custom page, you need to pass a View file just like for the "front-end". You can store this View file anywhere inside the `views` directory of the `themosis-theme`.
In order to create a custom page, you need to pass a View file.

For example:

```php
// File is stored in /resources/views/options/my-page.scout.php
// File is stored in /resources/views/options/my-page.blade.php
<div class="wrap">
<h1>{{{ $__page->get('title') }}}</h1>
<h1>{{ $__page->get('title') }}</h1>
<ul>
<li>Option 1</li>
<li>Option 2</li>
Expand All @@ -82,6 +84,7 @@ $page_view = View::make('options.my-page');

Page::make('my-custom-page', 'A Custom Page', null, $page_view)->set();
```

> Notice the `$__page` variable into the view. This variable is the instance of your Page. In the example above, we grabbed the title property registered with the `Page::make()` method.
You can also take advantage of the View composer method to execute code only when your custom page view is rendered and pass it extra data. Check the [view guide]({{url}}/views) for more information.
Expand Down
29 changes: 20 additions & 9 deletions posttype.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ PostType
- [Plural and single display names](#plural-and-single-display-names)
- [Customize your custom post type](#customize-your-custom-post-type)
- [Retrieve custom post type parameters value](#retrieve-custom-post-type-parameters-value)
- [Retrieve the WP_Post_Type instance](#retrieve-the-wp-post-type-instance)
- [Custom status](#custom-status)
- [Add custom status](#add-custom-status)

Expand All @@ -24,7 +25,7 @@ PostType::make('books', 'Books', 'Book')->set([
]);
```

This will build a basic custom post type accessible in the WordPress admin. You can customize the custom post type by passing arguments to the `set()` method. In the above code we specified that the custom post type should be public. You can pass all the arguments defined in the WordPress core function [register_post_type](https://codex.wordpress.org/Function_Reference/register_post_type#Arguments) like so:
This will build a basic custom post type accessible in the WordPress admin. You can customize the custom post type by passing arguments to the `set()` method. In the above code we specified that the custom post type should be public. You can pass all the arguments defined in the WordPress core function [register_post_type](https://developer.wordpress.org/reference/functions/register_post_type/) like so:

```php
PostType::make('slug-books', 'Books', 'Book')->set([
Expand Down Expand Up @@ -52,25 +53,25 @@ In the above code sample, the custom post type will have a plural display name o
### Customize your custom post type

In order to define the behavior of your custom post type, use the `set()` method and pass it an array of [arguments](https://codex.wordpress.org/Function_Reference/register_post_type#Arguments) like so:
In order to define the behavior of your custom post type, use the `set()` method and pass it an array of [parameters](https://developer.wordpress.org/reference/functions/register_post_type/#parameters) like so:

```php
PostType::make('books', 'Books', 'Book')->set([
'public' => false,
'supports' => ['title', 'editor'],
'labels' => [
'add_item' => __('Add', THEMOSIS_TEXTDOMAIN)
'add_item' => __('Add', 'THEME_TEXTDOMAIN')
]
]);
```

> Note: the `THEMOSIS_TEXTDOMAIN` constant value is defined inside the `application.config.php` file.
> Note: the `THEME_TEXTDOMAIN` constant value is defined inside the `theme.config.php` file.
### Retrieve custom post type parameters value

The `get()` method allows you to retrieve parameters value from your custom post type that **you defined** in the `set()` method. Here is a list of what you can grab:

> Note: this methods replace the deprecated method $postType->getSlug(). If you use this method, make sure to update your code if you use a version of the framework higher or equal to 1.2.0.
> Note: this methods replace the deprecated method $postType->getSlug(). If you used this method, make sure to update your code if you use a version of the framework higher or equal to 1.2.0.
- **name**: The custom post type name.
- **label**: The plural display name.
Expand Down Expand Up @@ -107,14 +108,23 @@ $rewriteParameters = $postType->get('rewrite');
$slug = $rewriteParameters['slug']; // return the 'library' value.
```

### Retrieve the WP_Post_Type instance

WordPress now returns an object when registering a custom post type. In order to get access to this instance, use the `instance()` method like so:

```php
$books = PostType::make('books', 'Books', 'Book')->set(['public' => true]);
$type = $books->instance(); // return WP_Post_Type instance
```

Custom status
-------------

The PostType API provides a method to register one or multiple custom statuses for your custom post type.

> Note: This do not work for core post types (post, page, attachment,...).
When you add custom statuses, the default statuses are removed except the `draft` one. There is no need to define a `draft` status in your list of custom statuses. This is handled automatically.
When you add custom statuses, the default statuses are removed except the `draft` and `trash` ones.

When you add custom statuses, they appear in the order you defined them. And the first defined status is the one used in order to "publish/register/post" your custom post type. So basically, the first defined status is acting like the core "Publish" button or simply is the first status to be saved for your custom post type except if you choose to draft it.

Expand All @@ -124,7 +134,7 @@ In this example, keeping the books custom post type, we want to build a system t

#### Add one status

The `status()` method uses the same arguments than the WordPress [register_post_status()](https://codex.wordpress.org/Function_Reference/register_post_status) function. But you can also pass an array of statuses.
The `status()` method uses the same arguments than the WordPress [register_post_status()](https://developer.wordpress.org/reference/functions/register_post_status/) function. But you can also pass an array of statuses.

First, let's add one custom status `rent`:

Expand All @@ -136,7 +146,7 @@ $books = PostType::make('books', 'Books', 'Book')->set();
$books->status('rent');
```

In the example above, one status `rent` is registered. The method assign default properties to the status, the same properties used in the [register_post_status()](https://codex.wordpress.org/Function_Reference/register_post_status) function:
In the example above, one status `rent` is registered. The method assign default properties to the status, the same properties used in the [register_post_status()](https://developer.wordpress.org/reference/functions/register_post_status/) function:

- **label**: Its default value is the status `name` with first character set to uppercase
- **public**: Default to `true`
Expand All @@ -150,7 +160,7 @@ In the following example, we add a custom publish button text to our `rent` stat

```php
$books->status('rent', [
'publish_text' => __('Rent the book', THEMOSIS_TEXTDOMAIN)
'publish_text' => __('Rent the book', THEME_TEXTDOMAIN)
]);
```

Expand Down Expand Up @@ -193,6 +203,7 @@ $books->status([
]
]);
```

The array key is the custom status name and each value is an array of status properties.

> Note: Currently the UI for custom statuses is a work-in-progress. When viewing the list of your custom post type, if you click on `Quick Edit`, it still displays core statuses.
Expand Down

0 comments on commit 4895c97

Please sign in to comment.