Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Last active November 26, 2018 13:13
Show Gist options
  • Save lgedeon/8b3cb0825a0c10b4178374f389d42e71 to your computer and use it in GitHub Desktop.
Save lgedeon/8b3cb0825a0c10b4178374f389d42e71 to your computer and use it in GitHub Desktop.
The Go Button can do anything with a click of a button! Adds a button to the WordPress Dashboard, that fires an action hook "gobutton_clicked". Add code in your theme or plugin to respond to that action and it will fire any time someone clicks.
<?php
/**
Plugin Name: Go Button
Plugin URI: https://gist.github.com/lgedeon/8b3cb0825a0c10b4178374f389d42e71
Description: The Go Button can do anything with a click of a button! Adds a button to the WordPress Dashboard, that fires an action hook "gobutton_clicked". Add code in your theme or plugin to respond to that action and it will fire any time someone clicks.
Version: 0.3
Author: lgedeon
Author URI: https://github.com/lgedeon
License: GPLv2 or later
Text Domain: gobutton
*/
// This can easily used as a drop-in inside themes or other plugins. As such it could be included multiple times.
if ( ! function_exists( 'gobutton_dashboard_widget_callback' ) ) {
add_action( 'wp_dashboard_setup', function() {
wp_add_dashboard_widget(
'gobutton_dashboard_widget',
__( 'Go Button: One Time & Long Running Process Launcher', 'gobutton' ),
'gobutton_dashboard_widget_callback'
);
} );
/**
* Create the function to output the contents of the Dashboard Widget.
*/
function gobutton_dashboard_widget_callback() {
$description = __( 'Press this button to run any one time or long running actions registered by other plugins or themes.', 'gobutton' );
$button_pressed = filter_has_var( INPUT_POST, 'gobutton' );
$nonce = filter_input( INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING );
if ( $button_pressed && wp_verify_nonce( $nonce, 'go_button' ) ) {
/**
* Fires each time user clicks the "Go" button on the dashboard.
*
* Also since this is inside the widget, this is an easy spot to output status. Admin Notices are a better
* option https://digwp.com/2016/05/wordpress-admin-notices/
*/
do_action( 'gobutton_clicked' );
}
?>
<form name="post" action="<?php echo esc_url( admin_url() ); ?>" method="post" id="go-button">
<p>
<?php
/**
* Filter description to notify your potential victim of their impending doom.
*
* Multiple plugins may use this same hook so prefer adding to description rather than replacing.
*/
esc_html( apply_filters( 'gobutton_description', $description ) );
?>
</p>
<p class="submit">
<?php wp_nonce_field( 'go_button' ); ?>
<?php submit_button( __( 'Go', 'gobutton' ), 'primary', 'gobutton', false ); ?>
<br class="clear"/>
</p>
</form>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment