1

プラグインの register_settings を少しきれいにしようとしています。これで次のようになります。

add_action( 'admin_init', 'hpblogposts_register_settings' );
function hpblogposts_register_settings() {
    register_setting( 'hpblogposts_settings_options_group', 'linkid01' );
    register_setting( 'hpblogposts_settings_options_group', 'linktext01' );
    register_setting( 'hpblogposts_settings_options_group', 'linkalt01' );
    register_setting( 'hpblogposts_settings_options_group', 'linkid02' );
    register_setting( 'hpblogposts_settings_options_group', 'linktext02' );
    register_setting( 'hpblogposts_settings_options_group', 'linkalt02' );
    register_setting( 'hpblogposts_settings_options_group', 'linkid03' );
    register_setting( 'hpblogposts_settings_options_group', 'linktext03' );
    register_setting( 'hpblogposts_settings_options_group', 'linkalt03' );
    register_setting( 'hpblogposts_settings_options_group', 'linkid04' );
    register_setting( 'hpblogposts_settings_options_group', 'linktext04' );
    register_setting( 'hpblogposts_settings_options_group', 'linkalt04' );
    register_setting( 'hpblogposts_settings_options_group', 'linkid05' );
    register_setting( 'hpblogposts_settings_options_group', 'linktext05' );
    register_setting( 'hpblogposts_settings_options_group', 'linkalt05' );
    register_setting( 'hpblogposts_settings_options_group', 'linkid06' );
    register_setting( 'hpblogposts_settings_options_group', 'linktext06' );
    register_setting( 'hpblogposts_settings_options_group', 'linkalt06' );
}

設定の「グループ」ごとに、register_settings が 3 回呼び出されます。私はそれを簡単にしようとしていました:

add_action( 'admin_init', 'hpblogposts_register_settings' );
function csHpBlogPostsSimplyRegisterSettings($id) {
    register_setting( 'hpblogposts_settings_options_group', 'linkid'+$id );
    register_setting( 'hpblogposts_settings_options_group', 'linktext'+$id );
    register_setting( 'hpblogposts_settings_options_group', 'linkalt'+$id );
}
function hpblogposts_register_settings() {
    csHpBlogPostsSimplyRegisterSettings('01');
    csHpBlogPostsSimplyRegisterSettings('02');
    csHpBlogPostsSimplyRegisterSettings('03');
    csHpBlogPostsSimplyRegisterSettings('04');
    csHpBlogPostsSimplyRegisterSettings('05');
    csHpBlogPostsSimplyRegisterSettings('06');
}

しかし、この方法では機能しません (値は保存されません)。

どこで間違いを犯したのでしょうか?

4

1 に答える 1

1

たとえば、文字列を連結するときは、プラスではなくドットを使用する必要があります。

register_setting( 'hpblogposts_settings_options_group', 'linkalt' . $id );
于 2013-05-08T13:44:58.130 に答える