Last active
July 22, 2016 12:28
-
-
Save tommcfarlin/20e5b5940e3639600173145087834f38 to your computer and use it in GitHub Desktop.
[WordPress] Displaying Custom Messages in WordPress, Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function init() { | |
add_action( | |
'get_acme_settings_messages', | |
array( $this, 'get_messages' ) | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function get_messages() { | |
$this->get_errors(); | |
$this->display_errors(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
private function get_errors() { | |
$this->errors = get_option( 'acme-errors' ); | |
delete_option( 'acme-errors' ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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