diff --git a/blueprints/dynamic-cpt-and-tax-generator/Screenshot.png b/blueprints/dynamic-cpt-and-tax-generator/Screenshot.png new file mode 100644 index 00000000..bf947f5c Binary files /dev/null and b/blueprints/dynamic-cpt-and-tax-generator/Screenshot.png differ diff --git a/blueprints/dynamic-cpt-and-tax-generator/admin.js b/blueprints/dynamic-cpt-and-tax-generator/admin.js new file mode 100644 index 00000000..844de275 --- /dev/null +++ b/blueprints/dynamic-cpt-and-tax-generator/admin.js @@ -0,0 +1,7 @@ +jQuery(document).ready(function($){ + $('#dcpt_action').change(function(){ + $('#new_cpt_fields, #add_taxonomy_fields').hide(); + if($(this).val()=='new_cpt') $('#new_cpt_fields').show(); + if($(this).val()=='add_taxonomy') $('#add_taxonomy_fields').show(); + }); +}); diff --git a/blueprints/dynamic-cpt-and-tax-generator/blueprint.json b/blueprints/dynamic-cpt-and-tax-generator/blueprint.json new file mode 100644 index 00000000..1ce96f50 --- /dev/null +++ b/blueprints/dynamic-cpt-and-tax-generator/blueprint.json @@ -0,0 +1,35 @@ +{ + "$schema": "https://playground.wordpress.net/blueprint-schema.json", + "meta": { + "title": "Dynamic CPT & Taxonomy Generator", + "description": "Create new CPTs with optional taxonomy or add taxonomy to existing CPTs. All managed from a single plugin.", + "author": "Rima Prajapati", + "categories": [ + "Content", + "CPT", + "Admin" + ] + }, + "landingPage": "/wp-admin/admin.php?page=dynamic-cpt-generator", + "login": true, + "steps": [ + { + "step": "installPlugin", + "pluginData": { + "resource": "git:directory", + "url": "https://github.com/WordPress/blueprints", + "ref": "trunk", + "refType": "branch", + "path": "blueprints/dynamic-cpt-and-tax-generator" + }, + "options": { + "activate": true + } + } + ], + "preferredVersions": { + "php": "8.3", + "wp": "latest" + }, + "features": {} +} diff --git a/blueprints/dynamic-cpt-and-tax-generator/dynamic-cpt-and-tax-generator.php b/blueprints/dynamic-cpt-and-tax-generator/dynamic-cpt-and-tax-generator.php new file mode 100644 index 00000000..2e5aeca7 --- /dev/null +++ b/blueprints/dynamic-cpt-and-tax-generator/dynamic-cpt-and-tax-generator.php @@ -0,0 +1,221 @@ + +
+

Dynamic CPT & Taxonomy Generator

+ + errors as $err) { + echo '

'.esc_html($err).'

'; + } + // Display success messages + foreach($this->success as $msg) { + echo '

'.esc_html($msg).'

'; + } + ?> + +
+ + +

Choose Action

+ + + + + + + +
+
+ handle_form(); + } + } + + private function handle_form() { + $action = sanitize_text_field($_POST['dcpt_action']); + $cpts = get_option('dcpt_saved_cpts', array()); + + if($action == 'new_cpt') { + $cpt_slug = sanitize_text_field($_POST['cpt_slug']); + $cpt_name = sanitize_text_field($_POST['cpt_name']); + $tax_slug = sanitize_text_field($_POST['tax_slug']); + $tax_name = sanitize_text_field($_POST['tax_name']); + $cpt_icon = sanitize_text_field($_POST['cpt_icon']); + + // Validation + if(empty($cpt_slug)) $this->errors[] = "CPT slug is required."; + elseif(!preg_match('/^[a-z0-9_-]+$/', $cpt_slug)) $this->errors[] = "CPT slug can only contain lowercase letters, numbers, dashes, and underscores."; + if(post_type_exists($cpt_slug)) $this->errors[] = "CPT slug '{$cpt_slug}' already exists."; + + if(empty($cpt_name)) $this->errors[] = "CPT name is required."; + + if(empty($this->errors)) { + $cpts[$cpt_slug] = array( + 'name'=>$cpt_name, + 'icon'=>$cpt_icon, + 'taxonomy'=>array( + 'slug'=>$tax_slug, + 'name'=>$tax_name + ) + ); + update_option('dcpt_saved_cpts', $cpts); + $this->success[] = "CPT '{$cpt_name}' saved successfully."; + } + } + + if($action == 'add_taxonomy') { + $existing_cpt = sanitize_text_field($_POST['existing_cpt']); + $existing_tax_slug = sanitize_text_field($_POST['existing_tax_slug']); + $existing_tax_name = sanitize_text_field($_POST['existing_tax_name']); + + if(empty($existing_cpt)) $this->errors[] = "Please select a CPT."; + if(empty($existing_tax_slug)) $this->errors[] = "Taxonomy slug is required."; + elseif(taxonomy_exists($existing_tax_slug)) $this->errors[] = "Taxonomy slug '{$existing_tax_slug}' already exists."; + if(empty($existing_tax_name)) $this->errors[] = "Taxonomy name is required."; + + if(empty($this->errors)) { + if(!isset($cpts[$existing_cpt])) $cpts[$existing_cpt] = array('name'=>$existing_cpt); + $cpts[$existing_cpt]['taxonomy'] = array( + 'slug'=>$existing_tax_slug, + 'name'=>$existing_tax_name + ); + update_option('dcpt_saved_cpts', $cpts); + $this->success[] = "Taxonomy '{$existing_tax_name}' added to CPT '{$existing_cpt}'."; + } + } + } + + public function register_saved_cpts() { + $cpts = get_option('dcpt_saved_cpts', array()); + if(!empty($cpts)) { + foreach($cpts as $slug => $data) { + // Register CPT + if(!post_type_exists($slug)) { + register_post_type($slug, array( + 'labels'=>array('name'=>$data['name'],'singular_name'=>$data['name']), + 'public'=>true, + 'has_archive'=>true, + 'show_in_rest'=>true, + 'menu_icon'=> !empty($data['icon']) ? $data['icon'] : 'dashicons-admin-post', + 'supports'=>array('title','editor','thumbnail') + )); + } + + // Register taxonomy + if(!empty($data['taxonomy']['slug']) && !taxonomy_exists($data['taxonomy']['slug'])) { + register_taxonomy($data['taxonomy']['slug'], $slug, array( + 'labels'=>array('name'=>$data['taxonomy']['name'],'singular_name'=>$data['taxonomy']['name']), + 'public'=>true, + 'hierarchical'=>true, + 'show_in_rest'=>true + )); + } + } + } + } +} + +new Dynamic_CPT_Taxonomy_Generator();