{"id":25858933,"url":"https://github.com/codad5/easy-metabox","last_synced_at":"2025-06-12T02:39:27.099Z","repository":{"id":276509085,"uuid":"929493665","full_name":"codad5/easy-metabox","owner":"codad5","description":"Your wp metabox library","archived":false,"fork":false,"pushed_at":"2025-02-08T18:14:19.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-08T18:24:49.950Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codad5.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-08T17:17:14.000Z","updated_at":"2025-02-08T18:21:28.000Z","dependencies_parsed_at":"2025-02-08T18:35:08.689Z","dependency_job_id":null,"html_url":"https://github.com/codad5/easy-metabox","commit_stats":null,"previous_names":["codad5/codad5-easy-metabox"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codad5%2Feasy-metabox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codad5%2Feasy-metabox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codad5%2Feasy-metabox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codad5%2Feasy-metabox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codad5","download_url":"https://codeload.github.com/codad5/easy-metabox/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241422977,"owners_count":19960597,"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":[],"created_at":"2025-03-01T20:38:13.767Z","updated_at":"2025-03-01T20:38:14.492Z","avatar_url":"https://github.com/codad5.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# EasyMetabox\n\nEasyMetabox is a WordPress library that simplifies the creation and management of custom meta boxes for posts, pages, and custom post types. It provides an intuitive API for adding custom fields with various input types and handling their data.\n\n## Core Features\n\n- **Quick Edit Support:** Enables inline editing of custom post type fields directly from the post list table.\n  \n  **Important:** Adding a field for Quick Edit using the `allow_quick_edit` option only works if you also add that field as a column in the post list table. To do this, use the appropriate filters (like `manage_{post_type}_posts_columns` and `manage_{post_type}_posts_custom_column`) to include the field in the table display.\n\n- **WordPress Media Integration:** Provides a `wp_media` field type for seamless media uploads and management.\n\n## Installation\n\nSince this is a WordPress library, you can't use Composer directly unless your application compiles Composer dependencies. Here are the installation steps:\n\n1. Download the source code from GitHub: [codad5/easy-metabox](https://github.com/codad5/easy-metabox)\n2. Place the files in your WordPress plugin or theme directory.\n3. Include the library in your code:\n\n   ```php\n   require_once __DIR__ . '/path-to-easy-metabox/get-easy-metabox.php';\n   ```\n\n## Basic Usage\n\nHere's a simple example of how to create a meta box:\n\n```php\n// Create a new meta box\n$metabox = getEasyMetabox(\n    'my_meta_box',            // Unique identifier\n    'Additional Information', // Meta box title\n    'post'                    // Post type\n);\n\n// Add fields\n$metabox-\u003eadd_field('author_email', 'Author Email', 'email');\n$metabox-\u003eadd_field('publish_date', 'Publish Date', 'date');\n\n// Set up the meta box\n$metabox-\u003esetup_actions();\n```\n\n## Available Field Types\n\n- text\n- textarea\n- select\n- checkbox\n- radio\n- number\n- date\n- url\n- email\n- tel\n- password\n- hidden\n- color\n- file\n- wp_media\n\n## Field Configuration\n\nWhen adding fields, you can specify various options:\n\n```php\n$metabox-\u003eadd_field(\n    'field_id',          // Field ID\n    'Field Label',       // Field Label\n    'text',              // Field Type\n    [],                  // Options (for select, radio, checkbox)\n    [                    // HTML attributes\n        'class' =\u003e 'my-class',\n        'required' =\u003e true,\n        'placeholder' =\u003e 'Enter value'\n    ],\n    [                    // Additional options\n        'allow_quick_edit' =\u003e true,\n        'default' =\u003e 'Default value'\n    ]\n);\n```\n\n## Quick Edit Support\n\nTo enable Quick Edit support for a field:\n\n```php\n$metabox-\u003eadd_field(\n    'field_name',\n    'Field Label',\n    'text',\n    [],\n    [],\n    ['allow_quick_edit' =\u003e true]\n);\n```\n\n**Remember:** For the Quick Edit functionality to work, the field must also be added as a column in the post list table. You can do this by hooking into filters like `manage_{post_type}_posts_columns` to add the column and `manage_{post_type}_posts_custom_column` to output the field’s data. Without adding the field to the table, the Quick Edit option will not appear in your admin list.\n\n## WordPress Media Integration\n\nTo add a media upload field:\n\n```php\n$metabox-\u003eadd_field(\n    'image_field',\n    'Upload Image',\n    'wp_media',\n    [],\n    ['multiple' =\u003e true] // Allow multiple file selection\n);\n```\n\n## Retrieving Meta Values\n\n```php\n// Get a single field value\n$value = $metabox-\u003eget_field_value('field_id');\n\n// Get all meta values for a post\n$all_meta = $metabox-\u003eall_meta($post_id);\n```\n\n## Complete Example\n\nHere's a complete example showing how to create a meta box for a custom post type:\n\n```php\n\u003c?php\n/**\n * Example of integrating EasyMetabox with a Custom Post Type\n */\n\nrequire_once __DIR__ . '/path-to-easy-metabox/get-easy-metabox.php';\n\n// Define post type constant\nconst PRODUCT_POST_TYPE = 'product';\n\n/**\n * Register the Product custom post type\n */\nfunction register_product_post_type(): void {\n    $labels = array(\n        'name'               =\u003e 'Products',\n        'singular_name'      =\u003e 'Product',\n        'menu_name'          =\u003e 'Products',\n        'add_new'            =\u003e 'Add New',\n        'add_new_item'       =\u003e 'Add New Product',\n        'edit_item'          =\u003e 'Edit Product',\n        'new_item'           =\u003e 'New Product',\n        'view_item'          =\u003e 'View Product',\n        'search_items'       =\u003e 'Search Products',\n        'not_found'          =\u003e 'No products found',\n        'not_found_in_trash' =\u003e 'No products found in Trash'\n    );\n\n    $args = array(\n        'labels'             =\u003e $labels,\n        'public'             =\u003e true,\n        'publicly_queryable' =\u003e true,\n        'show_ui'            =\u003e true,\n        'show_in_menu'       =\u003e true,\n        'query_var'          =\u003e true,\n        'rewrite'            =\u003e array('slug' =\u003e 'products'),\n        'capability_type'    =\u003e 'post',\n        'has_archive'        =\u003e true,\n        'hierarchical'       =\u003e false,\n        'menu_position'      =\u003e 5,\n        'menu_icon'          =\u003e 'dashicons-cart',\n        'supports'           =\u003e array('title', 'editor', 'thumbnail')\n    );\n\n    register_post_type(PRODUCT_POST_TYPE, $args);\n}\n\n// Register the custom post type\nadd_action('init', 'register_product_post_type');\n\n/**\n * Create and configure the product meta box\n */\nfunction add_product_metabox(): void {\n    global $product_metabox;\n    \n    $product_metabox = getEasyMetabox('product_details', 'Product Details', PRODUCT_POST_TYPE);\n\n    $product_metabox-\u003eadd_field('price', 'Price', 'number', [], [\n        'required' =\u003e true,\n        'min' =\u003e 0,\n        'step' =\u003e 0.01\n    ]);\n\n    $product_metabox-\u003eadd_field('category', 'Category', 'select', [\n        'electronics' =\u003e 'Electronics',\n        'clothing' =\u003e 'Clothing',\n        'books' =\u003e 'Books'\n    ]);\n\n    $product_metabox-\u003eadd_field('images', 'Product Images', 'wp_media', [], [\n        'multiple' =\u003e true\n    ]);\n\n    $product_metabox-\u003esetup_actions();\n}\n\nadd_action('admin_init', 'add_product_metabox');\n\n**\n * Handle saving the meta box data\n */\nfunction save_product_meta($post_id): bool {\n    global $product_metabox;\n\n    try {\n        if (defined('DOING_AUTOSAVE') \u0026\u0026 DOING_AUTOSAVE) {\n            return false;\n        }\n\n        if (!current_user_can('edit_post', $post_id)) {\n            return false;\n        }\n\n        return $product_metabox-\u003esave($post_id);\n    } catch (\\Throwable $th) {\n        return false;\n    }\n}\n\n// Hook into saving the post type\nadd_action(\"save_post_\" . PRODUCT_POST_TYPE, 'save_product_meta');\n\n/**\n * Optional: Display product meta data on the front end\n */\nfunction display_product_meta($content) {\n    global $product_metabox;\n    \n    // Only modify product post type content\n    if (!is_singular(PRODUCT_POST_TYPE)) {\n        return $content;\n    }\n\n    $post_id = get_the_ID();\n    $price = $product_metabox-\u003eget_field_value('price', $post_id);\n    $category = $product_metabox-\u003eget_field_value('category', $post_id);\n    $images = $product_metabox-\u003eget_field_value('images', $post_id, false); // false to get array of images\n\n    $meta_html = '\u003cdiv class=\"product-meta\"\u003e';\n    $meta_html .= '\u003cp class=\"price\"\u003ePrice: $' . esc_html($price) . '\u003c/p\u003e';\n    $meta_html .= '\u003cp class=\"category\"\u003eCategory: ' . esc_html($category) . '\u003c/p\u003e';\n    \n    if (!empty($images)) {\n        $meta_html .= '\u003cdiv class=\"product-gallery\"\u003e';\n        foreach ($images as $image_url) {\n            $meta_html .= '\u003cimg src=\"' . esc_url($image_url) . '\" alt=\"Product Image\"\u003e';\n        }\n        $meta_html .= '\u003c/div\u003e';\n    }\n    \n    $meta_html .= '\u003c/div\u003e';\n\n    return $content . $meta_html;\n}\n\n// Add meta data to the content\nadd_filter('the_content', 'display_product_meta');\n```\n\n## Validation\n\nThe library includes built-in validation for field types. You can show admin errors by enabling them:\n\n```php\n$metabox-\u003eset_show_admin_error(true);\n```\n\n## Prefix Support\n\nYou can set a prefix for all field IDs:\n\n```php\n$metabox-\u003eset_prefix('my_prefix_');\n```\n\n## Custom Input Types\n\nYou can add custom input types:\n\n```php\n$metabox-\u003eset_input_type_html('custom_type', function($id, $data) {\n    // Return HTML string for custom input type\n    return \"\u003cinput type='text' id='{$id}' name='{$id}' /\u003e\";\n});\n```\n## License\n\nThis project is licensed under the GPL-2.0+ License.\n\n## Author\n\nCreated by [Codad5](https://codad5.me)\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodad5%2Feasy-metabox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodad5%2Feasy-metabox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodad5%2Feasy-metabox/lists"}