Abschnitte und Registerkarten DRY – WordPress Einstellungen API

Ich habe eine benutzerdefinierte Einstellungsseite mit 3 Einstellungsbereichen unter 3 Registerkarten erstellt. Jedoch bin ich ein totaler Anfänger, wenn es um PHP geht und ich habe das Gefühl, dass ich viel unnötigen, wiederholten Code geschrieben habe. Meine Einstellungsfelder sind unter den verschiedenen Registerkarten identisch, aber ich habe keine andere Möglichkeit gefunden, sie separat in der database zu speichern. Gibt es eine Möglichkeit, den Code zum Beispiel mit einem Array zu verkürzen? Wie könnte ich das gleiche Einstellungsfeld in einem separaten Tab wiederverwenden?

Ich werde dankbar für Nachhilfe sein.

Hier ist der Code aus meiner functions.php:

add_action('admin_menu', 'myTheme_admin'); function myTheme_admin() { /* Base Menu */ add_menu_page( 'myTheme settings', // Admin page title 'myTheme settings', // Admin menu label 'manage_options', 'myTheme-general-options', // Admin slug 'myTheme_general_index'); // Display Page } add_action('admin_init', 'myTheme_options'); function myTheme_options() { /* First Box Options Section */ add_settings_section( 'myTheme_first_box', // ID used to identify this section and with which to register options 'First box settings', 'boxes_front_page_callback', // Callback used to render the description of the section 'first_box_option' // Page on which to add this section of options ); add_settings_field( 'box_first_title', // ID used to identify the field throughout the theme 'Title', // The label to the left of the option interface element 'box_first_input_callback', 'first_box_option', // The page on which this option will be added 'myTheme_first_box', // ID of the section array( 'box_first_title' ) ); add_settings_field( 'box_first_description', 'Description', 'box_first_desc_callback', 'first_box_option', 'myTheme_first_box', array( 'box_first_description' ) ); add_settings_field( 'box_first_link', 'Link', 'box_first_input_callback', 'first_box_option', 'myTheme_first_box', array( 'box_first_link' ) ); /*------------------------------ Second Box Options Section -------------------------------- */ add_settings_section( 'myTheme_second_box', 'Ustawienia boksu drugiego', 'boxes_front_page_callback', // Callback used to render the description of the section 'second_box_option' ); add_settings_field( 'box_second_title', 'Title', 'box_second_input_callback', 'second_box_option', 'myTheme_second_box', array( 'box_second_title' ) ); add_settings_field( 'box_second_description', 'Description', 'box_second_desc_callback', 'second_box_option', 'myTheme_second_box', array( 'box_second_description' ) ); add_settings_field( 'box_second_link', 'Link', 'box_second_input_callback', 'second_box_option', 'myTheme_second_box', array( 'box_second_link' ) ); /*------------------------------ Third Box Options Section -------------------------------- */ add_settings_section( 'myTheme_third_box', 'Ustawienia boksu trzeciego', 'boxes_front_page_callback', // Callback used to render the description of the section 'third_box_option' ); add_settings_field( 'box_third_title', 'Title', 'box_third_input_callback', 'third_box_option', 'myTheme_third_box', array( 'box_third_title' ) ); add_settings_field( 'box_third_description', 'Description', 'box_third_desc_callback', 'third_box_option', 'myTheme_third_box', array( 'box_third_description' ) ); add_settings_field( 'box_third_link', 'Link', 'box_third_input_callback', 'third_box_option', 'myTheme_third_box', array( 'box_third_link' ) ); register_setting('first_box_option', 'first_box_option'); register_setting('second_box_option', 'second_box_option'); register_setting('third_box_option', 'third_box_option'); } /* Call Backs -----------------------------------------------------------------*/ function boxes_front_page_callback() { echo '

Lorem ipsum

'; } function box_first_input_callback($args) { $options = get_option('first_box_option'); echo ''; } function box_second_input_callback($args) { $options = get_option('second_box_option'); echo ''; } function box_third_input_callback($args) { $options = get_option('third_box_option'); echo ''; } function box_first_desc_callback($args) { $options = get_option('first_box_option'); echo ''; } function box_second_desc_callback($args) { $options = get_option('second_box_option'); echo ''; } function box_third_desc_callback($args) { $options = get_option('third_box_option'); echo ''; } /* Display Page -----------------------------------------------------------------*/ function myTheme_general_index() { ?>

myTheme Settings

<?php }

Solutions Collecting From Web of "Abschnitte und Registerkarten DRY – WordPress Einstellungen API"

Nun, Sie könnten so etwas tun: Erstellen Sie ein assoziatives Array für Einstellungen und durchlaufen Sie sie, um Einstellungen und Abschnitte für jedes Feld zu erstellen.

 $settings = array( 'setting_1_id' => array( 'title'=>'First Box Settings', 'page'=>'first_box_option', 'fields'=> array( array( 'id'=> 'box_first_title', 'title'=>'Title', 'callback'=> 'text_callback' ), array( 'id'=> 'box_first_desc', 'title'=>'Description', 'callback'=> 'textarea_callback' ), array( 'id'=> 'box_first_link', 'title'=>'Link', 'callback'=> 'text_callback' ), ) ), 'setting_2_id' => array( 'title'=>'Second Box Settings', 'page'=>'second_box_option', 'fields'=> array( array( 'id'=> 'box_second_title', 'title'=>'Title', 'callback'=> 'text_callback' ), array( 'id'=> 'box_second_desc', 'title'=>'Description', 'callback'=> 'textarea_callback' ), array( 'id'=> 'box_second_link', 'title'=>'Link', 'callback'=> 'text_callback' ), ) ), 'setting_3_id' => array( 'title'=>'Third Box Settings', 'page'=>'third_box_option', 'fields'=> array( array( 'id'=> 'box_third_title', 'title'=>'Title', 'callback'=> 'text_callback' ), array( 'id'=> 'box_third_desc', 'title'=>'Description', 'callback'=> 'textarea_callback' ), array( 'id'=> 'box_third_link', 'title'=>'Link', 'callback'=> 'text_callback' ), ) ), ); 

Dann können Sie die foreach verwenden, um die einzelnen Einstellungen zu durchlaufen.

 foreach( $settings as $id => $values){ add_settings_section( $id, // ID used to identify this section and with which to register options $values['title'], 'boxes_front_page_callback', // Callback used to render the description of the section $values['page'] // Page on which to add this section of options ); // Loop through the fields to add different fields foreach ($values['fields'] as $field) { add_settings_field( $field['id'], // ID used to identify the field throughout the theme $field['title'], // The label to the left of the option interface element $field['callback'], $values['page'], // The page on which this option will be added $id, // ID of the section array( $values['page'], //option name $field['title'] //id ) ); } register_setting($values['page'], $values['page']); } 

Und schließlich brauchen Sie nur drei callbacke, für Text, Textarea und Frontpage

 /* Call Backs */ function boxes_front_page_callback() { echo '

Lorem ipsum

'; } function text_callback($args) { $options = get_option($args[0]); echo ''; } function textarea_callback($args) { $options = get_option($args[0]); echo ''; }

Ich habe das bereits getestet, funktioniert wie vorgesehen. Klicken Sie hier , um den core des vollständigen Codes zu sehen