|
| 1 | +# Custom Post Type Inline List Table for WordPress |
| 2 | + |
| 3 | +By leveraging the Custom Post Type Inline List Table library, developers can easily register, display, and manage custom |
| 4 | +post types with enhanced functionalities such as custom columns, script and style enqueues, admin menu highlighting, and |
| 5 | +more. Designed for seamless integration with WordPress core functionalities, this tool streamlines the administrative |
| 6 | +tasks associated with custom post types, offering a developer-friendly approach to customization and management. |
| 7 | + |
| 8 | +**Key Features:** |
| 9 | + |
| 10 | +- **Custom Column Management:** Define and manage custom columns for your post type's list table, providing a tailored |
| 11 | + view of your data. |
| 12 | +- **Script and Style Enqueues:** Specify admin screens where custom scripts and styles should be loaded, enhancing the |
| 13 | + admin UI and UX. |
| 14 | +- **Admin Menu Highlighting:** Maintain menu and submenu highlighting within the admin sidebar, improving navigation and |
| 15 | + usability. |
| 16 | +- **Flexible Pagination:** Control the number of items displayed per page in the list table, supporting efficient data |
| 17 | + handling and presentation. |
| 18 | +- **Streamlined Integration:** Utilize hooks and WordPress core functions for a smooth integration, adhering to |
| 19 | + WordPress standards and best practices. |
| 20 | + |
| 21 | +This library facilitates the creation of a refined administrative interface for custom post types, ensuring a coherent |
| 22 | +and efficient management experience within the WordPress admin. |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +Ensure you have the package installed in your project. If not, you can typically include it using Composer: |
| 27 | + |
| 28 | +```php |
| 29 | +composer require arraypress/cpt-inline-list-table |
| 30 | +``` |
| 31 | + |
| 32 | +### Example Usage |
| 33 | + |
| 34 | +The `register_inline_list_table` function allows for easy setup and configuration of your custom post type's inline list |
| 35 | +table. Here's how to use it: |
| 36 | + |
| 37 | +```php |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +// Example usage of register_table_post_type to create a 'Conditional Fee' custom post type. |
| 42 | +register_inline_table_post_type( |
| 43 | + 'conditional_fee', // The key for the custom post type. |
| 44 | + __( 'Conditional Fee', 'text-domain' ), // The singular name of the custom post type for labels. |
| 45 | + __( 'Conditional Fees', 'text-domain' ), // The plural name of the custom post type for labels. |
| 46 | + 'conditional_fee', // The slug for the custom post type. |
| 47 | + [ 'excerpt', 'custom-fields', 'editor' ], // (Optional) Additional features the post type supports. |
| 48 | + false // (Optional) Whether to expose this post type in the WordPress REST API. Enables use of the Gutenberg editor and REST API queries. |
| 49 | +); |
| 50 | + |
| 51 | +/** |
| 52 | + * Defines columns for the list table of a custom post type, showcasing conditional discounts. |
| 53 | + * This configuration automatically includes default columns such as title, date, and author. |
| 54 | + * Additional custom columns can specify callbacks for rendering their content or use formatters |
| 55 | + * for specific data presentation. If only a key and label are provided (without a callback), |
| 56 | + * the system will first look for a matching property in the post object, then check post meta. |
| 57 | + * |
| 58 | + * When defining columns, it's crucial to understand the built-in logic for data retrieval: |
| 59 | + * |
| 60 | + * 1. If a 'callback' is provided, it will be used to fetch and render the column's data. |
| 61 | + * 2. Without a 'callback', the system searches for a matching property within the post object. |
| 62 | + * 3. If not found in the post object, the system then searches the post meta. |
| 63 | + * 4. A 'formatter' function can be used to format the value obtained from the callback or automatic data retrieval. |
| 64 | + * |
| 65 | + * This approach provides flexibility in displaying both standard and custom data within your list table. |
| 66 | + */ |
| 67 | + |
| 68 | +$columns = [ |
| 69 | + // Example of a custom column with a callback and formatter. |
| 70 | + 'amount' => [ |
| 71 | + 'label' => __( 'Amount', 'edd-advanced-fees' ), |
| 72 | + 'callback' => function ( $post ) { |
| 73 | + return get_post_meta( $post->ID, 'amount', true ); |
| 74 | + }, |
| 75 | + 'formatter' => function ( $value, $post ) { |
| 76 | + return edd_currency_filter( edd_format_amount( $value ) ); |
| 77 | + }, |
| 78 | + ], |
| 79 | + // Example of a simple column that relies on automatic data sourcing. |
| 80 | + 'expiration_date' => [ |
| 81 | + 'label' => __( 'Expiration Date', 'wp-conditional-discounts' ), |
| 82 | + // No callback needed; the system will automatically search for 'expiration_date' in post object or meta. |
| 83 | + ] |
| 84 | +]; |
| 85 | + |
| 86 | +// Registers an inline list table for a specified custom post type, configuring it with |
| 87 | +// custom columns, administrative URLs, and settings for menu highlighting. |
| 88 | +register_inline_list_table( |
| 89 | + 'conditional_fee', // The custom post type identifier. |
| 90 | + $columns, // Associative array of columns with render callbacks and formatters. |
| 91 | + 'edd_conditional_fees_table', // Hook name to attach the list table initialization. |
| 92 | + 10, // Priority for the hook to control when the list table is initialized. |
| 93 | + 'edit.php?post_type=download&page=edd-settings&tab=extensions', // URL for admin redirects. |
| 94 | + [ 'download_page_edd-settings' ], // Admin screens where scripts/styles should be enqueued. |
| 95 | + 'edit.php?post_type=download', // Parent file slug for menu highlighting. |
| 96 | + 'edd-settings' // Submenu file slug for submenu highlighting. |
| 97 | +); |
| 98 | + |
| 99 | +// Registers a settings section for managing conditional fees within the product settings. |
| 100 | +function register_section( array $sections ): array { |
| 101 | + $sections['conditional_fees'] = __( 'Conditional Fees', 'edd-conditional-fees' ); |
| 102 | + |
| 103 | + return $sections; |
| 104 | +} |
| 105 | + |
| 106 | +add_filter( 'edd_settings_sections_extensions', __NAMESPACE__ . '\\register_section' ); |
| 107 | + |
| 108 | +// Adds settings for the 'Conditional Fees' section within the product settings, enabling the configuration of rules. |
| 109 | +function register_settings( array $existing_settings ): array { |
| 110 | + return array_merge( $existing_settings, [ |
| 111 | + 'conditional_fees' => [ |
| 112 | + [ |
| 113 | + 'id' => 'conditional_fees_table', |
| 114 | + 'name' => __( 'Conditional Fees', 'edd-conditional-fees' ), |
| 115 | + 'type' => 'hook', |
| 116 | + ], |
| 117 | + ] |
| 118 | + ] ); |
| 119 | +} |
| 120 | + |
| 121 | +add_filter( 'edd_settings_extensions', __NAMESPACE__ . '\\register_settings' ); |
| 122 | +``` |
| 123 | + |
| 124 | +## Contributions |
| 125 | + |
| 126 | +We welcome contributions to enhance the library's functionality and compatibility. Feel free to submit pull requests or |
| 127 | +report issues on our GitHub repository. |
| 128 | + |
| 129 | +## License |
| 130 | + |
| 131 | +The Custom Post Type Inline List Table library is open-sourced software licensed under the GPL-2.0-or-later license. It |
| 132 | +is free for personal and commercial use, adhering to the terms of the GNU General Public License. |
0 commit comments