Skip to content
Open

v3.0 #98

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
.phpunit.result.cache
coverage.xml
/coverage
145 changes: 102 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

## Requirements

* PHP >=7.2
* PHP >=8.1
* [Composer](https://getcomposer.org/)
* [WordPress](https://wordpress.org) >=5.1
* [WordPress](https://wordpress.org) >=6.3

## Installation

Expand All @@ -20,64 +20,123 @@ Run the following in your terminal to install PostTypes with [Composer](https://
$ composer require jjgrainger/posttypes
```

PostTypes uses [PSR-4](https://www.php-fig.org/psr/psr-4/) autoloading and can be used with the Composer's autoloader. Below is a basic example of getting started, though your setup may be different depending on how you are using Composer.

```php
require __DIR__ . '/vendor/autoload.php';

use PostTypes\PostType;

$books = new PostType( 'book' );

$books->register();
```

See Composer's [basic usage](https://getcomposer.org/doc/01-basic-usage.md#autoloading) guide for details on working with Composer and autoloading.
PostTypes uses [PSR-4](https://www.php-fig.org/psr/psr-4/) autoloading and can be used with the Composer's autoloader. See Composer's [basic usage](https://getcomposer.org/doc/01-basic-usage.md#autoloading) guide for details on working with Composer and autoloading.

## Basic Usage

Below is a basic example of setting up a simple book post type with a genre taxonomy. For more information, check out the [online documentation here](https://posttypes.jjgrainger.co.uk).
#### Create a custom post type

Custom post types are defined as classes that extend the base `PostType` class. At a minimum, the `name` method must be implemented to define the post type slug. All other methods are optional and allow you to configure labels, options, taxonomies, admin columns, filters, and more as needed.

```php
// Require the Composer autoloader.
require __DIR__ . '/vendor/autoload.php';
<?php

// Import PostTypes.
use PostTypes\PostType;
use PostTypes\Taxonomy;

// Create a book post type.
$books = new PostType( 'book' );
use PostTypes\Columns;

class Book extends PostType {
/**
* Define the Post Type name.
*/
public function name(): string {
return 'book';
}

/**
* Define the Post Type labels.
*/
public function labels(): array {
return [
'name' => __( 'Book', 'text-domain' ),
'singular_name' => __( 'Book', 'text-domain' ),
'menu_name' => __( 'Books', 'text-domain' ),
'all_items' => __( 'Books', 'text-domain' ),
'add_new' => __( 'Add New', 'text-domain' ),
'add_new_item' => __( 'Add New Book', 'text-domain' ),
'edit_item' => __( 'Edit Book', 'text-domain' ),
'new_item' => __( 'New Book', 'text-domain' ),
'view_item' => __( 'View Book', 'text-domain' ),
'search_items' => __( 'Search Books', 'text-domain' ),
'not_found' => __( 'No Books found', 'text-domain' ),
'not_found_in_trash' => __( 'No Books found in Trash', 'text-domain'),
'parent_item_colon' => __( 'Parent Book', 'text-domain' ),
];
}

/**
* Define Post Type feature supports.
*/
public function supports(): array {
return [
'title',
'editor',
'thumbnail',
'custom-fields'
];
}

/**
* Define Taxonomies associated with the Post Type.
*/
public function taxonomies(): array {
return [
'genre'
'category',
];
}

/**
* Set the menu icon for the Post Type.
*/
public function icon(): string {
return 'dashicons-book';
}

/**
* Set the admin post table filters.
*/
public function filters(): array {
return [
'genre',
'category'
];
}

/**
* Define the columns for the admin post table.
*/
public function columns(Columns $columns): Columns {
// Remove the author and date column.
$columns->remove( [ 'author', 'date' ] );

// Add a Rating column.
$columns->add( 'rating', __( 'Rating', 'post-types' ) );

// Populate the rating column.
$columns->populate( 'rating', function( $post_id ) {
echo get_post_meta( $post_id, 'rating', true );
} );

return $columns;
}
}
```

// Attach the genre taxonomy (which is created below).
$books->taxonomy( 'genre' );
### Register a custom post type

// Hide the date and author columns.
$books->columns()->hide( [ 'date', 'author' ] );
Once the custom post type class is created it can be registered to WordPress by instantiating and call the register method.

// Set the Books menu icon.
$books->icon( 'dashicons-book-alt' );
```php
// Instantiate the Books PostType class.
$books = new Books;

// Register the post type to WordPress.
// Register the books PostType to WordPress.
$books->register();

// Create a genre taxonomy.
$genres = new Taxonomy( 'genre' );

// Set options for the taxonomy.
$genres->options( [
'hierarchical' => false,
] );

// Register the taxonomy to WordPress.
$genres->register();
```

## Notes

* The full documentation can be found online at [posttypes.jjgrainger.co.uk](https://posttypes.jjgrainger.co.uk)
* The class has no methods for making custom fields for post types, use [Advanced Custom Fields](https://advancedcustomfields.com)
* The book's example used in the README.md can be found in [examples/books.php](examples/books.php)
* Licensed under the [MIT License](https://github.com/jjgrainger/PostTypes/blob/master/LICENSE)
* Maintained under the [Semantic Versioning Guide](https://semver.org)

Expand Down
143 changes: 102 additions & 41 deletions docs/Getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

## Requirements

* PHP >=7.2
* PHP >=8.1
* [Composer](https://getcomposer.org/)
* [WordPress](https://wordpress.org) >=5.1
* [WordPress](https://wordpress.org) >=6.3

## Installation

Expand All @@ -16,55 +16,116 @@ Run the following in your terminal to install PostTypes with [Composer](https://
$ composer require jjgrainger/posttypes
```

PostTypes uses [PSR-4](https://www.php-fig.org/psr/psr-4/) autoloading and can be used with the Composer's autoloader. Below is a basic example of getting started, though your setup may be different depending on how you are using Composer.

```php
require __DIR__ . '/vendor/autoload.php';

use PostTypes\PostType;

$books = new PostType( 'book' );

$books->register();
```

See Composer's [basic usage](https://getcomposer.org/doc/01-basic-usage.md#autoloading) guide for details on working with Composer and autoloading.
PostTypes uses [PSR-4](https://www.php-fig.org/psr/psr-4/) autoloading and can be used with the Composer's autoloader. See Composer's [basic usage](https://getcomposer.org/doc/01-basic-usage.md#autoloading) guide for details on working with Composer and autoloading.

## Basic Usage

Below is a basic example of setting up a simple book post type with a genre taxonomy.
#### Create a custom post type

Custom post types are defined as classes that extend the base `PostType` class. At a minimum, the `name` method must be implemented to define the post type slug. All other methods are optional and allow you to configure labels, options, taxonomies, admin columns, filters, and more as needed.

```php
// Require the Composer autoloader.
require __DIR__ . '/vendor/autoload.php';
<?php

// Import PostTypes.
use PostTypes\PostType;
use PostTypes\Taxonomy;

// Create a book post type.
$books = new PostType( 'book' );
use PostTypes\Columns;

class Book extends PostType {
/**
* Define the Post Type name.
*/
public function name(): string {
return 'book';
}

/**
* Define the Post Type labels.
*/
public function labels(): array {
return [
'name' => __( 'Book', 'text-domain' ),
'singular_name' => __( 'Book', 'text-domain' ),
'menu_name' => __( 'Books', 'text-domain' ),
'all_items' => __( 'Books', 'text-domain' ),
'add_new' => __( 'Add New', 'text-domain' ),
'add_new_item' => __( 'Add New Book', 'text-domain' ),
'edit_item' => __( 'Edit Book', 'text-domain' ),
'new_item' => __( 'New Book', 'text-domain' ),
'view_item' => __( 'View Book', 'text-domain' ),
'search_items' => __( 'Search Books', 'text-domain' ),
'not_found' => __( 'No Books found', 'text-domain' ),
'not_found_in_trash' => __( 'No Books found in Trash', 'text-domain'),
'parent_item_colon' => __( 'Parent Book', 'text-domain' ),
];
}

/**
* Define Post Type feature supports.
*/
public function supports(): array {
return [
'title',
'editor',
'thumbnail',
'custom-fields'
];
}

/**
* Define Taxonomies associated with the Post Type.
*/
public function taxonomies(): array {
return [
'genre'
'category',
];
}

/**
* Set the menu icon for the Post Type.
*/
public function icon(): string {
return 'dashicons-book';
}

/**
* Set the admin post table filters.
*/
public function filters(): array {
return [
'genre',
'category'
];
}

/**
* Define the columns for the admin post table.
*/
public function columns(Columns $columns): Columns {
// Remove the author and date column.
$columns->remove( [ 'author', 'date' ] );

// Add a Rating column.
$columns->add( 'rating', __( 'Rating', 'post-types' ) );

// Populate the rating column.
$columns->populate( 'rating', function( $post_id ) {
echo get_post_meta( $post_id, 'rating', true );
} );

return $columns;
}
}
```

// Attach the genre taxonomy (which is created below).
$books->taxonomy( 'genre' );
### Register a custom post type

// Hide the date and author columns.
$books->columns()->hide( [ 'date', 'author' ] );
Once the custom post type class is created it can be registered to WordPress by instantiating and call the register method.

// Set the Books menu icon.
$books->icon( 'dashicons-book-alt' );
```php
// Instantiate the Books PostType class.
$books = new Books;

// Register the post type to WordPress.
// Register the books PostType to WordPress.
$books->register();

// Create a genre taxonomy.
$genres = new Taxonomy( 'genre' );

// Set options for the taxonomy.
$genres->options( [
'hierarchical' => false,
] );

// Register the taxonomy to WordPress.
$genres->register();
```
28 changes: 0 additions & 28 deletions docs/Notes.md

This file was deleted.

Loading