Halloween Sale 15% off Ends in See the plans
PHP

Simple Site Details Settings Page

Adds Settings > Site Details where you can store your phone number, address and opening hours once, then show them anywhere with [wpide_site_detail key="phone"].

Don’t have WPIDE yet? Install it on your site

Code

$wpide_site_fields = array(
    'phone'   => 'Phone number',
    'email'   => 'Contact email',
    'address' => 'Address',
    'hours'   => 'Opening hours',
);

add_action( 'admin_init', function () use ( $wpide_site_fields ) {
    register_setting( 'wpide_site_details', 'wpide_site_details', array(
        'type'              => 'array',
        'default'           => array(),
        'sanitize_callback' => function ( $input ) use ( $wpide_site_fields ) {
            $clean = array();

            foreach ( array_keys( $wpide_site_fields ) as $key ) {
                $clean[ $key ] = isset( $input[ $key ] ) ? sanitize_textarea_field( $input[ $key ] ) : '';
            }

            return $clean;
        },
    ) );
} );

add_action( 'admin_menu', function () use ( $wpide_site_fields ) {
    add_options_page( 'Site Details', 'Site Details', 'manage_options', 'wpide-site-details', function () use ( $wpide_site_fields ) {
        $values = (array) get_option( 'wpide_site_details', array() );

        echo '<div class="wrap"><h1>Site Details</h1><form method="post" action="options.php">';
        settings_fields( 'wpide_site_details' );
        echo '<table class="form-table" role="presentation">';

        foreach ( $wpide_site_fields as $key => $label ) {
            printf(
                '<tr><th scope="row"><label for="wpide-%1$s">%2$s</label></th><td><textarea class="large-text" rows="2" id="wpide-%1$s" name="wpide_site_details[%1$s]">%3$s</textarea><p class="description"><code>[wpide_site_detail key="%1$s"]</code></p></td></tr>',
                esc_attr( $key ),
                esc_html( $label ),
                esc_textarea( isset( $values[ $key ] ) ? $values[ $key ] : '' )
            );
        }

        echo '</table>';
        submit_button();
        echo '</form></div>';
    } );
} );

add_shortcode( 'wpide_site_detail', function ( $atts ) {
    $atts   = shortcode_atts( array( 'key' => '' ), $atts, 'wpide_site_detail' );
    $values = (array) get_option( 'wpide_site_details', array() );
    $value  = isset( $values[ $atts['key'] ] ) ? $values[ $atts['key'] ] : '';

    return nl2br( esc_html( $value ) );
} );

Notes

Edit $fields to add more details. Values are stored in the wpide_site_details option.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.