注册一个设置并存储在数组中 - Wordpress

时间:2018-04-18 09:32:37

标签: php arrays wordpress options

我在前端的表单中有一个select标记。 此select的值必须是用户在Backend中的插件选项中存储在数组中的选项。 我已经创建了选项页面,并使用了register_setting()选项,但我无法创建一个值数组。

我需要创建一个包含事件名称,开始时间和结束时间的数组,而不是必须在同一页面的表格中打印所有值。

这里的代码到目前为止

function tnt_custom_settings(){
  register_setting( 'tnt-settings-group', 'event_name' );

  add_settings_section( 'tnt-add-new-event-section', 'Aggiungi Evento', 'tnt_add_event_field', 'tnt_options' );

  add_settings_field( 'event_name', 'Nome Evento', 'tnt_event_name', 'tnt_options', 'tnt-add-new-event-section', array( 'event_name' ) );
}

function tnt_event_name(){
  $EventName = esc_attr( get_option( 'event_name' ) );
  echo "<input type='text' name='event_name' value='".$EventName."' placeholder='nome evento'>";
}

1 个答案:

答案 0 :(得分:0)

根据我理解你的问题,我尝试过这样可以帮助你。

如果它正常工作,请检查下面的答案。

<?php
add_action('admin_menu', 'my_options_create_menu');
function my_options_create_menu() {

    //create new top-level menu
    add_menu_page('options', 'options', 'administrator', __FILE__, 'my_settings_page' , plugins_url('/images/icon.png', __FILE__) );

    //call register settings function
    add_action( 'admin_init', 'register_my_options_settings' );
}


function register_my_options_settings() {
    //register our settings
    register_setting( 'tnt-settings-group', 'event_name' );
    register_setting( 'tnt-settings-group', 'start_time' );
    register_setting( 'tnt-settings-group', 'end_time' );
}

function my_settings_page() {
?>
<div class="wrap">
<h1>My Option</h1>

<form method="post" action="options.php">
    <?php settings_fields( 'tnt-settings-group' ); ?>
    <?php do_settings_sections( 'tnt-settings-group' ); ?>
    <table class="form-table">
        <tr valign="top">
        <th scope="row">New Option Name</th>
        <td><input type="text" name="event_name" value="<?php echo esc_attr( get_option('event_name') ); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">Some Other Option</th>
        <td><input type="text" name="start_time" value="<?php echo esc_attr( get_option('start_time') ); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">Options, Etc.</th>
        <td><input type="text" name="end_time" value="<?php echo esc_attr( get_option('end_time') ); ?>" /></td>
        </tr>
    </table>    
    <?php submit_button(); ?>
</form>
</div>
<?php } ?>