Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active July 22, 2016 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/20e5b5940e3639600173145087834f38 to your computer and use it in GitHub Desktop.
Save tommcfarlin/20e5b5940e3639600173145087834f38 to your computer and use it in GitHub Desktop.
[WordPress] Displaying Custom Messages in WordPress, Part 2
<div class="wrap">
<h1>Acme Options Pages</h1>
<?php do_action( 'get_acme_settings_messages' ); ?>
<form method="post" action="<?php echo esc_html( admin_url( 'admin-post.php' ) ); ?>">
<!-- TODO: This is where we render the rest of the page. -->
<?php
wp_nonce_field( 'yhd-settings-save', 'yellow-house-design-directory-importer' );
submit_button();
?>
</form>
</div><!-- .wrap -->
<?php
public function init() {
add_action(
'get_acme_settings_messages',
array( $this, 'get_messages' )
);
}
<?php
public function get_messages() {
$this->get_errors();
$this->display_errors();
}
<?php
private function get_errors() {
$this->errors = get_option( 'acme-errors' );
delete_option( 'acme-errors' );
}
<?php
private function display_errors() {
if ( empty( $this->errors ) ) {
return;
}
// Create the list of errors.
$errors = '<ul>';
foreach ( $this->errors as $error ) {
$errors .= '<li>' . $error . '</li>';
}
$errors .= '</ul>';
// Create the markup for the error message.
$html = "
<div class='notice notice-error'>
$errors
</div><!-- .notice-error -->
";
// Set the HTML we'll allow for sanitization.
$allowed_html = array(
'div' => array(
'class' => array(),
),
'ul' => array(),
'li' => array(),
);
echo wp_kses( $html, $allowed_html );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment