最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Create Metaboxes dynamically in admin for Pages

programmeradmin3浏览0评论

I've created in the admin editor of my site homepage a Metabox with an input and a textarea fields. The code works great. However my goal is to allow the admin user to duplicate the Metabox with the same fields (of course no value inside) by clicking on a button "Add new".

The concept: the user can add as many sections as he wants in the front end of the site homepage. A section corresponds to a Metabox (title + description).

Here is the code of my Metabox:

add_action( 'add_meta_boxes', 'mysite_home_add_meta_box' );
if ( ! function_exists( 'mysite_home_add_meta_box' ) ) {
    function mysite_home_add_meta_box() {
        add_meta_box( 'additional-page-metabox-options', esc_html__( 'Section 1', 'mysite' ), 'mysite_home_metabox_controls', 'page', 'normal', 'low' );
    }
}

if ( ! function_exists( 'mysite_home_metabox_controls' ) ) {
    function mysite_home_metabox_controls( $post ) {
        $meta = get_post_meta( $post->ID );
        $section_title_home_input_field = ( isset( $meta['section_title_home_input_field'][0] ) && '' !== $meta['section_title_home_input_field'][0] ) ? $meta['section_title_home_input_field'][0] : '';
        $section_descr_home_textarea_field = ( isset( $meta['section_descr_home_textarea_field'][0] ) && '' !== $meta['section_descr_home_textarea_field'][0] ) ? $meta['section_descr_home_textarea_field'][0] : '';
        wp_nonce_field( 'mysite_home_control_meta_box', 'mysite_home_control_meta_box_nonce' );
        ?>
        <div class="post_meta_extras">
            <p>
                <label><?php esc_attr_e( 'Title', 'mysite' ); ?></label>
                <input type="text" name="section_title_home_input_field" value="<?php echo esc_attr( $section_title_home_input_field ); ?>">
            </p>
            <p>
                <label><?php esc_attr_e( 'Description', 'mysite' ); ?></label>
                <textarea type="text" name="section_descr_home_textarea_field" id="" class="widefat" cols="100%" rows="8"><?php echo esc_attr( $section_descr_home_textarea_field ); ?></textarea>
            </p>
        <?php
    }
}

add_action( 'save_post', 'mysite_home_save_metaboxes' );
if ( ! function_exists( 'mysite_home_save_metaboxes' ) ) {
    function mysite_home_1_save_metaboxes( $post_id ) {

        if ( ! isset( $_POST['mysite_home_control_meta_box_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['mysite_home_control_meta_box_nonce'] ), 'mysite_home_control_meta_box' ) ) {
            return $post_id;
        }

        if ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] ) {
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }

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

        if ( isset( $_POST['section_title_home_input_field'] ) ) { 
            update_post_meta( $post_id, 'section_title_home_input_field', sanitize_text_field( wp_unslash( $_POST['section_title_home_input_field'] ) ) );
        }

        if ( isset( $_POST['section_descr_home_textarea_field'] ) ) {
            update_post_meta( $post_id, 'section_descr_home_textarea_field', $_POST['section_descr_home_textarea_field'] );
        }

    }
}

I'm struggling to find a solution. Any help would be appreciated, thanks!

发布评论

评论列表(0)

  1. 暂无评论