| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Ticket 28672 Samples |
|---|
| 4 | Author: Pete Nelson |
|---|
| 5 | Version: 1.0 |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | if ( !defined( 'ABSPATH' ) ) exit( 'restricted access' ); |
|---|
| 9 | |
|---|
| 10 | add_action( 'admin_init', 'ticket_28672_add_settings' ); |
|---|
| 11 | |
|---|
| 12 | function ticket_28672_add_settings() { |
|---|
| 13 | |
|---|
| 14 | global $ticket_28672_admin_notices; |
|---|
| 15 | $ticket_28672_admin_notices = array(); |
|---|
| 16 | |
|---|
| 17 | // add settings field to a valid built-in section |
|---|
| 18 | $results = add_settings_field( '_28672_general_default', 'Valid General/Default Field', 'ticket_28672_settings_field', 'general', 'default' ); |
|---|
| 19 | |
|---|
| 20 | if ( is_wp_error( $results ) ) |
|---|
| 21 | $ticket_28672_admin_notices['invalid'][] = $results->get_error_message(); |
|---|
| 22 | else |
|---|
| 23 | $ticket_28672_admin_notices['valid'][] = 'Valid General/Default field added.'; |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | // add settings field to a non-existent built-in section |
|---|
| 27 | $results = add_settings_field( '_28672_general_invalid', 'Invalid General Section Field', 'ticket_28672_settings_field', 'general', 'non-existent-section' ); |
|---|
| 28 | |
|---|
| 29 | if ( is_wp_error( $results ) ) |
|---|
| 30 | $ticket_28672_admin_notices['invalid'][] = $results->get_error_message(); |
|---|
| 31 | else |
|---|
| 32 | $ticket_28672_admin_notices['valid'][] = 'General Invalid Section field added.'; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | // add a settings section to a built-in page |
|---|
| 36 | add_settings_section( 'new_section', 'New General Section', 'ticket_28672_settings_section', 'general' ); |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | // add settings field to the new section |
|---|
| 40 | $results = add_settings_field( '_28672_general_new_section_field', 'Valid General/New Section Field', 'ticket_28672_settings_field', 'general', 'new_section' ); |
|---|
| 41 | |
|---|
| 42 | if ( is_wp_error( $results ) ) |
|---|
| 43 | $ticket_28672_admin_notices['invalid'][] = $results->get_error_message(); |
|---|
| 44 | else |
|---|
| 45 | $ticket_28672_admin_notices['valid'][] = 'Valid General/New Section field added.'; |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | // add a settings field to a theoretical plugin page |
|---|
| 49 | $results = add_settings_field( '__28672_plugin_field', 'New Plugin Field', 'ticket_28672_settings_field', 'ticket-28672-page', 'my-plugin-section' ); |
|---|
| 50 | |
|---|
| 51 | if ( is_wp_error( $results ) ) |
|---|
| 52 | $ticket_28672_admin_notices['invalid'][] = $results->get_error_message(); |
|---|
| 53 | else |
|---|
| 54 | $ticket_28672_admin_notices['valid'][] = 'My Plugin Section/New Plugin Field Added (before section was defined)'; |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | // whoops! let's add the settings section for the theoretical plugin, then add the field |
|---|
| 58 | add_settings_section( 'my-plugin-section', 'My Plugin Section', 'ticket_28672_settings_section', 'ticket-28672-page' ); |
|---|
| 59 | $results = add_settings_field( '__28672_plugin_field', 'New Plugin Field', 'ticket_28672_settings_field', 'ticket-28672-page', 'my-plugin-section' ); |
|---|
| 60 | |
|---|
| 61 | if ( is_wp_error( $results ) ) |
|---|
| 62 | $ticket_28672_admin_notices['invalid'][] = $results->get_error_message(); |
|---|
| 63 | else |
|---|
| 64 | $ticket_28672_admin_notices['valid'][] = 'My Plugin Section/New Plugin Field Added (after section was defined)'; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | // display the results |
|---|
| 69 | add_action( 'admin_notices', 'ticket_28672_display_admin_notices' ); |
|---|
| 70 | |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | function ticket_28672_settings_section( $args ) { |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | function ticket_28672_settings_field() { |
|---|
| 79 | echo 'Ticket 28672 Input Field Placeholder'; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | function ticket_28672_display_admin_notices( ) { |
|---|
| 84 | |
|---|
| 85 | global $ticket_28672_admin_notices; |
|---|
| 86 | |
|---|
| 87 | foreach ( $ticket_28672_admin_notices['valid'] as $message ) { |
|---|
| 88 | ?> |
|---|
| 89 | <div class="updated"> |
|---|
| 90 | <p>Ticket 28672: <?php echo esc_attr( $message ); ?></p> |
|---|
| 91 | </div> |
|---|
| 92 | <?php |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | foreach ( $ticket_28672_admin_notices['invalid'] as $message ) { |
|---|
| 96 | ?> |
|---|
| 97 | <div class="error"> |
|---|
| 98 | <p>Ticket 28672: <?php echo esc_attr( $message ); ?></p> |
|---|
| 99 | </div> |
|---|
| 100 | <?php |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | } |
|---|