Guest User

Untitled

a guest
Nov 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. <?php
  2. add_action( 'add_meta_boxes', function() {
  3. add_meta_box( 'custom-metabox', 'Select values', 'fill_metabox', 'post', 'normal' );
  4. });
  5.  
  6. function fill_metabox( $post ) {
  7. wp_nonce_field( basename(__FILE__), 'mam_nonce' );
  8.  
  9. // How to use 'get_post_meta()' for multiple checkboxes as array?
  10.  
  11. $elements = get_elements(); //returns an associative array
  12. foreach ( $elements as $element) {
  13. ?>
  14.  
  15. <p>
  16. <input
  17. type = "checkbox"
  18. name = "multval[]"
  19. value = <?php echo $element;?>
  20. //How to use checked() function in case of
  21. //multiple checkbox as arrays
  22. >
  23. <?php echo $element;?>
  24. </p>
  25.  
  26. <?php
  27. }
  28. }
  29.  
  30.  
  31. add_action( 'save_post', function( $post_id ) {
  32. $is_autosave = wp_is_post_autosave( $post_id );
  33. $is_revision = wp_is_post_revision( $post_id );
  34. $is_valid_nonce = ( isset( $_POST[ 'mam_nonce' ] ) && wp_verify_nonce( $_POST[ 'mam_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
  35.  
  36. if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
  37. return;
  38. }
  39.  
  40. /*How to run a loop to save values for each checkbox?*/
  41. });
  42.  
  43. add_action( 'add_meta_boxes', function() {
  44. add_meta_box( 'custom-metabox', 'Select values', 'fill_metabox', 'post', 'normal' );
  45. });
  46.  
  47. function fill_metabox( $post ) {
  48. wp_nonce_field( basename(__FILE__), 'mam_nonce' );
  49.  
  50. // How to use 'get_post_meta()' for multiple checkboxes as array?
  51. $postmeta = maybe_unserialize( get_post_meta( $post->ID, 'elements', true ) );
  52.  
  53. // Our associative array here. id = value
  54. $elements = array(
  55. 'apple' => 'Apple',
  56. 'orange' => 'Orange',
  57. 'banana' => 'Banana'
  58. );
  59.  
  60. // Loop through array and make a checkbox for each element
  61. foreach ( $elements as $id => $element) {
  62.  
  63. // If the postmeta for checkboxes exist and
  64. // this element is part of saved meta check it.
  65. if ( is_array( $postmeta ) && in_array( $id, $postmeta ) ) {
  66. $checked = 'checked="checked"';
  67. } else {
  68. $checked = null;
  69. }
  70. ?>
  71.  
  72. <p>
  73. <input type="checkbox" name="multval[]" value="<?php echo $id;?>" <?php echo $checked; ?> />
  74. <?php echo $element;?>
  75. </p>
  76.  
  77. <?php
  78. }
  79. }
  80.  
  81.  
  82. add_action( 'save_post', function( $post_id ) {
  83. $is_autosave = wp_is_post_autosave( $post_id );
  84. $is_revision = wp_is_post_revision( $post_id );
  85. $is_valid_nonce = ( isset( $_POST[ 'mam_nonce' ] ) && wp_verify_nonce( $_POST[ 'mam_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
  86.  
  87. if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
  88. return;
  89. }
  90.  
  91. // If the checkbox was not empty, save it as array in post meta
  92. if ( ! empty( $_POST['multval'] ) ) {
  93. update_post_meta( $post_id, 'elements', $_POST['multval'] );
  94.  
  95. // Otherwise just delete it if its blank value.
  96. } else {
  97. delete_post_meta( $post_id, 'elements' );
  98. }
  99.  
  100. });
Add Comment
Please, Sign In to add comment