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

settings api - Splitting do_settings_sections within HTML?

programmeradmin1浏览0评论

This seems like it should be very simple to achieve, but I have spent far too long trying to figure it out. I have two settings sections:

add_settings_section( 'cp_required_info', 'One', array($this, 'cp_required_info'), 'cp-settings' );
add_settings_section( 'cp_sync_info', 'Two', array($this, 'cp_sync_info'), 'cp-settings' );
register_setting( 'cp-settings', 'cp_admin_options', array( $this, 'sanitize' ) );

How can I get these sections to output in different locations in the HTML? do_settings_sections is only allowing $page. I have tried do_settings_fields which allows for $page and $section and it does achieve what I want, however when I remove the do_settings_section and leave only the do_settings_fields, all of my output disappears. Below is an example of what I am trying to achieve.

<?php settings_fields( 'cp-settings' );   
//do_settings_sections( 'cp-settings' );
do_settings_fields( 'cp-settings', 'cp_required_info' );?>

<!--Some HTML goes here-->

<?php
do_settings_fields( 'cp-settings', 'cp_sync_info' );?>
?>

Does anyone know how to achieve this? All of my callbacks are functioning properly, its just a matter of placing the sections on the page.

EDIT: Including the add_settings_fields:

public function cp_required_info() {
        add_settings_field( 'cp_apikey', 'API Key:', array( $this, 'cp_apikey_callback' ), 'cp-settings', 'cp_required_info', array( 'label_for' => 'cp_apikey') );      
        add_settings_field( 'cp_username', 'Username:', array( $this, 'cp_username_callback' ), 'cp-settings', 'cp_required_info', array( 'label_for' => 'cp_username') );      
        add_settings_field( 'cp_tablename', 'Table Name:', array( $this, 'cp_tablename_callback' ), 'cp-settings', 'cp_required_info', array( 'label_for' => 'cp_tablename') );      
    }

public function cp_sync_info() {
        add_settings_field('cp_collect_posts', 'Posts', array($this, 'cp_posts_callback'), 'cp-settings', 'cp_sync_info', array( 'label_for' => 'cp_collect_posts') );
    }

This seems like it should be very simple to achieve, but I have spent far too long trying to figure it out. I have two settings sections:

add_settings_section( 'cp_required_info', 'One', array($this, 'cp_required_info'), 'cp-settings' );
add_settings_section( 'cp_sync_info', 'Two', array($this, 'cp_sync_info'), 'cp-settings' );
register_setting( 'cp-settings', 'cp_admin_options', array( $this, 'sanitize' ) );

How can I get these sections to output in different locations in the HTML? do_settings_sections is only allowing $page. I have tried do_settings_fields which allows for $page and $section and it does achieve what I want, however when I remove the do_settings_section and leave only the do_settings_fields, all of my output disappears. Below is an example of what I am trying to achieve.

<?php settings_fields( 'cp-settings' );   
//do_settings_sections( 'cp-settings' );
do_settings_fields( 'cp-settings', 'cp_required_info' );?>

<!--Some HTML goes here-->

<?php
do_settings_fields( 'cp-settings', 'cp_sync_info' );?>
?>

Does anyone know how to achieve this? All of my callbacks are functioning properly, its just a matter of placing the sections on the page.

EDIT: Including the add_settings_fields:

public function cp_required_info() {
        add_settings_field( 'cp_apikey', 'API Key:', array( $this, 'cp_apikey_callback' ), 'cp-settings', 'cp_required_info', array( 'label_for' => 'cp_apikey') );      
        add_settings_field( 'cp_username', 'Username:', array( $this, 'cp_username_callback' ), 'cp-settings', 'cp_required_info', array( 'label_for' => 'cp_username') );      
        add_settings_field( 'cp_tablename', 'Table Name:', array( $this, 'cp_tablename_callback' ), 'cp-settings', 'cp_required_info', array( 'label_for' => 'cp_tablename') );      
    }

public function cp_sync_info() {
        add_settings_field('cp_collect_posts', 'Posts', array($this, 'cp_posts_callback'), 'cp-settings', 'cp_sync_info', array( 'label_for' => 'cp_collect_posts') );
    }
Share Improve this question edited Oct 20, 2015 at 15:42 T Andrew 501 silver badge6 bronze badges asked Oct 20, 2015 at 15:13 user82238user82238 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

Nevermind, figured it out. Made the following changes:

add_settings_section( 'cp_required_info', null, array($this, 'cp_required_info'), 'cp-settings-group' );
add_settings_section( 'cp_sync_info', null, array($this, 'cp_sync_info'), 'cp-settings-group' );
register_setting( 'cp-settings-group', 'cp_admin_options', array( $this, 'sanitize' ) );

<?php settings_fields( 'cp-settings-group' );   
do_settings_sections( 'cp-settings-group' );
do_settings_fields( 'cp-settings', 'cp_required_info' );?>

<!--Some HTML goes here-->

<?php
do_settings_fields( 'cp-settings', 'cp_sync_info' );?>
?>
发布评论

评论列表(0)

  1. 暂无评论