복붙노트

[WORDPRESS] 워드 프레스 옵션 페이지에 메타 박스 추가

WORDPRESS

워드 프레스 옵션 페이지에 메타 박스 추가

해결법


  1. 1.네, 그것은 가능합니다. 이전 문제의 코드는 정확하지만 뭔가 중요한 그리워하거나 질문에 해당 코드를 추가하지 않았습니다.

    네, 그것은 가능합니다. 이전 문제의 코드는 정확하지만 뭔가 중요한 그리워하거나 질문에 해당 코드를 추가하지 않았습니다.

    여기에 데모 플러그인 당신이 작업을 얻을 수 있도록 할 수 있습니다.

    데모 플러그인에서 기본 코드는 다음과 같다. 주 전체 exemple의 측면 메타 박스는 작동하지 않으며이 워드 프레스 2.8 용으로 작성 및 최신 버전의 거의 5.0되었을 때 두 개의 열이 레이아웃있다.

    // Source code by Frank Bueltge at gist.github.com/bueltge/757903
    class howto_metabox_plugin {
        function howto_metabox_plugin() {
            add_action('admin_menu', array($this, 'on_admin_menu')); 
            add_action('admin_post_save_howto_metaboxes_general', array($this, 'on_save_changes'));
        }
    
        function on_admin_menu() {
            $this->pagehook = add_options_page('Howto Metabox Page Title', "HowTo Metaboxes", 'manage_options', 'howto_metaboxes', array($this, 'on_show_page'));
            add_action('load-'.$this->pagehook, array($this, 'on_load_page'));
        }
    
        function on_load_page() {
            wp_enqueue_script('common');
            wp_enqueue_script('wp-lists');
            wp_enqueue_script('postbox');
            add_meta_box('howto-metaboxes-contentbox-2', 'Contentbox 2 Title', array($this, 'on_contentbox_2_content'), $this->pagehook, 'normal', 'core');
            add_meta_box('howto-metaboxes-contentbox-additional-1', 'Contentbox Additional 1 Title', array($this, 'on_contentbox_additional_1_content'), $this->pagehook, 'additional', 'core');
        }
    
        function on_show_page() {
            //define some data can be given to each metabox during rendering
            $data = array('My Data 1', 'My Data 2', 'Available Data 1');
            ?>
            <div id="howto-metaboxes-general" class="wrap">
            <?php screen_icon('options-general'); ?>
            <h2>Metabox Showcase Plugin Page</h2>
            <form action="admin-post.php" method="post">
                <?php wp_nonce_field('howto-metaboxes-general'); ?>
                <input type="hidden" name="action" value="save_howto_metaboxes_general" />
    
                <div id="poststuff" class="metabox-holder">
                    <div id="side-info-column" class="inner-sidebar">
                        <?php do_meta_boxes($this->pagehook, 'side', $data); ?>
                    </div>
                    <div id="post-body" class="has-sidebar">
                        <div id="post-body-content" class="has-sidebar-content">
                            <?php do_meta_boxes($this->pagehook, 'normal', $data); ?>
                            <?php do_meta_boxes($this->pagehook, 'additional', $data); ?>
                            <p>
                                <input type="submit" value="Save Changes" class="button-primary" name="Submit"/>    
                            </p>
                        </div>
                    </div>
                    <br class="clear"/>
    
                </div>  
            </form>
            </div>
            <script type="text/javascript">
                //<![CDATA[
                jQuery(document).ready( function($) {
                    // close postboxes that should be closed
                    $('.if-js-closed').removeClass('if-js-closed').addClass('closed');
                    // postboxes setup
                    postboxes.add_postbox_toggles('<?php echo $this->pagehook; ?>');
                });
                //]]>
            </script>
    
            <?php
        }
    
        function on_save_changes() {
            if ( !current_user_can('manage_options') )
                wp_die( __('Cheatin&#8217; uh?') );         
    
            check_admin_referer('howto-metaboxes-general');     
            //process here your on $_POST validation and / or option saving 
            //lets redirect the post request into get request (you may add additional params at the url, if you need to show save results
            wp_redirect($_POST['_wp_http_referer']);        
        }
        function on_contentbox_2_content($data) {
            sort($data);
            ?>
            <p>The given parameter at <b>reverse sorted</b> order are: <em><?php echo implode(' | ', array_reverse($data)); ?></em></p>
            <?php
        }
        function on_contentbox_additional_1_content($data) {
            ?>
            <p>This and the 2nd <em>additional</em> box will be addressed by an other group identifier to render it by calling with this dedicated name.</p>
            <p>You can have as much as needed box groups.</p>
            <?php
        }
    }
    $my_howto_metabox_plugin = new howto_metabox_plugin();
    
  2. from https://stackoverflow.com/questions/8626844/add-meta-box-to-wordpress-options-page by cc-by-sa and MIT license