post-metabox.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. add_action('add_meta_boxes', 'ninja_forms_add_custom_box');
  3. /* Do something with the data entered */
  4. add_action('save_post', 'ninja_forms_save_postdata');
  5. /* Adds a box to the main column on the Post and Page edit screens */
  6. function ninja_forms_add_custom_box() {
  7. add_meta_box(
  8. 'ninja_forms_selector',
  9. __( 'Append A Ninja Form', 'ninja-forms'),
  10. 'ninja_forms_inner_custom_box',
  11. 'post',
  12. 'side',
  13. 'low'
  14. );
  15. add_meta_box(
  16. 'ninja_forms_selector',
  17. __( 'Append A Ninja Form', 'ninja-forms'),
  18. 'ninja_forms_inner_custom_box',
  19. 'page',
  20. 'side',
  21. 'low'
  22. );
  23. }
  24. /* Prints the box content */
  25. function ninja_forms_inner_custom_box() {
  26. $post_id = ! empty( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : 0;
  27. // Use nonce for verification
  28. wp_nonce_field( 'ninja_forms_append_form', 'nf_append_form' );
  29. // The actual fields for data entry
  30. ?>
  31. <select id="ninja_form_select" name="ninja_form_select">
  32. <option value="0">-- <?php _e('None', 'ninja-forms');?></option>
  33. <?php
  34. $all_forms = ninja_forms_get_all_forms();
  35. $form_id = get_post_meta( $post_id, 'ninja_forms_form', true );
  36. foreach( $all_forms as $form ){
  37. $title = $form['data']['form_title'];
  38. $id = $form['id'];
  39. ?>
  40. <option value="<?php echo esc_attr( $id );?>"<?php selected( $id, $form_id );?>>
  41. <?php echo $title;?>
  42. </option>
  43. <?php
  44. }
  45. ?>
  46. </select>
  47. <?php
  48. }
  49. /* When the post is saved, saves our custom data */
  50. function ninja_forms_save_postdata( $post_id ) {
  51. global $wpdb;
  52. if(isset($_POST['nf_append_form'])){
  53. // verify if this is an auto save routine.
  54. // If it is our form has not been submitted, so we dont want to do anything
  55. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  56. return $post_id;
  57. // verify this came from the our screen and with proper authorization,
  58. // because save_post can be triggered at other times
  59. if ( !wp_verify_nonce( $_POST['nf_append_form'], 'ninja_forms_append_form' ) )
  60. return $post_id;
  61. // Check permissions
  62. if ( 'page' == $_POST['post_type'] ) {
  63. if ( !current_user_can( 'edit_page', $post_id ) )
  64. return $post_id;
  65. } else {
  66. if ( !current_user_can( 'edit_post', $post_id ) )
  67. return $post_id;
  68. }
  69. // OK, we're authenticated: we need to find and save the data
  70. $post_id = absint( $_POST['post_ID'] );
  71. $form_id = absint( $_POST['ninja_form_select'] );
  72. if ( empty ( $form_id ) ) {
  73. delete_post_meta( $post_id, 'ninja_forms_form' );
  74. } else {
  75. update_post_meta( $post_id, 'ninja_forms_form', $form_id );
  76. }
  77. }
  78. }