meta boxes - Metabox field in Wordpress admin menu is not saved - Stack Overflow

admin2025-04-15  2

I created a special menu called price list on the WordPress admin page. I have custom metabox area on this menu page. I also have reproducible input elements. Everything seems to be fine, just the data is not being saved. I'm stuck at this stage. Please can someone help me with this? This is my code that creates a menu and metabox on the admin page:

<?php function register_pricepage_subpage() {
    add_menu_page( 'Custom Price List Page', 'Dynamic Price List', 'manage_options', 'dynamic-price-list', 'price_page_meta_box_display' );
    add_action( 'admin_init', 'register_pricepage_settings' );
}
add_action( 'admin_menu', 'register_pricepage_subpage' );

function register_pricepage_settings() {
    register_setting( 'pricepage-settings-group', 'name[]' );
    register_setting( 'pricepage-settings-group', 'price[]' );
}

function price_page_meta_box_display($post) {
    global $post;
    $price_page_fields = get_post_meta(isset($post->id), 'price_page_fields', true);
    wp_nonce_field( 'price_page_meta_box_nonce', 'price_page_meta_box_nonce' );
    ?>

    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('#add-row').on('click', function() {
                var row = $('.empty-row.screen-reader-text').clone(true);
                row.removeClass('empty-row screen-reader-text');
                row.insertBefore('#price_page-fieldset-one tbody>tr:last');
                return false;
            });
            $('.remove-row').on('click', function() {
                $(this).parents('tr').remove();
                return false;
            });
        });
    </script>

    <form method="post" action="options.php">
        <?php
        settings_fields( 'pricepage-settings-group' ); 
        do_settings_sections( 'pricepage-settings-group' ); 
        ?>     
        <p><a id="add-row" class="button" href="#">Add Row</a>
        </p>
        <table id="price_page-fieldset-one" width="50%" style="padding: 1rem 2rem;" >
            <thead>
                <tr>
                    <th width="75%" style="border: solid 1px #000;padding: 3px 5px;">Name</th>
                    <th width="20%" style="border: solid 1px #000;padding: 3px 5px;">Price</th>
                    <th width="5%" style="border: solid 1px #000;padding: 3px 5px;">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if ( $price_page_fields ) :
                    foreach ( $price_page_fields as $field ) {
                        ?>
                        <tr>
                            <td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo esc_attr( $field['name'] ); ?>" /></td>
                            <td><input type="text" class="widefat" name="price[]" value="<?php if ($field['price'] != '') echo esc_attr( $field['price'] ); ?>" /></td>
                            <td><a class="button remove-row" href="#" style="padding: 0rem 1.5rem;">-</a></td>
                        </tr>
                        <?php
                    }
                else :
                    ?>
                    <tr>
                        <td><input type="text" class="widefat" name="name[]" /></td>
                        <td><input type="text" class="widefat" name="price[]" value=" " /></td>
                        <td><a class="button remove-row" href="#" style="padding: 0rem 1.5rem;">-</a></td>
                    </tr>
                <?php endif; ?>
                <tr class="empty-row screen-reader-text">
                    <td><input type="text" class="widefat" name="name[]" /></td>
                    <td><input type="text" class="widefat" name="price[]" value=" " /></td>
                    <td><a class="button remove-row" href="#" style="padding: 0rem 1.5rem;">-</a></td>  
                </tr>
            </tbody>
        </table>
        <span class="save-button">
            <?php submit_button(); ?>
        </span>
    </form>

    <?php
}
add_action('save_post', 'price_page_meta_box_save');
function price_page_meta_box_save($post_id) {
    if ( ! isset( $_POST['price_page_meta_box_nonce'] ) ||
        ! wp_verify_nonce( $_POST['price_page_meta_box_nonce'], 'price_page_meta_box_nonce' ) )
        return;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;
    if (!current_user_can('edit_post', $post_id))
        return;
    $old = get_post_meta($post_id, 'price_page_fields', true);
    $new = array();
    $names = $_POST['name'];
    $prices = $_POST['price'];
    $count = count( $names );
    for ( $i = 0; $i < $count; $i++ ) {
        if ( $names[$i] != '' ) :
            $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );
            if ( $prices[$i] == ' ' )
                $new[$i]['price'] = '';
            else
                $new[$i]['price'] = stripslashes( $prices[$i] );
        endif;
    }
    if ( !empty( $new ) && $new != $old )
        update_post_meta( $post_id, 'price_page_fields', $new );
    elseif ( empty($new) && $old )
        delete_post_meta( $post_id, 'price_page_fields', $old );
}

I created a special menu called price list on the WordPress admin page. I have custom metabox area on this menu page. I also have reproducible input elements. Everything seems to be fine, just the data is not being saved. I'm stuck at this stage. Please can someone help me with this? This is my code that creates a menu and metabox on the admin page:

<?php function register_pricepage_subpage() {
    add_menu_page( 'Custom Price List Page', 'Dynamic Price List', 'manage_options', 'dynamic-price-list', 'price_page_meta_box_display' );
    add_action( 'admin_init', 'register_pricepage_settings' );
}
add_action( 'admin_menu', 'register_pricepage_subpage' );

function register_pricepage_settings() {
    register_setting( 'pricepage-settings-group', 'name[]' );
    register_setting( 'pricepage-settings-group', 'price[]' );
}

function price_page_meta_box_display($post) {
    global $post;
    $price_page_fields = get_post_meta(isset($post->id), 'price_page_fields', true);
    wp_nonce_field( 'price_page_meta_box_nonce', 'price_page_meta_box_nonce' );
    ?>

    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('#add-row').on('click', function() {
                var row = $('.empty-row.screen-reader-text').clone(true);
                row.removeClass('empty-row screen-reader-text');
                row.insertBefore('#price_page-fieldset-one tbody>tr:last');
                return false;
            });
            $('.remove-row').on('click', function() {
                $(this).parents('tr').remove();
                return false;
            });
        });
    </script>

    <form method="post" action="options.php">
        <?php
        settings_fields( 'pricepage-settings-group' ); 
        do_settings_sections( 'pricepage-settings-group' ); 
        ?>     
        <p><a id="add-row" class="button" href="#">Add Row</a>
        </p>
        <table id="price_page-fieldset-one" width="50%" style="padding: 1rem 2rem;" >
            <thead>
                <tr>
                    <th width="75%" style="border: solid 1px #000;padding: 3px 5px;">Name</th>
                    <th width="20%" style="border: solid 1px #000;padding: 3px 5px;">Price</th>
                    <th width="5%" style="border: solid 1px #000;padding: 3px 5px;">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if ( $price_page_fields ) :
                    foreach ( $price_page_fields as $field ) {
                        ?>
                        <tr>
                            <td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo esc_attr( $field['name'] ); ?>" /></td>
                            <td><input type="text" class="widefat" name="price[]" value="<?php if ($field['price'] != '') echo esc_attr( $field['price'] ); ?>" /></td>
                            <td><a class="button remove-row" href="#" style="padding: 0rem 1.5rem;">-</a></td>
                        </tr>
                        <?php
                    }
                else :
                    ?>
                    <tr>
                        <td><input type="text" class="widefat" name="name[]" /></td>
                        <td><input type="text" class="widefat" name="price[]" value=" " /></td>
                        <td><a class="button remove-row" href="#" style="padding: 0rem 1.5rem;">-</a></td>
                    </tr>
                <?php endif; ?>
                <tr class="empty-row screen-reader-text">
                    <td><input type="text" class="widefat" name="name[]" /></td>
                    <td><input type="text" class="widefat" name="price[]" value=" " /></td>
                    <td><a class="button remove-row" href="#" style="padding: 0rem 1.5rem;">-</a></td>  
                </tr>
            </tbody>
        </table>
        <span class="save-button">
            <?php submit_button(); ?>
        </span>
    </form>

    <?php
}
add_action('save_post', 'price_page_meta_box_save');
function price_page_meta_box_save($post_id) {
    if ( ! isset( $_POST['price_page_meta_box_nonce'] ) ||
        ! wp_verify_nonce( $_POST['price_page_meta_box_nonce'], 'price_page_meta_box_nonce' ) )
        return;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;
    if (!current_user_can('edit_post', $post_id))
        return;
    $old = get_post_meta($post_id, 'price_page_fields', true);
    $new = array();
    $names = $_POST['name'];
    $prices = $_POST['price'];
    $count = count( $names );
    for ( $i = 0; $i < $count; $i++ ) {
        if ( $names[$i] != '' ) :
            $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );
            if ( $prices[$i] == ' ' )
                $new[$i]['price'] = '';
            else
                $new[$i]['price'] = stripslashes( $prices[$i] );
        endif;
    }
    if ( !empty( $new ) && $new != $old )
        update_post_meta( $post_id, 'price_page_fields', $new );
    elseif ( empty($new) && $old )
        delete_post_meta( $post_id, 'price_page_fields', $old );
}
Share Improve this question edited Feb 4 at 15:04 wws34 asked Feb 4 at 10:24 wws34wws34 131 silver badge6 bronze badges 2
  • It is not post. save_post wont work in custom page inputs – Vel Commented Feb 5 at 10:59
  • @Vel Thanks for your attention. What path should I follow? How can I save custom fields on the admin page? Do you have any ideas about this? – wws34 Commented Feb 5 at 19:56
Add a comment  | 

1 Answer 1

Reset to default 0

I just shared sample code to save the custom fields. You can modify whatever you want.

function register_pricepage_subpage() {
    add_menu_page( 'Custom Price List Page', 'Dynamic Price List', 'manage_options', 'dynamic-price-list', 'price_page_meta_box_display' );
    add_action( 'admin_init', 'register_pricepage_settings' );
}
add_action( 'admin_menu', 'register_pricepage_subpage' );

function register_pricepage_settings() {
    
    // register_setting( 'pricepage-settings-group', 'name[]' );
    // register_setting( 'pricepage-settings-group', 'price[]' );
     
      update_option( 'price_page_fields', "" );

    if ( isset( $_POST['name'] ) &&  isset( $_POST['price'] ) ){
        // print_r($_POST);
        //     return;
        // if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        //     return;
        // if (!current_user_can('edit_post', $post_id))
        //     return;
        $old = get_option( 'price_page_fields' );
        $new = array();
        $names = $_POST['name'];
        $prices = $_POST['price'];

       
        $count = count( $names );
        for ( $i = 0; $i < $count; $i++ ) {
            if ( $names[$i] != '' ) :
                $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );
                if ( $prices[$i] == ' ' )
                    $new[$i]['price'] = '';
                else
                    $new[$i]['price'] = stripslashes( $prices[$i] );
            endif;
        }
        if ( !empty( $new ) && $new != $old ){


            update_option( 'price_page_fields', $new );

            //update_post_meta( $post_id, 'price_page_fields', $new );
        }elseif ( empty($new) && $old ){
            //delete_post_meta( $post_id, 'price_page_fields', $old );
        
        }
    }
}

function price_page_meta_box_display($post) {

        
    // global $post;
    // $price_page_fields = get_post_meta(isset($post->id), 'price_page_fields', true);
    wp_nonce_field( 'price_page_meta_box_nonce', 'price_page_meta_box_nonce' );


    $price_page_fields = get_option( 'price_page_fields' );

    ?>

    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('#add-row').on('click', function() {
                var row = $('.empty-row.screen-reader-text').clone(true);
                row.removeClass('empty-row screen-reader-text');
                row.insertBefore('#price_page-fieldset-one tbody>tr:last');
                return false;
            });
            $('.remove-row').on('click', function() {
                $(this).parents('tr').remove();
                return false;
            });
        });
    </script>

    <form method="post" action="">
        <?php
        settings_fields( 'pricepage-settings-group' ); 
        do_settings_sections( 'pricepage-settings-group' ); 
        ?>     
        <p><a id="add-row" class="button" href="#">Add Row</a>
        </p>
        <table id="price_page-fieldset-one" width="50%" style="padding: 1rem 2rem;" >
            <thead>
                <tr>
                    <th width="75%" style="border: solid 1px #000;padding: 3px 5px;">Name</th>
                    <th width="20%" style="border: solid 1px #000;padding: 3px 5px;">Price</th>
                    <th width="5%" style="border: solid 1px #000;padding: 3px 5px;">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if ( $price_page_fields ) :
                    foreach ( $price_page_fields as $field ) {
                        ?>
                        <tr>
                            <td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo esc_attr( $field['name'] ); ?>" /></td>
                            <td><input type="text" class="widefat" name="price[]" value="<?php if ($field['price'] != '') echo esc_attr( $field['price'] ); ?>" /></td>
                            <td><a class="button remove-row" href="#" style="padding: 0rem 1.5rem;">-</a></td>
                        </tr>
                        <?php
                    }
                else :
                    ?>
                    <tr>
                        <td><input type="text" class="widefat" name="name[]" /></td>
                        <td><input type="text" class="widefat" name="price[]" value=" " /></td>
                        <td><a class="button remove-row" href="#" style="padding: 0rem 1.5rem;">-</a></td>
                    </tr>
                <?php endif; ?>
                <tr class="empty-row screen-reader-text">
                    <td><input type="text" class="widefat" name="name[]" /></td>
                    <td><input type="text" class="widefat" name="price[]" value=" " /></td>
                    <td><a class="button remove-row" href="#" style="padding: 0rem 1.5rem;">-</a></td>  
                </tr>
            </tbody>
        </table>
        <span class="save-button">
            <?php submit_button(); ?>
        </span>
    </form>

    <?php
}

Output

转载请注明原文地址:http://www.anycun.com/QandA/1744727589a86781.html