Last active
March 21, 2017 13:30
-
-
Save damiencarbery/27dc3ac53556eb0d3ddb8bffedef0955 to your computer and use it in GitHub Desktop.
WordPress Dashboard Widget as a Client Support Form
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 | |
// WordPress Administration Bootstrap | |
$admin_file = 'wp-admin/admin.php'; | |
$plugin_dir = dirname( __FILE__ ) . '/'; | |
// Find the wp-admin/admin.php file. | |
$relative_dirs = ''; | |
$num_rel_dirs = 0; | |
while ( !file_exists( $plugin_dir . $relative_dirs .$admin_file ) && $num_rel_dirs < 10 ) { | |
$relative_dirs .= '../'; | |
$num_rel_dirs++; | |
} | |
require_once( $plugin_dir . $relative_dirs .$admin_file ); | |
// Send the support email. | |
//error_log('$_POST:'.var_export($_POST)); | |
if ( array_key_exists( 'support-request-nonce', $_POST ) && wp_verify_nonce( $_POST[ 'support-request-nonce' ], 'support-request-nonce' ) ) { | |
if ( array_key_exists( 'support_issue', $_POST ) ) { | |
$support_issue = filter_var( $_POST[ 'support_issue' ], FILTER_SANITIZE_STRING ); | |
// Set the From email and name if supplied. | |
$headers = array(); | |
if ( array_key_exists( 'email_from', $_POST ) ) { | |
if ( array_key_exists( 'name_from', $_POST ) ) { | |
$headers[] = sprintf( "From: %s <%s>", filter_var( $_POST[ 'name_from' ], FILTER_SANITIZE_STRING ), filter_var( $_POST[ 'email_from' ], FILTER_SANITIZE_EMAIL ) ); | |
} | |
else { | |
$headers[] = sprintf( "From: %s", filter_var( $_POST[ 'email_from' ], FILTER_SANITIZE_EMAIL ) ); | |
} | |
} | |
wp_mail( 'daymobrew@yahoo.com', 'Support Request From: ' . get_bloginfo( 'name' ), $support_issue, $headers ); | |
} | |
} | |
$goback = add_query_arg( 'support-request-submitted', 'true', wp_get_referer() ); | |
wp_redirect( $goback ); | |
exit; |
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 Support info box into WordPress Dashboard | |
add_action('wp_dashboard_setup', 'dcwd_add_dashboard_widgets' ); | |
function dcwd_add_dashboard_widgets() { | |
wp_add_dashboard_widget('wp_dashboard_widget', 'Site Information', 'dcwd_theme_info'); | |
} | |
function dcwd_theme_info() { | |
?> | |
<ul> | |
<li><strong>Developed By:</strong> Damien Carbery</li> | |
<li><strong>Website:</strong> <a href='http://damiencarbery.com'>damiencarbery.com</a></li> | |
<li><strong>Contact:</strong> <a href='mailto:damien@damiencarbery.com'>damien@damiencarbery.com</a></li> | |
<li><strong>Phone:</strong> XXX-XXX-XXXX</li> | |
</ul> | |
<?php | |
} |
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 Support info box to Dashboard | |
add_action('wp_dashboard_setup', 'dcwd_add_dashboard_support_widget' ); | |
function dcwd_add_dashboard_support_widget() { | |
// Support form is only for authors and higher - exclude subscribers. | |
if ( current_user_can( 'edit_posts' ) ) { | |
wp_add_dashboard_widget( 'dcwd-support-form', 'Support Request', 'dcwd_support_form' ); | |
} | |
} | |
function dcwd_support_form() { | |
if ( array_key_exists( 'support-request-submitted', $_GET ) && true == $_GET[ 'support-request-submitted' ] ) { | |
$now = date( get_option( 'time_format' ) ); | |
echo '<div class="updated inline"><p><strong>Support Request Submitted at '.$now.'</strong></p></div>'; | |
} | |
$current_user = wp_get_current_user(); | |
// Exclude the user first and last name only if set. | |
$name_from = ''; | |
if ( strlen( $current_user->user_firstname ) && strlen( $current_user->user_lastname ) ) { | |
$name_from = $current_user->user_firstname . ' ' . $current_user->user_lastname; | |
} | |
?> | |
<form method="post" action="<?php echo plugin_dir_url( __FILE__ ); ?>dcwd-support-request.php"> | |
<p>If you are having problems with your site or need some changes made, please describe them below.</p> | |
<p>A support request will be opened and Damien will respond.</p> | |
<textarea name="support_issue" rows="10" cols="50" id="support_issue" class="large-text code"></textarea> | |
<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="Submit Support Request" /></p> | |
<input type="hidden" name="support-request-nonce" value="<?php echo wp_create_nonce('support-request-nonce'); ?>" /> | |
<input type="hidden" name="email_from" value="<?php echo $current_user->user_email; ?>" /> | |
<?php if ( strlen( $name_from ) ) { ?> | |
<input type="hidden" name="name_from" value="<?php echo $current_user->user_firstname, ' ', $current_user->user_lastname; ?>" /> | |
<?php } ?> | |
</form> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment