tsu_newsletter_custom_post.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // Dodaj akcję, aby zarejestrować niestandardowy typ postu
  3. add_action('init', 'register_custom_post_type');
  4. function register_custom_post_type() {
  5. $labels = array(
  6. 'name' => 'Emaile',
  7. 'singular_name' => 'Email',
  8. 'menu_name' => 'Emaile',
  9. 'name_admin_bar' => 'Emaile',
  10. 'add_new' => 'Dodaj nowy',
  11. 'add_new_item' => 'Dodaj nowy email',
  12. 'new_item' => 'Nowy email',
  13. 'edit_item' => 'Edytuj email',
  14. 'view_item' => 'Zobacz email',
  15. 'all_items' => 'Wszystkie emaile',
  16. 'search_items' => 'Szukaj emaili',
  17. 'not_found' => 'Nie znaleziono emaili',
  18. 'not_found_in_trash' => 'Nie znaleziono emaili w koszu',
  19. );
  20. $args = array(
  21. 'labels' => $labels,
  22. 'public' => false,
  23. 'publicly_queryable' => false,
  24. 'show_ui' => true,
  25. 'show_in_menu' => true,
  26. 'query_var' => true,
  27. 'rewrite' => array('slug' => 'emails'),
  28. 'capability_type' => 'post',
  29. 'has_archive' => true,
  30. 'hierarchical' => false,
  31. 'menu_position' => null,
  32. 'supports' => array('title', 'editor'),
  33. );
  34. register_post_type('project', $args);
  35. // Dodaj akcję, aby zarejestrować niestandardowe pola meta
  36. add_action('add_meta_boxes', 'add_email_meta_box');
  37. }
  38. function add_email_meta_box() {
  39. add_meta_box(
  40. 'email_meta_box',
  41. 'Adres E-mail',
  42. 'render_email_meta_box',
  43. 'project', // Tutaj używamy identyfikatora niestandardowego typu postu
  44. 'normal',
  45. 'default'
  46. );
  47. }
  48. function render_email_meta_box($post) {
  49. // Pobierz aktualną wartość pola meta
  50. $email_value = get_post_meta($post->ID, '_project_email', true);
  51. // Wyświetl pole input
  52. echo '<label for="project_email">Adres E-mail:</label>';
  53. echo '<input type="email" id="project_email" name="project_email" value="' . esc_attr($email_value) . '" />';
  54. }
  55. // Dodaj akcję, aby zapisać wartość pola meta
  56. add_action('save_post', 'save_email_meta');
  57. function save_email_meta($post_id) {
  58. if (isset($_POST['project_email'])) {
  59. update_post_meta($post_id, '_project_email', sanitize_email($_POST['project_email']));
  60. }
  61. }