{"id":18931063,"url":"https://github.com/gturpin-dev/posttypehandler","last_synced_at":"2025-04-15T16:32:20.362Z","repository":{"id":38414213,"uuid":"496949491","full_name":"gturpin-dev/PostTypeHandler","owner":"gturpin-dev","description":"Helper class to quickly manage PostType and Taxonomy declarations","archived":false,"fork":false,"pushed_at":"2023-11-07T12:19:34.000Z","size":93,"stargazers_count":7,"open_issues_count":1,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-09-18T01:16:13.070Z","etag":null,"topics":["hacktoberfest","hacktoberfest-2022","php","wordpress","wordpress-php-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gturpin-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-05-27T10:10:50.000Z","updated_at":"2024-05-11T15:22:12.000Z","dependencies_parsed_at":"2023-11-07T13:29:46.613Z","dependency_job_id":"eebac544-0b7a-4d56-ac55-ccb6ed041647","html_url":"https://github.com/gturpin-dev/PostTypeHandler","commit_stats":{"total_commits":77,"total_committers":4,"mean_commits":19.25,"dds":"0.20779220779220775","last_synced_commit":"50ecb1e8569c3d808cb8eccf9debeebbb0ed00ec"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gturpin-dev%2FPostTypeHandler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gturpin-dev%2FPostTypeHandler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gturpin-dev%2FPostTypeHandler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gturpin-dev%2FPostTypeHandler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gturpin-dev","download_url":"https://codeload.github.com/gturpin-dev/PostTypeHandler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223679363,"owners_count":17184853,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","hacktoberfest-2022","php","wordpress","wordpress-php-library"],"created_at":"2024-11-08T11:40:20.610Z","updated_at":"2024-11-08T11:40:21.284Z","avatar_url":"https://github.com/gturpin-dev.png","language":"PHP","readme":"# PostTypeHandler\nHelper class to quickly manage PostType and Taxonomy declarations\n\n[![Packagist](https://img.shields.io/packagist/v/gturpin/post-type-handler.svg?style=flat-square\u0026colorB=blue)](https://packagist.org/packages/gturpin/post-type-handler?color=red)\n[![Packagist](https://img.shields.io/packagist/dt/gturpin/post-type-handler.svg?style=flat-square\u0026colorB=blue)](https://packagist.org/packages/gturpin/post-type-handler)\n\n## Features\n- Easily add new Post Types or update existing ones\n- Easily add new Taxonomies\n- Easily link Post Types to Taxonomies \u0026 vice versa\n- Easily add new columns to the admin and manage them ( populate, sort, reorder )\n- Easily add admin taxonomy filters\n\n## Installation\n\n**Install with composer**\n\nRun the following in your terminal to install the package with composer\n\n```sh\ncomposer require gturpin/post-type-handler\n```\n\nThe package use the autoloader, so don't forget to register the autoloader.\nIf you don't know how see the basic example below.\n\n## Basic Usage\nBelow is a basic example of setting up a simple PostType.\n\n```php\n// don't forget to import your autoloader\nrequire_once __DIR__ . '/vendor/autoload.php';\n\nuse PostTypeHandler\\PostType;\n\n$post_type_handler = new PostType( 'Book' );\n$post_type_handler-\u003eregister();\n```\n\nYou can set the dashicon like that :\n\n```php\n$post_type_handler-\u003eset_icon( 'dashicons-book' );\n// Just the dashicon name also works\n$post_type_handler-\u003eset_icon( 'book' );\n```\n\nYou can add custom $options and $labels to the PostType declaration.\n\n```php\n$labels = [\n\t'add_new'   =\u003e __( 'my add_new', 'context' ),\n\t'all_items' =\u003e __( 'my all_items', 'context' ),\n];\n\n$options = [\n\t'public'    =\u003e true,\n\t'menu_icon' =\u003e 'dashicons-admin-post',\n\t'rewrite'   =\u003e [\n\t\t'slug' =\u003e 'my-post-type',\n\t],\n];\n\n$post_type_handler = new PostType( 'Book', $options, $labels );\n$post_type_handler-\u003eregister();\n```\n\nYou can also set the taxonomies for the PostType if they are previously registered.\n\n```php\n$post_type_handler = new PostType( 'Book' );\n\n// add multiple taxonomies\n$post_type_handler-\u003eset_taxonomies( [ 'custom-taxonomy', 'post_tag' ] );\n\n// or add a single taxonomy\n$post_type_handler-\u003eset_taxonomies( 'custom-taxonomy' );\n\n$post_type_handler-\u003eregister();\n```\n\nOtherwise you can register a new Taxonomy and then even add it to the PostType declaration.\n\n```php\nuse PostTypeHandler\\Taxonomy;\n// Register the Taxonomy\n$taxonomy_handler = new Taxonomy( 'custom-taxonomy' );\n$taxonomy_handler-\u003eregister();\n\n// Add it to the PostType in PostType declaration\n$post_type_handler = new PostType( 'Book' );\n$post_type_handler-\u003eset_taxonomies( 'custom-taxonomy' );\n$post_type_handler-\u003eregister();\n```\n\nOr you can set the taxonomy to a Post Type that is already registered.\n\n```php\n$taxonomy_handler = new Taxonomy( 'custom-taxonomy' );\n// This is a post type slug and must be lower case!\n// Also works with an array and/or variable: [ 'book', $post_type_handler ]\n$taxonomy_handler-\u003eset_post_types( 'book' );\n$taxonomy_handler-\u003eregister();\n```\n\nYou can give the Taxonomy object itself to the PostType.\n\n```php\n$taxonomy_handler = new Taxonomy( 'custom-taxonomy' );\n$taxonomy_handler-\u003eregister();\n\n$post_type_handler = new PostType( 'Book' );\n// Also works with an array : [ 'post_tag', $taxonomy_handler ]\n$post_type_handler-\u003eset_taxonomies( $taxonomy_handler );\n$post_type_handler-\u003eregister();\n```\n\nYou can do the same with an existing CPT, like Post :\n\n```php\n$posts = new PostType( 'Post' );\n$posts-\u003eset_taxonomies( 'custom-taxonomy' );\n$posts-\u003eregister();\n```\n\nYou can also remove a taxonomy from a Post Type.\n\n```php\n$posts = new PostType( 'Post' );\n$posts-\u003eremove_taxonomy( 'post_tag' );\n$posts-\u003eregister();\n```\n\n## Manage Post Types columns\nI will explain some examples of how to manage the columns for a Post Type.\n\n1. Register the Post Type.\n2. Manipulate the columns.\n3. Save the changes by re-registering the Post Type.\n\n### Add a new column\nTo add new columns to a Post Type you can do the following\n\n```php\n// Call the columns function to get access to the column manager and add a new column\n$post_type_handler-\u003ecolumns()-\u003eadd( [\n\t'custom-slug' =\u003e __( 'Custom label', 'context' ),\n\t'year'        =\u003e __( 'Year', 'context' ),\n] );\n\n// You can also pass only one slug and label\n$post_type_handler-\u003ecolumns()-\u003eadd( 'custom-slug', 'Custom label' );\n```\n\n### Hide a column\nTo hide a column you can do the following\n\n```php\n// Call the columns function to get access to the column manager and hide a built-in column or a custom one\n$post_type_handler-\u003ecolumns()-\u003ehide( [\n\t'custom-slug',\n\t'date'\n] );\n\n// You can also hide only one column\n$post_type_handler-\u003ecolumns()-\u003ehide( 'year' );\n```\n\n### Set all columns\nYou can set all columns at once\nBy doing this you must take a look at the [Manage columns hook](https://developer.wordpress.org/reference/hooks/manage_post_type_posts_columns/) to prevent unwanted columns\n\n```php\n// Call the columns function to get access to the column manager and set all columns\n$post_type_handler-\u003ecolumns()-\u003eset( [\n\t'custom-slug' =\u003e __( 'Custom label', 'context' ),\n\t'year'        =\u003e __( 'Year', 'context' ),\n] );\n```\n\n### Populate a column\nTo populate a column you can do the following\n- You can only populate one column at once\n- You must display the content and not return it\n- You can't use this to populate a [built-in column](https://developer.wordpress.org/reference/hooks/manage_post_type_posts_columns/#more-information)\n\n```php\n// Call the columns function to get access to the column manager and populate a column\n$post_type_handler-\u003ecolumns()-\u003epopulate( 'custom-slug', function( $column, $post_id ) {\n\techo get_the_title( $post_id );\n} );\n$post_type_handler-\u003ecolumns()-\u003epopulate( 'year', function( $column, $post_id ) {\n\techo get_the_date( 'Y', $post_id );\n} );\n```\n\nYou can also add new columns to existing and built in post types\n\n```php\n$posts = new PostType( 'Post' );\n$posts-\u003ecolumns()-\u003eadd( 'id' );\n$posts-\u003ecolumns()-\u003epopulate( 'id', function( $column, $post_id ) {\n\techo $post_id;\n} );\n$posts-\u003eregister();\n```\n\n### Make a column sortable\nTo make a column sortable you can do the following\n- You must make the column slug in key and value of the array\n- The value must be the meta key to sort using\n- Don't forget to populate the column before you make it sortable!\n\n```php\n// Call the columns function to get access to the column manager and make a column sortable\n$post_type_handler-\u003ecolumns()-\u003esortable( [\n\t'rating' =\u003e 'rating',\n\t// You can add true to make the sort by numeric order\n\t// or false to make it by alphabetical order which is the default\n\t'year'   =\u003e [ 'year', true ],\n] );\n```\n\n### Order the columns\nYou may want to order the columns, even the native ones, do the following\n\n- Set the final position, starting from 0\n- Avoid duplicate and negative positions in your array!\n\n```php\n$post_type_handler-\u003ecolumns()-\u003eorder( [\n\t// Reorder the native columns\n\t'title'       =\u003e 5,\n\t// Use large numbers to be sure that the column will be at the end\n\t'cb'          =\u003e 15,\n\t'custom-slug' =\u003e 1,\n\t'rating'      =\u003e 3,\n\t'author'      =\u003e 8,\n] );\n```\n\n### Adding taxonomy filters to the edit screen\nTo add taxonomy filters to the edit screen you can do the following\n\n- Add a list of taxonomies slugs\n- The order is important because the filters will be displayed that order!\n\n```php\n$post_type_handler-\u003eset_taxonomy_filters( [\n\t'custom-taxonomy',\n] );\n```\n\n## Hooks\n\n| Hook type | Hook name                                     | Params         | Description                                  |\n| --------- | --------------------------------------------- | -------------- | -------------------------------------------- |\n| Filter    | gt_post_type_{$post_type}_labels              | array $labels  | Custom the labels for the post type          |\n| Filter    | gt_post_type_{$post_type}_options             | array $options | Custom the options for the post type         |\n| Filter    | gt_post_type_{$post_type}_check_slug_conflict | boolean $check | Check for slug conflicts. Requires DB query. |\n| Filter    | gt_taxonomy_{$post_type}_labels               | array $labels  | Custom the labels for the taxonomy           |\n| Filter    | gt_taxonomy_{$post_type}_options              | array $options | Custom the options for the taxonomy          |\n\n\n## TODOS\n\n- ~~Can also add taxonomy by sending the object itself ( by the object itself, maybe with a __tostring method )~~\n- ~~Adding a way to manage Columns~~\n  - ~~Hide columns and defaults for each post type~~\n  - ~~Adding new columns to the admin screen~~\n  - ~~Set columns order~~\n  - ~~Set the entire columns array~~\n  - ~~Populate any column with a custom function~~\n  - ~~Can sort each columns with their values ( numerically / alphabetically )~~\n- ~~Adding a function to easily add icon without using the $options array~~\n- ~~Adding a way to manage the Filters on screen admin~~\n  - ~~Set an array to order them and keep an order~~\n- ~~Add a class to manage the taxonomies~~\n  - ~~Adding new Taxonomies~~\n  - Can work on existing taxonomies ( post_tag \u0026 category )\n  - ~~Can be registered on a post type directly ( by the slug or the object itself, maybe with a __tostring method )~~\n- ~~Can work on existing post types ( update options and labels )~~\n- ~~Add the @link/author/license to the main class~~\n- Same columns but for the taxonomies\n- Can delete row actions ( edit, view, trash, delete ) from the admin screen ( 'post_row_actions' )\n- Check if we can do the same for adding ones\n- Check to add/remove bulk edit actions\n- Check if we can add/update/remove the list above the bulk actions ( all / published etc ... )\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgturpin-dev%2Fposttypehandler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgturpin-dev%2Fposttypehandler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgturpin-dev%2Fposttypehandler/lists"}