NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cia-post-types.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Craft in America Custom Types Plugin
  4. *
  5. * @since 1.0.0
  6. * @package cia_post_types
  7. *
  8. * @wordpress-plugin
  9. * Plugin Name: Craft in America - Content Types
  10. * Plugin URI:
  11. * Description: Adds custom custom types, and p2p relationships
  12. * Version: 1.0.0
  13. * Author: TOJ <john@yvvas.com>
  14. */
  15. // If this file is called directly, abort.
  16. if ( ! defined( 'WPINC' ) ) { die; }
  17. require_once 'includes/cmb2/init.php';
  18. include('includes/custom-types.php');
  19. include('includes/p2p-mappings.php');
  20. /**
  21. * Class that holds all the necessary
  22. * functionality to build custom post types
  23. */
  24. class PostType {
  25. /**
  26. * The custom post type slug
  27. * @var string
  28. */
  29. private $post_type;
  30. /**
  31. * The custom post type icon
  32. * @var string
  33. */
  34. private $icon;
  35. function __construct($post_type, $icon) {
  36. $this->post_type = $post_type;
  37. $this->icon = $icon;
  38. }
  39. /** Register custom post type call-back */
  40. public function register_post_type() {
  41. $args = array(
  42. 'label' => esc_html( $this->post_type, 'test-plugin' ),
  43. 'public' => true,
  44. 'menu_position' => 47,
  45. 'menu_icon' => $this->icon,
  46. 'supports' => array( 'title', 'editor', 'revisions', 'thumbnail' ),
  47. 'has_archive' => false,
  48. 'show_in_rest' => true,
  49. 'publicly_queryable' => false,
  50. );
  51. register_post_type( $this->post_type, $args );
  52. }
  53. }
  54. /**
  55. * Plugin Logic
  56. * Where the magic actually happens
  57. */
  58. $custom_types = get_all_custom_types();
  59. foreach ($custom_types as $type):
  60. $icon = get_icon($type);
  61. add_action( 'init', [ new PostType($type, $icon), 'register_post_type' ] );
  62. // Register custom meta-boxes
  63. // register_custom_meta_box( 'hero_box' 'Custom Hero Section', 'hero', $type );
  64. endforeach;
  65. add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );
  66. function sanitize_hero_urls( $value, $field_args, $field ) {
  67. $encoded = wp_json_encode( array( 'url' => $value ), true );
  68. return $encoded;
  69. }
  70. function cmb_hero_render_row_cb( $field_args, $field ) {
  71. $id = $field->args( 'id' );
  72. $label = $field->args( 'name' );
  73. $name = $field->args( '_name' );
  74. $value = $field->escaped_value();
  75. $description = $field->args( 'description' );
  76. // !: Impossible to fine &quot; conversion
  77. $decoded = json_decode( str_replace('&quot;', '"', $value), true );
  78. ?>
  79. <div class="custom-field-row">
  80. <p>
  81. <label for="<?php echo $id; ?>"><?php echo $label; ?></label>
  82. <input style="width: 100%;" id="<?php echo $id; ?>" type="text" name="<?php echo $name; ?>"
  83. value="<?php echo $decoded['url']; ?>"/>
  84. </p>
  85. <p class="description"><?php echo $description; ?></p>
  86. </div>
  87. <?php
  88. }
  89. /**
  90. * Define the metabox and field configurations.
  91. */
  92. function cmb2_sample_metaboxes() {
  93. /**
  94. * Initiate the metabox
  95. */
  96. $cmb = new_cmb2_box( array(
  97. 'id' => 'hero_metabox',
  98. 'title' => __( 'Hero', 'cmb2' ),
  99. 'object_types' => array( 'artist', ), // Post type
  100. 'context' => 'normal',
  101. 'priority' => 'high',
  102. 'show_names' => true, // Show field names on the left
  103. 'show_in_rest' => WP_REST_Server::READABLE
  104. // 'cmb_styles' => false, // false to disable the CMB stylesheet
  105. // 'closed' => true, // Keep the metabox closed by default
  106. ) );
  107. // URL text field
  108. $cmb->add_field( array(
  109. 'name' => __( 'YouTube URL', 'cmb2' ),
  110. 'desc' => __( 'Video for the hero section to display', 'cmb2' ),
  111. 'id' => 'hero_header',
  112. 'type' => 'text',
  113. 'sanitization_cb' => 'sanitize_hero_urls',
  114. 'render_row_cb' => 'cmb_hero_render_row_cb'
  115. ) );
  116. }