Advertisement
MattCampbell95

Custom Post Type generation with Advanced Custom Fields

Dec 27th, 2022 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.65 KB | None | 0 0
  1. <?php
  2.  
  3. if( have_rows('custom_post_types', 'option') ):
  4.     while( have_rows('custom_post_types', 'option') ) : the_row();
  5.  
  6.     // ACF Custom Label Declarations
  7.  
  8.     $cpt_name_plural = '"name" => ' . esc_html__( '"' . get_sub_field('cpt_name_plural') . '"' );
  9.     $cpt_name_single = '"singular_name" => ' . esc_html__( '"' . get_sub_field('cpt_name_single') . '"' );
  10.     $cpt_label_add_new = '"add_new" => ' . esc_html__( '"' . get_sub_field('cpt_label_add_new') . '"' );
  11.     $cpt_label_add_new_item = '"add_new_item" => ' . esc_html__( '"' . get_sub_field('cpt_label_add_new_item') . '"' );
  12.     $cpt_label_edit_item = '"edit_item" => ' . esc_html__( '"' . get_sub_field('cpt_label_edit_item') . '"' );
  13.     $cpt_label_new_item = '"new_item" => ' . esc_html__( '"' . get_sub_field('cpt_label_new_item') . '"' );
  14.     $cpt_label_view_item = '"view_item" => ' . esc_html__( '"' . get_sub_field('cpt_label_view_item') . '"' );
  15.     $cpt_label_search_items = '"search_items" => ' . esc_html__( '"' . get_sub_field('cpt_label_search_items') . '"' );
  16.     $cpt_label_not_found = '"not_found" => ' . esc_html__( '"' . get_sub_field('cpt_label_not_found') . '"' );
  17.     $cpt_label_not_found_in_trash = '"not_found_in_trash" => ' . esc_html__( '"' . get_sub_field('cpt_label_not_found_in_trash') . '"' );
  18.     $cpt_label_parent_item_colon = '"parent_item_colon" => ' . esc_html__( '"' . get_sub_field('cpt_label_parent_item_colon') . '"' );
  19.     $cpt_label_all_items = '"all_items" => ' . esc_html__( '"' . get_sub_field('cpt_label_all_items') . '"' );
  20.     $cpt_label_archives = '"archives" => ' . esc_html__( '"' . get_sub_field('cpt_label_archives') . '"' );
  21.     $cpt_label_attributes = '"attributes" => ' . esc_html__( '"' . get_sub_field('cpt_label_attributes') . '"' );
  22.     $cpt_label_insert_into_item = '"insert_into_item" => ' . esc_html__( '"' . get_sub_field('cpt_label_insert_into_item') . '"' );
  23.     $cpt_label_uploaded_to_this_item = '"uploaded_to_this_item" => ' . esc_html__( '"' . get_sub_field('cpt_label_uploaded_to_this_item') . '"' );
  24.     $cpt_label_featured_image = '"featured_image" => ' . esc_html__( '"' . get_sub_field('cpt_label_featured_image') . '"' );
  25.     $cpt_label_set_featured_image = '"set_featured_image" => ' . esc_html__( '"' . get_sub_field('cpt_label_set_featured_image') . '"' );
  26.     $cpt_label_remove_featured_image = '"remove_featured_image" => ' . esc_html__( '"' . get_sub_field('cpt_label_remove_featured_image') . '"' );
  27.     $cpt_label_use_featured_image = '"use_featured_image" => ' . esc_html__( '"' . get_sub_field('cpt_label_use_featured_image') . '"' );
  28.     $cpt_label_menu_name = '"menu_name" => ' . esc_html__( '"' . get_sub_field('cpt_label_menu_name') . '"' );
  29.     $cpt_label_filter_items_list = '"filter_items_list" => ' . esc_html__( '"' . get_sub_field('cpt_label_filter_items_list') . '"' );
  30.     $cpt_label_filter_by_date = '"filter_by_date" => ' . esc_html__( '"' . get_sub_field('cpt_label_filter_by_date') . '"' );
  31.     $cpt_label_items_list_navigation = '"items_list_navigation" => ' . esc_html__( '"' . get_sub_field('cpt_label_items_list_navigation') . '"' );
  32.     $cpt_label_items_list = '"items_list" => ' . esc_html__( '"' . get_sub_field('cpt_label_items_list') . '"' );
  33.     $cpt_label_item_published = '"item_published" => ' . esc_html__( '"' . get_sub_field('cpt_label_item_published') . '"' );
  34.     $cpt_label_item_published_privately = '"item_published_privately" => ' . esc_html__( '"' . get_sub_field('cpt_label_item_published_privately') . '"' );
  35.     $cpt_label_item_reverted_to_draft = '"item_reverted_to_draft" => ' . esc_html__( '"' . get_sub_field('cpt_label_item_reverted_to_draft') . '"' );
  36.     $cpt_label_item_scheduled = '"item_scheduled" => ' . esc_html__( '"' . get_sub_field('cpt_label_item_scheduled') . '"' );
  37.     $cpt_label_item_updated = '"item_updated" => ' . esc_html__( '"' . get_sub_field('cpt_label_item_updated') . '"' );
  38.     $cpt_label_item_link = '"item_link" => ' . esc_html__( '"' . get_sub_field('cpt_label_item_link') . '"' );
  39.     $cpt_label_item_link_description = '"item_link_description" => ' . esc_html__( '"' . get_sub_field('cpt_label_item_link_description') . '"' );
  40.  
  41.     // ACF Custom Args Declarations
  42.  
  43.     $arg_label = '"label" => "' . get_sub_field('cpt_name_plural') . '"';
  44.     $arg_description = '"description" => "' . get_sub_field('cpt_description') . '"';
  45.     $arg_has_archive = '"has_archive" => ' . get_sub_field('cpt_archive_page_requirement');
  46.     $arg_exclude_from_search = '"exclude_from_search" => ' . get_sub_field('cpt_search_results');
  47.     $arg_rewrite_slug = '"rewrite" => [ "slug" => "' . get_sub_field('cpt_slug') . '", "with_front" => true ]';
  48.     $arg_menu_position = '"menu_position" => ' . get_sub_field('cpt_menu_position');
  49.     $arg_menu_icon = '"menu_icon" => "' . get_sub_field('cpt_dashicon') . '"';
  50.  
  51.     // ACF Custom Args Declarations
  52.  
  53.     $register_slug = '"' . get_sub_field('cpt_slug') . '"';
  54.  
  55.     // Start the Custom Fields Loop
  56.  
  57.     function register_my_cpts() {
  58.         if( get_sub_field('cpt_custom_labels') ):
  59.             $labels = [
  60.                 $cpt_name_plural,
  61.                 $cpt_name_single,
  62.                 $cpt_label_add_new,
  63.                 $cpt_label_add_new_item,
  64.                 $cpt_label_edit_item,
  65.                 $cpt_label_new_item,
  66.                 $cpt_label_view_item,
  67.                 $cpt_label_search_items,
  68.                 $cpt_label_not_found,
  69.                 $cpt_label_not_found_in_trash,
  70.                 $cpt_label_parent_item_colon,
  71.                 $cpt_label_all_items,
  72.                 $cpt_label_archives,
  73.                 $cpt_label_attributes,
  74.                 $cpt_label_insert_into_item,
  75.                 $cpt_label_uploaded_to_this_item,
  76.                 $cpt_label_featured_image,
  77.                 $cpt_label_set_featured_image,
  78.                 $cpt_label_remove_featured_image,
  79.                 $cpt_label_use_featured_image,
  80.                 $cpt_label_menu_name,
  81.                 $cpt_label_filter_items_list,
  82.                 $cpt_label_filter_by_date,
  83.                 $cpt_label_items_list_navigation,
  84.                 $cpt_label_items_list,
  85.                 $cpt_label_item_published,
  86.                 $cpt_label_item_published_privately,
  87.                 $cpt_label_item_reverted_to_draft,
  88.                 $cpt_label_item_scheduled,
  89.                 $cpt_label_item_updated,
  90.                 $cpt_label_item_link,
  91.                 $cpt_label_item_link_description,
  92.             ];
  93.         else:
  94.             $labels = [
  95.                 "name" => esc_html__( "name" ),
  96.                 "singular_name" => esc_html__( "singular_name" ),
  97.                 "add_new" => esc_html__( "add_new" ),
  98.                 "add_new_item" => esc_html__( "add_new_item" ),
  99.                 "edit_item" => esc_html__( "edit_item" ),
  100.                 "new_item" => esc_html__( "new_item" ),
  101.                 "view_item" => esc_html__( "view_item" ),
  102.                 "search_items" => esc_html__( "search_items" ),
  103.                 "not_found" => esc_html__( "not_found" ),
  104.                 "not_found_in_trash" => esc_html__( "not_found_in_trash" ),
  105.                 "parent_item_colon" => esc_html__( "parent_item_colon" ),
  106.                 "all_items" => esc_html__( "all_items" ),
  107.                 "archives" => esc_html__( "archives" ),
  108.                 "attributes" => esc_html__( "attributes" ),
  109.                 "insert_into_item" => esc_html__("insert_into_item" ),
  110.                 "uploaded_to_this_item" => esc_html__( "uploaded_to_this_item" ),
  111.                 "featured_image" => esc_html__( "featured_image" ),
  112.                 "set_featured_image" => esc_html__( "set_featured_image" ),
  113.                 "remove_featured_image" => esc_html__( "remove_featured_image" ),
  114.                 "use_featured_image" => esc_html__( "use_featured_image" ),
  115.                 "menu_name" => esc_html__( "menu_name" ),
  116.                 "filter_items_list" => esc_html__( "filter_items_list" ),
  117.                 "filter_by_date" => esc_html__( "filter_by_date" ),
  118.                 "items_list_navigation" => esc_html__( "items_list_navigation" ),
  119.                 "items_list" => esc_html__( "items_list" ),
  120.                 "item_published" => esc_html__( "item_published" ),
  121.                 "item_published_privately" => esc_html__( "item_published_privately" ),
  122.                 "item_reverted_to_draft" => esc_html__( "item_reverted_to_draft" ),
  123.                 "item_scheduled" => esc_html__( "item_scheduled" ),
  124.                 "item_updated" => esc_html__( "item_updated" ),
  125.                 "item_link" => esc_html__( "item_link" ),
  126.                 "item_link_description" => esc_html__( "item_link_description" ),
  127.             ];
  128.         endif;
  129.         $args = [
  130.             $arg_label,
  131.             "labels" => $labels,
  132.             $arg_description,
  133.             "public" => true,
  134.             "publicly_queryable" => true,
  135.             "show_ui" => true,
  136.             "show_in_rest" => true,
  137.             "rest_base" => "",
  138.             "rest_controller_class" => "WP_REST_Posts_Controller",
  139.             "rest_namespace" => "wp/v2",
  140.             $arg_has_archive,
  141.             "show_in_menu" => true,
  142.             "show_in_nav_menus" => true,
  143.             "delete_with_user" => false,
  144.             $arg_exclude_from_search,
  145.             "capability_type" => "post",
  146.             "map_meta_cap" => true,
  147.             "hierarchical" => false,
  148.             "can_export" => false,
  149.             $arg_rewrite_slug,
  150.             "query_var" => true,
  151.             $arg_menu_position,
  152.             $arg_menu_icon,
  153.             "supports" => [ "title", "editor" ],
  154.             "show_in_graphql" => false,
  155.         ];
  156.         register_post_type( $register_slug, $args );
  157.     }
  158.     add_action( 'init', 'register_my_cpts' );
  159.     endwhile;
  160. endif;
  161.  
  162. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement