Advertisement
ohlookitsdany90

Custom Post Type

Jul 7th, 2022
1,195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.36 KB | None | 0 0
  1. <?php
  2. function cptui_register_my_cpts() {
  3.  
  4.     /**
  5.      * Post Type: Case Studies.
  6.      */
  7.  
  8.     $labels = [
  9.         "name" => __( "Case Studies", "custom-post-type-ui" ),
  10.         "singular_name" => __( "Case Study", "custom-post-type-ui" ),
  11.         "menu_name" => __( "My Case Studies", "custom-post-type-ui" ),
  12.         "all_items" => __( "All Case Studies", "custom-post-type-ui" ),
  13.         "add_new" => __( "Add new", "custom-post-type-ui" ),
  14.         "add_new_item" => __( "Add new Case Study", "custom-post-type-ui" ),
  15.         "edit_item" => __( "Edit Case Study", "custom-post-type-ui" ),
  16.         "new_item" => __( "New Case Study", "custom-post-type-ui" ),
  17.         "view_item" => __( "View Case Study", "custom-post-type-ui" ),
  18.         "view_items" => __( "View Case Studies", "custom-post-type-ui" ),
  19.         "search_items" => __( "Search Case Studies", "custom-post-type-ui" ),
  20.         "not_found" => __( "No Case Studies found", "custom-post-type-ui" ),
  21.         "not_found_in_trash" => __( "No Case Studies found in trash", "custom-post-type-ui" ),
  22.         "parent" => __( "Parent Case Study:", "custom-post-type-ui" ),
  23.         "featured_image" => __( "Featured image for this Case Study", "custom-post-type-ui" ),
  24.         "set_featured_image" => __( "Set featured image for this Case Study", "custom-post-type-ui" ),
  25.         "remove_featured_image" => __( "Remove featured image for this Case Study", "custom-post-type-ui" ),
  26.         "use_featured_image" => __( "Use as featured image for this Case Study", "custom-post-type-ui" ),
  27.         "archives" => __( "Case Study archives", "custom-post-type-ui" ),
  28.         "insert_into_item" => __( "Insert into Case Study", "custom-post-type-ui" ),
  29.         "uploaded_to_this_item" => __( "Upload to this Case Study", "custom-post-type-ui" ),
  30.         "filter_items_list" => __( "Filter Case Studies list", "custom-post-type-ui" ),
  31.         "items_list_navigation" => __( "Case Studies list navigation", "custom-post-type-ui" ),
  32.         "items_list" => __( "Case Studies list", "custom-post-type-ui" ),
  33.         "attributes" => __( "Case Studies attributes", "custom-post-type-ui" ),
  34.         "name_admin_bar" => __( "Case Study", "custom-post-type-ui" ),
  35.         "item_published" => __( "Case Study published", "custom-post-type-ui" ),
  36.         "item_published_privately" => __( "Case Study published privately.", "custom-post-type-ui" ),
  37.         "item_reverted_to_draft" => __( "Case Study reverted to draft.", "custom-post-type-ui" ),
  38.         "item_scheduled" => __( "Case Study scheduled", "custom-post-type-ui" ),
  39.         "item_updated" => __( "Case Study updated.", "custom-post-type-ui" ),
  40.         "parent_item_colon" => __( "Parent Case Study:", "custom-post-type-ui" ),
  41.     ];
  42.  
  43.     $args = [
  44.         "label" => __( "Case Studies", "custom-post-type-ui" ),
  45.         "labels" => $labels,
  46.         "description" => "",
  47.         "public" => true,
  48.         "publicly_queryable" => true,
  49.         "show_ui" => true,
  50.         "show_in_rest" => true,
  51.         "rest_base" => "",
  52.         "rest_controller_class" => "WP_REST_Posts_Controller",
  53.         "rest_namespace" => "wp/v2",
  54.         "has_archive" => true,
  55.         "show_in_menu" => true,
  56.         "show_in_nav_menus" => true,
  57.         "delete_with_user" => false,
  58.         "exclude_from_search" => false,
  59.         "capability_type" => "post",
  60.         "map_meta_cap" => true,
  61.         "hierarchical" => false,
  62.         "can_export" => false,
  63.         "rewrite" => [ "slug" => "case-studies", "with_front" => true ],
  64.         "query_var" => true,
  65.         "menu_position" => 5,
  66.         "menu_icon" => "dashicons-clipboard",
  67.         "supports" => [ "title", "editor", "thumbnail", "revisions" ],
  68.         "taxonomies" => [ "program" ],
  69.         "show_in_graphql" => false,
  70.     ];
  71.  
  72.     register_post_type( "case-studies", $args );
  73. }
  74.  
  75. add_action( 'init', 'cptui_register_my_cpts' );
  76.  
  77. function cptui_register_my_taxes() {
  78.  
  79.     /**
  80.      * Taxonomy: Case Studies Types.
  81.      */
  82.  
  83.     $labels = [
  84.         "name" => __( "Case Studies Types", "custom-post-type-ui" ),
  85.         "singular_name" => __( "Case Study Type", "custom-post-type-ui" ),
  86.         "menu_name" => __( "Case Studies Types", "custom-post-type-ui" ),
  87.         "all_items" => __( "All Case Studies Types", "custom-post-type-ui" ),
  88.         "edit_item" => __( "Edit Case Study Type", "custom-post-type-ui" ),
  89.         "view_item" => __( "View Case Study Type", "custom-post-type-ui" ),
  90.         "update_item" => __( "Update Case Study Type name", "custom-post-type-ui" ),
  91.         "add_new_item" => __( "Add new Case Study Type", "custom-post-type-ui" ),
  92.         "new_item_name" => __( "New Case Study Type name", "custom-post-type-ui" ),
  93.         "parent_item" => __( "Parent Case Study Type", "custom-post-type-ui" ),
  94.         "parent_item_colon" => __( "Parent Case Study Type:", "custom-post-type-ui" ),
  95.         "search_items" => __( "Search Case Studies Types", "custom-post-type-ui" ),
  96.         "popular_items" => __( "Popular Case Studies Types", "custom-post-type-ui" ),
  97.         "separate_items_with_commas" => __( "Separate Case Studies Types with commas", "custom-post-type-ui" ),
  98.         "add_or_remove_items" => __( "Add or remove Case Studies Types", "custom-post-type-ui" ),
  99.         "choose_from_most_used" => __( "Choose from the most used Case Studies Types", "custom-post-type-ui" ),
  100.         "not_found" => __( "No Case Studies Types found", "custom-post-type-ui" ),
  101.         "no_terms" => __( "No Case Studies Types", "custom-post-type-ui" ),
  102.         "items_list_navigation" => __( "Case Studies Types list navigation", "custom-post-type-ui" ),
  103.         "items_list" => __( "Case Studies Types list", "custom-post-type-ui" ),
  104.         "back_to_items" => __( "Back to Case Studies Types", "custom-post-type-ui" ),
  105.         "name_field_description" => __( "The name is how it appears on your site.", "custom-post-type-ui" ),
  106.         "parent_field_description" => __( "Assign a parent term to create a hierarchy. The term Jazz, for example, would be the parent of Bebop and Big Band.", "custom-post-type-ui" ),
  107.         "slug_field_description" => __( "The slug is the URL-friendly version of the name. It is usually all lowercase and contains only letters, numbers, and hyphens.", "custom-post-type-ui" ),
  108.         "desc_field_description" => __( "The description is not prominent by default; however, some themes may show it.", "custom-post-type-ui" ),
  109.     ];
  110.  
  111.    
  112.     $args = [
  113.         "label" => __( "Case Studies Types", "custom-post-type-ui" ),
  114.         "labels" => $labels,
  115.         "public" => true,
  116.         "publicly_queryable" => true,
  117.         "hierarchical" => true,
  118.         "show_ui" => true,
  119.         "show_in_menu" => true,
  120.         "show_in_nav_menus" => true,
  121.         "query_var" => true,
  122.         "rewrite" => [ 'slug' => 'program', 'with_front' => true, ],
  123.         "show_admin_column" => false,
  124.         "show_in_rest" => true,
  125.         "show_tagcloud" => false,
  126.         "rest_base" => "program",
  127.         "rest_controller_class" => "WP_REST_Terms_Controller",
  128.         "rest_namespace" => "wp/v2",
  129.         "show_in_quick_edit" => false,
  130.         "sort" => false,
  131.         "show_in_graphql" => false,
  132.     ];
  133.     register_taxonomy( "program", [ "case-studies" ], $args );
  134. }
  135. add_action( 'init', 'cptui_register_my_taxes' );
  136.  
  137. function custom_meta_box_markup($object)
  138. {
  139.  
  140.     wp_nonce_field(basename(__FILE__), "meta-box-nonce");
  141.  
  142.     ?>
  143.         <div>
  144.             <p>
  145.                 <strong>Location or Manufacturer</strong>
  146.                 <input name="location_text" type="text" value="<?php echo esc_attr(get_post_meta($object->ID, 'location_text', true)); ?>">
  147.             </p>
  148.            
  149.             <p>
  150.                 <strong>LBC & LPC Typology</strong>
  151.             <select name="typo-dropdown">
  152.                 <?php
  153.                     $option_values = array("","Building","Infrastructure + Landscape","Renovation","Consumer","Personal Care");
  154.  
  155.                     foreach($option_values as $key => $value)
  156.                     {
  157.                         if($value == get_post_meta($object->ID, "typo-dropdown", true))
  158.                         {
  159.                             ?>
  160.                                 <option selected><?php echo $value; ?></option>
  161.                             <?php    
  162.                         }
  163.                         else
  164.                         {
  165.                             ?>
  166.                                 <option><?php echo $value; ?></option>
  167.                             <?php
  168.                         }
  169.                     }
  170.                 ?>
  171.             </select>
  172.             </p>
  173.  
  174.             <p>
  175.             <strong>Petals</strong><br/>
  176.  
  177.             <?php
  178.                 $checkbox_value = get_post_meta($object->ID, "place-checkbox", true);
  179.  
  180.                 if($checkbox_value == "")
  181.                 {
  182.                     ?>
  183.                         <input name="place-checkbox" type="checkbox" value="true">
  184.                     <?php
  185.                 }
  186.                 else if($checkbox_value == "true")
  187.                 {
  188.                     ?>  
  189.                         <input name="place-checkbox" type="checkbox" value="true" checked>
  190.                     <?php
  191.                 }
  192.             ?>
  193.             <label for="place-checkbox">Place</label>          
  194.             <br/>
  195.  
  196.             <?php
  197.                 $checkbox_value = get_post_meta($object->ID, "water-checkbox", true);
  198.  
  199.                 if($checkbox_value == "")
  200.                 {
  201.                     ?>
  202.                         <input name="water-checkbox" type="checkbox" value="true">
  203.                     <?php
  204.                 }
  205.                 else if($checkbox_value == "true")
  206.                 {
  207.                     ?>  
  208.                         <input name="water-checkbox" type="checkbox" value="true" checked>
  209.                     <?php
  210.                 }
  211.             ?>
  212.             <label for="water-checkbox">Water</label>          
  213.             <br/>
  214.            
  215.             <?php
  216.                 $checkbox_value = get_post_meta($object->ID, "energy-checkbox", true);
  217.  
  218.                 if($checkbox_value == "")
  219.                 {
  220.                     ?>
  221.                         <input name="energy-checkbox" type="checkbox" value="true">
  222.                     <?php
  223.                 }
  224.                 else if($checkbox_value == "true")
  225.                 {
  226.                     ?>  
  227.                         <input name="energy-checkbox" type="checkbox" value="true" checked>
  228.                     <?php
  229.                 }
  230.             ?>
  231.             <label for="energy-checkbox">Energy</label>        
  232.             <br/>
  233.  
  234.             <?php
  235.                 $checkbox_value = get_post_meta($object->ID, "health-checkbox", true);
  236.  
  237.                 if($checkbox_value == "")
  238.                 {
  239.                     ?>
  240.                         <input name="health-checkbox" type="checkbox" value="true">
  241.                     <?php
  242.                 }
  243.                 else if($checkbox_value == "true")
  244.                 {
  245.                     ?>  
  246.                         <input name="health-checkbox" type="checkbox" value="true" checked>
  247.                     <?php
  248.                 }
  249.             ?>
  250.             <label for="health-checkbox">Health + Happiness</label>        
  251.             <br/>
  252.  
  253.             <?php
  254.                 $checkbox_value = get_post_meta($object->ID, "materials-checkbox", true);
  255.  
  256.                 if($checkbox_value == "")
  257.                 {
  258.                     ?>
  259.                         <input name="materials-checkbox" type="checkbox" value="true">
  260.                     <?php
  261.                 }
  262.                 else if($checkbox_value == "true")
  263.                 {
  264.                     ?>  
  265.                         <input name="materials-checkbox" type="checkbox" value="true" checked>
  266.                     <?php
  267.                 }
  268.             ?>
  269.             <label for="materials-checkbox">Materials</label>          
  270.             <br/>
  271.  
  272.             <?php
  273.                 $checkbox_value = get_post_meta($object->ID, "equity-checkbox", true);
  274.  
  275.                 if($checkbox_value == "")
  276.                 {
  277.                     ?>
  278.                         <input name="equity-checkbox" type="checkbox" value="true">
  279.                     <?php
  280.                 }
  281.                 else if($checkbox_value == "true")
  282.                 {
  283.                     ?>  
  284.                         <input name="equity-checkbox" type="checkbox" value="true" checked>
  285.                     <?php
  286.                 }
  287.             ?>
  288.             <label for="equity-checkbox">Equity</label>        
  289.             <br/>
  290.  
  291.             <?php
  292.                 $checkbox_value = get_post_meta($object->ID, "beauty-checkbox", true);
  293.  
  294.                 if($checkbox_value == "")
  295.                 {
  296.                     ?>
  297.                         <input name="beauty-checkbox" type="checkbox" value="true">
  298.                     <?php
  299.                 }
  300.                 else if($checkbox_value == "true")
  301.                 {
  302.                     ?>  
  303.                         <input name="beauty-checkbox" type="checkbox" value="true" checked>
  304.                     <?php
  305.                 }
  306.             ?>
  307.             <label for="beauty-checkbox">Beauty</label>        
  308.             <br/>
  309.             </p>
  310.             <p>
  311.                 <strong>Certification Type</strong>
  312.             <select name="cert-type-dropdown">
  313.                 <?php
  314.                     $option_values = array("","Full Living Certified","Petal Certified","Net Zero Energy Certified","Zero Energy Certified","Zero Carbon Certified","Imperative Certified");
  315.  
  316.                     foreach($option_values as $key => $value)
  317.                     {
  318.                         if($value == get_post_meta($object->ID, "cert-type-dropdown", true))
  319.                         {
  320.                             ?>
  321.                                 <option selected><?php echo $value; ?></option>
  322.                             <?php    
  323.                         }
  324.                         else
  325.                         {
  326.                             ?>
  327.                                 <option><?php echo $value; ?></option>
  328.                             <?php
  329.                         }
  330.                     }
  331.                 ?>
  332.             </select>
  333.             </p>
  334.             <p>
  335.                 <strong>Square Footage</strong>
  336.             <select name="sqfoot-dropdown">
  337.                 <?php
  338.                     $option_values = array("","N/A","< 1,000","1,000 - 10,000","10,000 - 50,000","50,000 - 100,000","> 150,000");
  339.  
  340.                     foreach($option_values as $key => $value)
  341.                     {
  342.                         if($value == get_post_meta($object->ID, "sqfoot-dropdown", true))
  343.                         {
  344.                             ?>
  345.                                 <option selected><?php echo $value; ?></option>
  346.                             <?php    
  347.                         }
  348.                         else
  349.                         {
  350.                             ?>
  351.                                 <option><?php echo $value; ?></option>
  352.                             <?php
  353.                         }
  354.                     }
  355.                 ?>
  356.             </select>
  357.             </p>
  358.             <p>
  359.             <strong>Biophilic Design Elements</strong><br/>
  360.  
  361.             <?php
  362.                 $checkbox_value = get_post_meta($object->ID, "ef-checkbox", true);
  363.  
  364.                 if($checkbox_value == "")
  365.                 {
  366.                     ?>
  367.                         <input name="ef-checkbox" type="checkbox" value="true">
  368.                     <?php
  369.                 }
  370.                 else if($checkbox_value == "true")
  371.                 {
  372.                     ?>  
  373.                         <input name="ef-checkbox" type="checkbox" value="true" checked>
  374.                     <?php
  375.                 }
  376.             ?>
  377.             <label for="ef-checkbox">Environmental Feature</label>         
  378.             <br/>
  379.  
  380.             <?php
  381.                 $checkbox_value = get_post_meta($object->ID, "nsf-checkbox", true);
  382.  
  383.                 if($checkbox_value == "")
  384.                 {
  385.                     ?>
  386.                         <input name="nsf-checkbox" type="checkbox" value="true">
  387.                     <?php
  388.                 }
  389.                 else if($checkbox_value == "true")
  390.                 {
  391.                     ?>  
  392.                         <input name="nsf-checkbox" type="checkbox" value="true" checked>
  393.                     <?php
  394.                 }
  395.             ?>
  396.             <label for="nsf-checkbox">Natural Shapes & Forms</label>           
  397.             <br/>
  398.  
  399.             <?php
  400.                 $checkbox_value = get_post_meta($object->ID, "npp-checkbox", true);
  401.  
  402.                 if($checkbox_value == "")
  403.                 {
  404.                     ?>
  405.                         <input name="npp-checkbox" type="checkbox" value="true">
  406.                     <?php
  407.                 }
  408.                 else if($checkbox_value == "true")
  409.                 {
  410.                     ?>  
  411.                         <input name="npp-checkbox" type="checkbox" value="true" checked>
  412.                     <?php
  413.                 }
  414.             ?>
  415.             <label for="npp-checkbox">Natural Patterns & Processes</label>         
  416.             <br/>
  417.  
  418.             <?php
  419.                 $checkbox_value = get_post_meta($object->ID, "ls-checkbox", true);
  420.  
  421.                 if($checkbox_value == "")
  422.                 {
  423.                     ?>
  424.                         <input name="ls-checkbox" type="checkbox" value="true">
  425.                     <?php
  426.                 }
  427.                 else if($checkbox_value == "true")
  428.                 {
  429.                     ?>  
  430.                         <input name="ls-checkbox" type="checkbox" value="true" checked>
  431.                     <?php
  432.                 }
  433.             ?>
  434.             <label for="ls-checkbox">Light & Space</label>         
  435.             <br/>
  436.  
  437.             <?php
  438.                 $checkbox_value = get_post_meta($object->ID, "pbr-checkbox", true);
  439.  
  440.                 if($checkbox_value == "")
  441.                 {
  442.                     ?>
  443.                         <input name="pbr-checkbox" type="checkbox" value="true">
  444.                     <?php
  445.                 }
  446.                 else if($checkbox_value == "true")
  447.                 {
  448.                     ?>  
  449.                         <input name="pbr-checkbox" type="checkbox" value="true" checked>
  450.                     <?php
  451.                 }
  452.             ?>
  453.             <label for="pbr-checkbox">Place-Based Relationships</label>        
  454.             <br/>
  455.  
  456.             <?php
  457.                 $checkbox_value = get_post_meta($object->ID, "hnr-checkbox", true);
  458.  
  459.                 if($checkbox_value == "")
  460.                 {
  461.                     ?>
  462.                         <input name="hnr-checkbox" type="checkbox" value="true">
  463.                     <?php
  464.                 }
  465.                 else if($checkbox_value == "true")
  466.                 {
  467.                     ?>  
  468.                         <input name="hnr-checkbox" type="checkbox" value="true" checked>
  469.                     <?php
  470.                 }
  471.             ?>
  472.             <label for="hnr-checkbox">Evolved Human-Nature Relationships</label>           
  473.             <br/>          
  474.             <p>
  475.                 <strong>Transect</strong><br />
  476.             <select name="transect-dropdown">
  477.                 <?php
  478.                     $option_values = array("","L1. Natural Habitat Preserve","L2. Rural Agricultural Zone","L3. Village or Campus Zone","L4. General Urban Zone","L5. Urban Center Zone","L6. Urban Core Zone");
  479.  
  480.                     foreach($option_values as $key => $value)
  481.                     {
  482.                         if($value == get_post_meta($object->ID, "transect-dropdown", true))
  483.                         {
  484.                             ?>
  485.                                 <option selected><?php echo $value; ?></option>
  486.                             <?php    
  487.                         }
  488.                         else
  489.                         {
  490.                             ?>
  491.                                 <option><?php echo $value; ?></option>
  492.                             <?php
  493.                         }
  494.                     }
  495.                 ?>
  496.             </select>
  497.             </p>
  498.             <p>
  499.                 <strong>Standard Version</strong><br />
  500.             <select name="standard-dropdown">
  501.                 <?php
  502.                     $option_values = array("","LPC 1.0","LBC 1.3","LBC 2.0","LBC 2.1","LBC 3.0","LBC 3.1","LBC 4.0");
  503.  
  504.                     foreach($option_values as $key => $value)
  505.                     {
  506.                         if($value == get_post_meta($object->ID, "standard-dropdown", true))
  507.                         {
  508.                             ?>
  509.                                 <option selected><?php echo $value; ?></option>
  510.                             <?php    
  511.                         }
  512.                         else
  513.                         {
  514.                             ?>
  515.                                 <option><?php echo $value; ?></option>
  516.                             <?php
  517.                         }
  518.                     }
  519.                 ?>
  520.             </select>
  521.             </p>           
  522.         </div>
  523.     <?php      
  524.  
  525. }
  526. function add_custom_meta_box()
  527. {
  528.     add_meta_box("demo-meta-box", "Case Study Options", "custom_meta_box_markup", "case-studies", "side", "high", null);
  529. }
  530.  
  531. add_action("add_meta_boxes", "add_custom_meta_box");
  532.  
  533. function save_custom_meta_box($post_id, $post, $update)
  534. {
  535.     if (!isset($_POST['meta-box-nonce']) || !wp_verify_nonce($_POST['meta-box-nonce'], basename(__FILE__)))
  536.         return $post_id;
  537.  
  538.     if(!current_user_can("edit_post", $post_id))
  539.         return $post_id;
  540.  
  541.     if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
  542.         return $post_id;
  543.  
  544.     $slug = "post";
  545.     if($slug != $post->post_type)
  546.         return $post_id;
  547.  
  548.     $location_text_value = "";
  549.     $typo_dropdown_value = "";
  550.     $place_checkbox_value = "";
  551.  
  552.  
  553.     if(isset($_POST['location_text']))
  554.     {
  555.         $typo_dropdown_value = $_POST['location_text'];
  556.     }  
  557.     update_post_meta($post_id, 'location_text', $location_text_value);
  558.    
  559.     if(isset($_POST['typo-dropdown']))
  560.     {
  561.         $typo_dropdown_value = $_POST['typo-dropdown'];
  562.     }  
  563.     update_post_meta($post_id, 'typo-dropdown', $typo_dropdown_value);
  564.  
  565.     if(isset($_POST['place-checkbox']))
  566.     {
  567.         $place_checkbox_value = $_POST['place-checkbox'];
  568.     }  
  569.     update_post_meta($post_id, 'place-checkbox', $place_checkbox_value);
  570. }
  571.  
  572. add_action("save_post", "save_custom_meta_box", 10, 3);
  573. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement