Skip to content

Instantly share code, notes, and snippets.

@man4toman
Created October 22, 2018 09:48
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 man4toman/ec13533ea4545b4b9688f27dedd9f80c to your computer and use it in GitHub Desktop.
Save man4toman/ec13533ea4545b4b9688f27dedd9f80c to your computer and use it in GitHub Desktop.
Add custom widget to WordPress dashboard
<?php
add_action( 'wp_dashboard_setup', 'my_custom_dashboard' );
function my_custom_dashboard() {
global $wp_meta_boxes;
wp_add_dashboard_widget( 'mycustomwidget', 'My Custom Widget', 'display_mycustomwidget' );
}
function display_mycustomwidget() {
if( isset( $_POST['mycustomwidget_nonce'] ) ){
if( wp_verify_nonce( $_POST['mycustomwidget_nonce'], 'mycustomwidget_action' ) || check_admin_referer( 'mycustomwidget_action', 'mycustomwidget_nonce' ) ){
update_option( "my_text", sanitize_text_field( $_POST["my_text"] ) );
}
}
?>
<form method="post">
Hello Dear!
<input type="text" name="my_text" id="my_text" placeholder="Enter the text" value="<?php echo esc_html( get_option('my_text') ); ?>" />
<input type="submit" name="save_my_text" id="save_my_text" class="button primary-button" value="Save it" />
<?php wp_nonce_field( 'mycustomwidget_action', 'mycustomwidget_nonce'); ?>
</form>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment