Halloween Sale 15% off Ends in See the plans
PHP

Notice Box Shortcode

Adds a [notice] shortcode for colored info, success, warning and error boxes that make tips and important warnings stand out in your content.

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

Code

add_shortcode( 'notice', function ( $atts, $content = '' ) {
    $atts   = shortcode_atts( array( 'type' => 'info', 'title' => '' ), $atts, 'notice' );
    $colors = array(
        'info'    => array( '#f0f6fc', '#2271b1' ),
        'success' => array( '#edfaef', '#00a32a' ),
        'warning' => array( '#fcf9e8', '#dba617' ),
        'error'   => array( '#fcf0f1', '#d63638' ),
    );
    $type   = isset( $colors[ $atts['type'] ] ) ? $atts['type'] : 'info';
    $style  = sprintf( 'margin:1.5em 0;padding:1em 1.25em;background:%s;border-left:4px solid %s;color:#1d2327;border-radius:2px;', $colors[ $type ][0], $colors[ $type ][1] );
    $title  = '' !== $atts['title'] ? '<p style="margin:0 0 .4em;"><strong>' . esc_html( $atts['title'] ) . '</strong></p>' : '';

    return '<div class="wpide-notice wpide-notice-' . esc_attr( $type ) . '" role="note" style="' . esc_attr( $style ) . '">' . $title . wpautop( do_shortcode( wp_kses_post( (string) $content ) ) ) . '</div>';
} );

Notes

Example: [notice type="warning" title="Heads up"]Back up first.[/notice]. Types are info, success, warning and error.

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.