数据未保存Wordpress自定义管理页面

时间:2015-01-11 02:51:08

标签: php wordpress forms admin

我尝试为Wordpress主题创建一个选项页面。我设法创建页面,即Metaboxes,但是当我发送表单时,数据不会被保存。在我向您展示我的代码之前,我指定使用类来更轻松地生成页面。也许这可以指导你解决问题。

在此代码中,我想保存“幻灯片”选项。此选项使用register_setting()注册,并在slideshow_metabox()方法中可见,该方法显示包含名为“slide [...]”的字段的表单。

    add_action('admin_menu', 'add_custom_admin_page');
function add_custom_admin_page(){
    add_menu_page('CYBER Auto Pièces', 'CYBER Auto Pièces', 'manage_options', 'cyberautopieces', '' );

    add_action( 'admin_init', 'cyberautopieces_register_settings' );
}

function cyberautopieces_register_settings() {
    //register our settings
    register_setting( 'cyberautopieces-settings', 'slide' );
}


new GeneratePageAdmin ('cyberautopieces','CYBER Auto Pièces','CYBER Auto Pièces', 'manage_options','cyberautopieces');

//Add some metaboxes to the page
add_action('add_meta_boxes','cyberautopieces_admin_metaboxes');
function cyberautopieces_admin_metaboxes(){
    add_meta_box('slideshow_box','Slideshow','slideshow_metabox','toplevel_page_cyberautopieces','normal','high');
    add_meta_box('save_box','Enregistrer','save_metabox','toplevel_page_cyberautopieces','side','high');
}

//Define the insides of the metabox
function slideshow_metabox(){

  var_dump(get_option('slide')); // DISPLAY bool(false) :(
  ?>

  <ul id="slideshow_box">
    <li>
        <a class="add-image-slider" href="#">
            <div class="images half">
                Choisir une image
                <img src="" alt="">
                <input type="hidden" name="slide[img][]">
            </div>
        </a>
        <div class="text half">
            <label><strong>Titre : </strong></label>
            <input type="text" name="slide[title][]" value="" />
            <label><strong>Slogan : </strong></label>
            <input type="text" name="slide[slogan][]" value="" />
            <strong>Situation : </strong>
            <select id="slide_situation" name="slide[text_position][]">
                <option value="left" selected>Gauche</option>
                <option value="right">Droite</option>
            </select>
        </div>
        <div class="add-delete">
            <span class="add">+</span><span class="delete">-</span>
        </div>
    </li>
</ul>
<?php
}

//Define the insides of the metabox
function save_metabox(){
   submit_button();
}

以下是我的类GeneratePageAdmin:

的内容
class GeneratePageAdmin
{
    var $hook;
    var $title;
    var $menu;
    var $permissions;
    var $slug;
    var $page;
/**
* Constructor class for the Simple Admin Metabox
*@param $hook - (string) parent page hook
*@param $title - (string) the browser window title of the page
*@param $menu - (string) the page title as it appears in the menuk
*@param $permissions - (string) the capability a user requires to see the page
*@param $slug - (string) a slug identifier for this page
*@param $body_content_cb - (callback) (optional) a callback that prints to the page, above the metaboxes. See the tutorial for more details.
*/
function __construct($hook, $title, $menu, $permissions, $slug, $body_content_cb='__return_true'){
    $this->hook = $hook;
    $this->title = $title;
    $this->menu = $menu;
    $this->permissions = $permissions;
    $this->slug = $slug;
    $this->body_content_cb = $body_content_cb;
    /* Add the page */
    add_action('admin_menu', array($this,'add_page'));
}
/**
* Adds the custom page.
* Adds callbacks to the load-* and admin_footer-* hooks
*/
function add_page(){
    /* Add the page */
    $this->page = add_submenu_page($this->hook,$this->title, $this->menu, $this->permissions,$this->slug, array($this,'render_page'),10);
    //var_dump($this->page);
    /* Add callbacks for this screen only */
    add_action('load-'.$this->page, array($this,'page_actions'),9);
    add_action('admin_footer-'.$this->page,array($this,'footer_scripts'));
}
/**
* Prints the jQuery script to initiliase the metaboxes
* Called on admin_footer-*
*/
function footer_scripts(){
    ?>
    <script> postboxes.add_postbox_toggles(pagenow);</script>
    <?php
}
/*
* Actions to be taken prior to page loading. This is after headers have been set.
* call on load-$hook
* This calls the add_meta_boxes hooks, adds screen options and enqueues the postbox.js script.
*/
function page_actions(){
    do_action('add_meta_boxes_'.$this->page, null);
    do_action('add_meta_boxes', $this->page, null);
    /* User can choose between 1 or 2 columns (default 2) */
    add_screen_option('layout_columns', array('max' => 2, 'default' => 2) );
    /* Enqueue WordPress' script for handling the metaboxes */
    wp_enqueue_script('postbox');
}
/**
* Renders the page
*/
function render_page(){
    ?>
    <div class="wrap" id="<?php echo $this->slug; ?>">

        <?php screen_icon(); ?>

        <h2> <?php echo esc_html($this->title);?> </h2>

        <form method="post">
            <?php settings_fields( $this->slug.'-settings' ); ?>
            <?php do_settings_sections( $this->slug.'-settings' ); ?>
            <input type="hidden" name="action" value="some-action">
            <?php wp_nonce_field( 'some-action-nonce' );
            /* Used to save closed metaboxes and their order */
            wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
            wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>

            <div id="poststuff">
                <div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">

                    <div id="post-body-content">
                        <?php call_user_func($this->body_content_cb); ?>
                    </div>

                    <div id="postbox-container-1" class="postbox-container">
                        <?php do_meta_boxes('','side',null); ?>
                    </div>

                    <div id="postbox-container-2" class="postbox-container">
                        <?php do_meta_boxes('','normal',null); ?>
                        <?php do_meta_boxes('','advanced',null); ?>
                    </div>

                </div> <!-- #post-body -->
            </div> <!-- #poststuff -->

        </form>

    </div><!-- .wrap -->
    <?php
}
}

对于长代码很抱歉,但我真的不知道问题出在哪里。

感谢您的帮助; - )

2 个答案:

答案 0 :(得分:0)

我说问题在于你实例化课程的方式。

使用钩子将其实例化为:

add_action( 'after_setup_theme', array( 'GeneratePageAdmin', 'init' ) );

当然,创建新方法init,并在那里开展工作。

如果你正在创建一个选项页面,那么钩子很可能是管理员,而不是after_setup_theme,但你应该明白这个想法。

我会做的是这样的事情:

public static function get_instance() {

    // create an object
    NULL === self::$instance and self::$instance = new self;

    // return the object
    return self::$instance; 

}

然后,通过Wordpress钩子实例化它:

add_action( 'after_setup_theme', array( GeneratePageAdmin::get_instance(), 'init' ) );

然后,您应该可以在班级中使用$this

答案 1 :(得分:0)

最后,我只需要在表单的标题中添加“action = options.php”,并删除settings_fields函数自动处理的这一部分。

<input type="hidden" name="action" value="some-action">
<?php wp_nonce_field( 'some-action-nonce' ); ?>