Created
October 22, 2018 09:48
-
-
Save man4toman/ec13533ea4545b4b9688f27dedd9f80c to your computer and use it in GitHub Desktop.
Add custom widget to WordPress dashboard
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 | |
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