0

So I am cloning some divs created from an array using jQuery. Here is an example.

I have a drop down list that contains items to add to the meta box.

The add button clones a hidden div based on the name in the drop down that matches the rel of the div. This then gets appended into my meta box. So it ends up looking like this.

        echo '<div id="page_selected_elements" name="page_selected_elements" class="page_selected_elements">';
        // content gets cloned into here
        // example div that gets added
        <div rel="Quote" class="page-element element1-4 test" id="page-element">
                <div class="page-element-item">
                    <div class="left-side">
                        <input type="button" value="+" class="increase-size">
                        <input type="button" value="-" class="decrease-size">
                        <span class="element-description">Quote</span>
                    </div>
                    <input type="hidden" id="page-option-item-type" class="page-option-item-type" value="Tab" name="">
                    <input type="hidden" id="page-option-item-size" class="page-option-item-size" value="element1-4" name="">
                    <div class="right-side">
                        <span id="element-size-text" class="element-size-text">1/4</span>
                        <div class="change-element-property">
                                    <input type="button" value="edit" rel="edit-box" id="page-element-edit-box" class="edit-element">
                                    <input type="button" value="Remove" class="remove-element" id="delete-element">
                        </div>
                    </div> 
                </div>
                <div id="my_modal" class="page-element-edit">
                <p>Quote</p>
                <input type="text" id="_cmb_page-option-quote" name="_cmb_page-option-quote" class="cmb_text_small">
                </div>
        </div>
        // end example div that gets added

        // getting the values for displaying after save
        $values = get_post_custom( $post->ID );
        $selected = isset( $values['addddData'] ) ? $values['addddData'][0] : '';
        echo $selected;
        echo '</div>';

     // Save data from metabox
function save( $post_id, $post )  {

    // verify nonce
    if ( ! isset( $_POST['wp_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['wp_meta_box_nonce'], basename(__FILE__) ) ) {
        return $post_id;
    }

    // check autosave
    if ( defined('DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    // check permissions
    if ( 'page' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) ) {
            return $post_id;
        }
    } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
        return $post_id;
    }

    $cloned = $_POST['page_selected_elements'];
    update_post_meta( $post_id, 'addddData', $cloned );

    //print_r($_POST);

    }

Now the problem is it will not save the contents inside of the div page_selected_elements I can see the key is added via phpmyadmin, but the meta_value is always empty when I save after cloning items "adding items from drop down".

As a test I manually edited the meta_value using phpmyadmin and pasted the example clone div as the value, refreshed and the div was displayed. However once I updated the page the value was removed.

As a test I changed the div page_selected_elements to a textarea to test my save function, and that works just fine, text I enter into the text area gets updated and displayed back on the page after the update.

Can anyone think what the problem might be? Could it have to do with dynamically added content? Maybe the update is not seeing it. I see the cloned items the whole time during the update but once it reloads the page they are gone and the meta_value is empty.

6
  • There is a lot of information missing. How does the data get submitted for one? The part eliminated to "keep it short" is the important part... at least I think it is. As is, I don't see any form submissions, AJAX, nothing that would populate $_POST. That is a problem.
    – s_ha_dum
    Oct 20, 2013 at 17:22
  • Updated my original post with the save function...
    – Bowenac
    Oct 20, 2013 at 17:32
  • The save() function doesn't appear to hooked to anything or used in any way. Again, there is information missing.
    – s_ha_dum
    Oct 20, 2013 at 18:57
  • Do you really need me to paste the whole contents of the file haha. I already told you how it works. But sure here is a paste bin. pastebin.com/sRd2NzvM I already mentioned how the save works if I enter plain text inside the div. So the save function is working.
    – Bowenac
    Oct 20, 2013 at 19:28
  • No. What I expect is for you to paste the relevant parts and leave out the irrelevant bits, instead of doing just the opposite.
    – s_ha_dum
    Oct 20, 2013 at 19:44

1 Answer 1

1

I believe the problem is with your use of sanitize_text_field (line 668 in your pastebin), which will strip markup. Proof of concept:

$str = '<p>Howdy Ho!</p>';
echo $str;
echo sanitize_text_field($str);

You will need to use some other function to clean the data... possibly wp_kses

6
  • Wouldn't that strip tags and leave me with plain text? Nothing gets saved when trying to save contents of a div. If I replace the div with a textbox and type something in the textbox it saves just fine.
    – Bowenac
    Oct 21, 2013 at 11:59
  • I believe my problem is this isn't going to work the way I was thinking. I need to somehow convert each clone item to xml and save the xml value as the meta_value. As it doesn't seem like you can save html with ids and classes as meta_value.
    – Bowenac
    Oct 22, 2013 at 20:09
  • I am pretty sure I was able to save html with id and class attributes with a bare bone custom meta box. I will recheck later when I am near my dev server.
    – s_ha_dum
    Oct 22, 2013 at 20:15
  • Awesome thanks, I haven't been able to. I have a more minified version of what I am trying to do if you would like to see it.
    – Bowenac
    Oct 22, 2013 at 20:16
  • Confirmed. I can save html with id and class attributes with a bare bones meta box.
    – s_ha_dum
    Oct 22, 2013 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.