1

I have a custom form . My form have three input fields book_name, chapter_title and chapter_content. chapter_title and chapter_content are dynamic form fields. I want to insert book_name as post_title column and chapter_title, chapter_content as serialize data into post_content column. But when submitting form, Unfortunately I can't serialize and inser the data into post_content columns. Here is my code -

<?php
if (!defined('ABSPATH')) {
    exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $book_name = isset($_POST['book_name']) ? sanitize_text_field($_POST['book_name']) : '';
    $chapter_names = isset($_POST['c_name']) ? $_POST['c_name'] : array();
    $chapter_contents = isset($_POST['chapter_content']) ? $_POST['chapter_content'] : array();

    $chapters = array();
    foreach ($chapter_names as $key => $chapter_name) {
        $chapter_content = wp_kses_post($chapter_contents[$key]);
        $chapters[] = array(
            'c_title' => $chapter_name,
            'c_content' => $chapter_content
        );
    }
    $serialized_chapters = serialize($chapters);
    $post_data = array(
        'post_title' => $book_name,
        'post_content' => $serialized_chapters,
        'post_type' => 'book',
        'post_status' => 'publish',
    );
    $post_id = wp_insert_post($post_data);

    if ($post_id) {
        echo '<div class="updated"><p>Book added successfully!</p></div>';
    } else {
        echo '<div class="error"><p>Error adding book. Please try again.</p></div>';
    }
}
?>
<div class="wrap">
    <div class="container">
        <form method="POST">
            <div class="row">
                <label for="book-name">Book Name:</label>
                <input type="text" id="book-name" name="book_name" required>
            </div>
            <div class="form-group" id="dynamic_form">
                <div class="row dynamic-chapter">
                    <div class="col-md-3">
                        <input type="text" name="c_name[]" placeholder="Enter Chapter Name" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <?php wp_editor('', 'chapter_content_0'); ?>
                    </div>
                </div>
            </div>
            <div class="btn-group">
                <a href="javascript:void(0)" class="btn btn-success" id="plus5">Add More</a>
                <a href="javascript:void(0)" class="btn btn-danger" id="minus5">Remove</a>
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
        </form>
    </div>
</div> 

and the script code is -

jQuery(document).ready(function () {
    var dynamic_form = jQuery("#dynamic_form").dynamicForm("#dynamic_form", "#plus5", "#minus5", {
        limit: 10,
        formPrefix: "dynamic_form",
        normalizeFullForm: false
    });


    jQuery("#dynamic_form #minus5").on('click', function () {
        var initDynamicId = jQuery(this).closest('#dynamic_form').parent().find("[id^='dynamic_form']").length;
        if (initDynamicId === 2) {
            jQuery(this).closest('#dynamic_form').next().find('#minus5').hide();
        }
        jQuery(this).closest('#dynamic_form').remove();
    });


});

How can I fix the problem and insert the chapter_title and chapter_content data into wp_posts table's post_content columns as serialize data?

0

Your Answer

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