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"].
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.