• I hope everyone is having a great day!

    I am creating this question as I have been stuck on this issue all day and I need some friendly advice.

    I have created a repeater custom metabox item for a custom post type called Proposal. The repeater works fine and is saving the content and displaying it how I would like. However, when I paste content into the textarea it throws a 403 error page when you update or publish the post and have MORE than 1 repeated option.

    The weird part is when you just add text to the textarea and don’t paste the content everything works fine. Obviously, this is an issue I need to fix before I release the plugin to the world. Here is the code I have. If you notice anything wrong, please let me know so I can improve it as I am fairly new to backend development.

    add_action( 'add_meta_boxes', 'wpp_proposal_services_box' );
    /* Do something with the data entered */
    add_action( 'save_post', 'wpp_save_services_data' );
    /* Adds a box to the main column on the Post and Page edit screens */
    function wpp_proposal_services_box() {
        add_meta_box(
            'wpp_proposal_services_box',
            __( 'Scope of Services', 'wp_proposal' ),
            'wpp_proposal_service_meta_box',
            'proposal',
            'normal',
            'high');
    }
    
    /* Prints Services box conent */
    function wpp_proposal_service_meta_box() {
        global $post;
        // Use nonce for verification
          wp_nonce_field( 'wpp_meta_box_nonce', 'meta_box_nonce' );
        ?>
    
        <div id="wpp_meta_inner">
        <?php
        //get the saved meta as an array
        $service = get_post_meta($post->ID,'service',false);
        $service_title = get_post_meta($post->ID, 'wpp_service_title', true);
        ?>
            <div>
    
                  <div class="wpp-input-container">
                      <label><?php _e('Title') ?></label>
                      <input type="text" name="wpp_service_title" id="wpp_service_title" value="<?php echo $service_title; ?>" />
                      <p><?php _e('Will display default "Scope of Services" if empty.') ?></p>
                  </div>
    
                  <div class="wpp-input-container">
                      <label class="wpp-label-repeater"><?php _e('Service Items') ?></label>
                      <?php
                      wp_nonce_field ( 'c_nonce_field', 'c_wpnonce');
                          $c = 0;
                          if ( count( $service ) > 0 ) {
                              if(!empty($service)) {
                                  foreach( $service as $service_item_val ) {
                                      foreach( $service_item_val as $service_item ) {
                                          if ( isset( $service_item['title'] ) || isset( $service_item['service_item'] ) ) {
                                              printf( '<div class="wpp-repeater-wrapper service">Title: <input class="wpp-repeater-input service" type="text" name="service[%1$s][title]" value="%2$s" />Description: <textarea class="wpp-repeater-input service" name="service[%1$s][service_item]" data-gramm_editor="false" value="">%3$s</textarea><span class="wpp-item-remove service">%4$s</span></div>', $c, $service_item['title'], $service_item['service_item'], __( 'Remove' ) );
                                              $c = $c +1;
                                          }
                                      }
                                  }
                              }
                          }
                      ?>
                      <span id="services_here"></span>
                      <div class="wpp-item-add services" style="visibility: hidden; margin-bottom: -20px;"><?php _e('Add Item'); ?></div>
                      <div class="wpp-item-add services add-button"><?php _e('Add Service'); ?></div>
                      </div>
    
            </div>
    
            <script>
            var $ =jQuery.noConflict();
            $(document).ready(function() {
                var count = <?php echo $c; ?>;
                $(".wpp-item-add.services").click(function() {
                    count = count + 1;
                    $('#services_here').append('<div class="wpp-repeater-wrapper service">Title: <input class="wpp-repeater-input service" type="text" name="service['+count+'][title]" value="" placeholder="" />Description: <textarea class="wpp-repeater-input service" name="service['+count+'][service_item]" data-gramm_editor="false" value="" placeholder=""></textarea><span class="wpp-item-remove service">Remove</span></div>' );
                    return false;
                });
                $(".wpp-item-remove.service").live('click', function() {
                    $(this).parent().remove();
                });
            });
            </script>
    
        </div>
    
    <?php }
    /* When the post is saved, saves our data */
    function wpp_save_services_data( $post_id ) {
      if(defined("DOING_AJAX") AND DOING_AJAX)
          return;
          if(!current_user_can('edit_post', $post_id ))
          return;
    	     if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'wpp_meta_box_nonce' ) ) return;
          $service = $_POST['service'];
          update_post_meta ($post_id, 'wpp_service_title', wp_kses( $_POST['wpp_service_title']));
          update_post_meta ($post_id,'service',$service);
    }
    
    • This topic was modified 6 years, 3 months ago by WP Codeus.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    When you call wp_kses(), you must pass a second argument, an array of allowed tags, or at least an empty array to strip all. With that change, your code works fine on my site. And yes, I added another input row and pasted in the field values. I did change the post type to match one of my custom types, the only other adjustment I made. I don’t know if this is related to your 403 response, but it needs to be addressed.

Viewing 1 replies (of 1 total)
  • The topic ‘403 Error when text is pasted in Custom Metabox Textarea’ is closed to new replies.