1. Herzlich willkommen bei WPDE.org, dem grössten und ältesten deutschsprachigen Community-Forum rund um das Thema WordPress. Du musst angemeldet oder registriert sein, um Beiträge verfassen zu können.
    Information ausblenden

Text-Input Feld in Plugin Options Page registrieren.

Dieses Thema im Forum "Plugins und Widgets" wurde erstellt von achimhecht, 6. April 2021.

  1. achimhecht

    achimhecht Member

    Registriert seit:
    25. März 2013
    Beiträge:
    11
    Zustimmungen:
    0
    Anfängerfrage: Ich versuche aufgrund des Beispiels auf wordpress.org eine Options Page für mein Plugin zu realisieren - klappt auch mit den "Select" Feldern... die Optionsfelder "Diaspora" und "Postpone" werden korrekt behandelt. Aber für ein simples Textfeld ("localecode") bekomme ich es (noch) nicht hin. Was mache ich falsch:


    <?php
    /**
    * custom option and settings
    */
    function wpj_settings_init() {
    // Register a new setting for "wpj" page.
    register_setting( 'wpj', 'wpj_options' );

    // Register a new section in the "wpj" page.
    add_settings_section(
    'wpj_section_developers',
    __( 'Basic Configuration', 'wpj' ), 'wpj_section_developers_callback',
    'wpj'
    );

    // Register fields in the "wpj_section_developers" section, inside the "wpj" page.
    add_settings_field(
    'wpj_field_localecode', // As of WP 4.6 this value is used only internally.
    // Use $args' label_for to populate the id inside the callback.
    __( 'Locale Code', 'wpj' ),
    'wpj_field_localwcode_cb',
    'wpj',
    'wpj_section_developers',
    array(
    'label_for' => 'wpj_field_localecode',
    'class' => 'wpj_row',
    'wpj_custom_data' => 'custom',
    )
    );

    add_settings_field(
    'wpj_field_diaspora', // As of WP 4.6 this value is used only internally.
    // Use $args' label_for to populate the id inside the callback.
    __( 'Diaspora', 'wpj' ),
    'wpj_field_diaspora_cb',
    'wpj',
    'wpj_section_developers',
    array(
    'label_for' => 'wpj_field_diaspora',
    'class' => 'wpj_row',
    'wpj_custom_data' => 'custom',
    )
    );
    add_settings_field(
    'wpj_field_postpone', // As of WP 4.6 this value is used only internally.
    // Use $args' label_for to populate the id inside the callback.
    __( 'Postpone Holidays', 'wpj' ),
    'wpj_field_postpone_cb',
    'wpj',
    'wpj_section_developers',
    array(
    'label_for' => 'wpj_field_postpone',
    'class' => 'wpj_row',
    'wpj_custom_data' => 'custom',
    )
    );

    }

    /**
    * Register wpj_settings_init to the admin_init action hook.
    */
    add_action( 'admin_init', 'wpj_settings_init' );


    /**
    * Custom option and settings:
    * - callback functions
    */


    /**
    * Developers section callback function.
    *
    * @param array $args The settings array, defining title, id, callback.
    */
    function wpj_section_developers_callback( $args ) {
    ?>
    <p id="<?php echo esc_attr( $args['id'] ); ?>"><?php esc_html_e( 'Edit your basic plugin settings here:', 'wpj' ); ?></p>
    <?php
    }

    /**
    * Locale code field callback function.
    */
    function wpj_field_localecode_cb( $args ) {
    // Get the value of the setting we've registered with register_setting()
    $options = get_option( 'wpj_options' );
    ?>
    <input
    id="<?php echo esc_attr( $args['label_for'] ); ?>"
    name="wpj_options[<?php echo esc_attr( $args['label_for'] ); ?>]"
    data-custom="<?php echo esc_attr( $args['wpj_custom_data'] ); ?>"
    size="8" type="text" value="de_DE.UTF8" />
    <p class="description">
    <?php esc_html_e( 'Edit your country locale (this affects the language of weekday and month names).', 'wpj' ); ?>
    </p>
    <?php
    }


    /**
    * Diaspora field callback function.
    */
    function wpj_field_diaspora_cb( $args ) {
    // Get the value of the setting we've registered with register_setting()
    $options = get_option( 'wpj_options' );
    ?>
    <select
    id="<?php echo esc_attr( $args['label_for'] ); ?>"
    data-custom="<?php echo esc_attr( $args['wpj_custom_data'] ); ?>"
    name="wpj_options[<?php echo esc_attr( $args['label_for'] ); ?>]">
    <option value="true" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], 'true', false ) ) : ( '' ); ?>>
    <?php esc_html_e( 'Yes', 'wpj' ); ?>
    </option>
    <option value="false" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], 'false', false ) ) : ( '' ); ?>>
    <?php esc_html_e( 'No', 'wpj' ); ?>
    </option>
    </select>
    <p class="description">
    <?php esc_html_e( 'Select "Yes" if you live in Galut, outside Israel or "No" if you live at Eretz Israel (this affects the durarion of some holidays).', 'wpj' ); ?>
    </p>
    <?php
    }
    /**
    * Postpone on Saturday field callback function.
    */
    function wpj_field_postpone_cb( $args ) {
    // Get the value of the setting we've registered with register_setting()
    $options = get_option( 'wpj_options' );
    ?>
    <select
    id="<?php echo esc_attr( $args['label_for'] ); ?>"
    data-custom="<?php echo esc_attr( $args['wpj_custom_data'] ); ?>"
    name="wpj_options[<?php echo esc_attr( $args['label_for'] ); ?>]">
    <option value="true" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], 'true', false ) ) : ( '' ); ?>>
    <?php esc_html_e( 'Yes', 'wpj' ); ?>
    </option>
    <option value="false" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], 'false', false ) ) : ( '' ); ?>>
    <?php esc_html_e( 'No', 'wpj' ); ?>
    </option>
    </select>
    <p class="description">
    <?php esc_html_e( 'Postbone holiday if it falls on a saturday.', 'wpj' ); ?>
    </p>
    <?php
    }

    /**
    * Add the top level menu page.
    */
    function wpj_options_page() {
    add_menu_page(
    'WP Jewify It',
    'WP Jewify It Options',
    'manage_options',
    'wpj',
    'wpj_options_page_html'
    );
    }


    /**
    * Register our wpj_options_page to the admin_menu action hook.
    */
    add_action( 'admin_menu', 'wpj_options_page' );


    /**
    * Top level menu callback function
    */
    function wpj_options_page_html() {
    // check user capabilities
    if ( ! current_user_can( 'manage_options' ) ) {
    return;
    }

    // add error/update messages

    // check if the user have submitted the settings
    // WordPress will add the "settings-updated" $_GET parameter to the url
    if ( isset( $_GET['settings-updated'] ) ) {
    // add settings saved message with the class of "updated"
    add_settings_error( 'wpj_messages', 'wpj_message', __( 'Settings Saved', 'wpj' ), 'updated' );
    }

    // show error/update messages
    settings_errors( 'wpj_messages' );
    ?>
    <div class="wrap">
    <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
    <form action="options.php" method="post">
    <?php
    // output security fields for the registered setting "wpj"
    settings_fields( 'wpj' );
    // output setting sections and their fields
    // (sections are registered for "wpj", each field is registered to a specific section)
    do_settings_sections( 'wpj' );
    // output save settings button
    submit_button( 'Save Settings' );
    ?>
    </form>
    </div>
    <?php
    }

     
  2. mensmaximus

    mensmaximus Well-Known Member

    Registriert seit:
    24. Juli 2014
    Beiträge:
    8.857
    Zustimmungen:
    437
    Bitte Programmcode als Code einfügen. Bitte erwarte nicht, dass Helfer den Code reformatieren um diesen vernünftig lesen zu können.
     
  3. achimhecht

    achimhecht Member

    Registriert seit:
    25. März 2013
    Beiträge:
    11
    Zustimmungen:
    0
    Sorry

    Code:
    Entschuldige, aber du musst dich registrieren oder anmelden um den Inhalt sehen zu können!
     
  4. Shadow

    Shadow Well-Known Member

    Registriert seit:
    12. Februar 2007
    Beiträge:
    2.969
    Zustimmungen:
    57
    dann dürfte wahrscheinlich
    wpj_field_localwcode_cb , nicht richtig geschrieben sein ?!?
     
    mensmaximus gefällt das.
  5. mensmaximus

    mensmaximus Well-Known Member

    Registriert seit:
    24. Juli 2014
    Beiträge:
    8.857
    Zustimmungen:
    437
    Nicht nur wahrscheinlich. Die callback Funktion gibt es nicht. W und e liegen nah beieinander :D
     
    Shadow gefällt das.
  6. achimhecht

    achimhecht Member

    Registriert seit:
    25. März 2013
    Beiträge:
    11
    Zustimmungen:
    0
    Danke für den Hinweis. Hatte ich schon selbst bemerkt und korrigiert... läuft aber trotzdem nicht...
     
  7. mensmaximus

    mensmaximus Well-Known Member

    Registriert seit:
    24. Juli 2014
    Beiträge:
    8.857
    Zustimmungen:
    437
    Der Code ist grausam zu lesen:

    Code:
    Entschuldige, aber du musst dich registrieren oder anmelden um den Inhalt sehen zu können!
    Value ist fest definiert. Das müsste aber $options['wpj_field_localecode_cb'] stehen.
     
  8. achimhecht

    achimhecht Member

    Registriert seit:
    25. März 2013
    Beiträge:
    11
    Zustimmungen:
    0
    Ja. Hab ich so aus wordpress.org übernommen und nur angepasst ;) Es war halt das erste Beispiel, das (mit den Selects) funktionierte.

    Das mit dem festen Wert hatte ich gemacht, um zu sehen, ob überhaupt etwas ausgegeben wird - habs entsprechend geändert... immer noch dasselbe Ergebnis:

    Screenshot 2021-04-08 10.51.00.png
     
  9. mensmaximus

    mensmaximus Well-Known Member

    Registriert seit:
    24. Juli 2014
    Beiträge:
    8.857
    Zustimmungen:
    437
    Gib das Textfeld einmal komplett festcodiert aus, also:
    Code:
    Entschuldige, aber du musst dich registrieren oder anmelden um den Inhalt sehen zu können!
     
  1. Diese Seite verwendet Cookies, um Inhalte zu personalisieren, diese deiner Erfahrung anzupassen und dich nach der Registrierung angemeldet zu halten.
    Wenn du dich weiterhin auf dieser Seite aufhältst, akzeptierst du unseren Einsatz von Cookies.
    Information ausblenden
  1. Diese Seite verwendet Cookies, um Inhalte zu personalisieren, diese deiner Erfahrung anzupassen und dich nach der Registrierung angemeldet zu halten.
    Wenn du dich weiterhin auf dieser Seite aufhältst, akzeptierst du unseren Einsatz von Cookies.
    Information ausblenden