Skip to content

Instantly share code, notes, and snippets.

@edheltzel
Created October 22, 2015 17:17
Show Gist options
  • Save edheltzel/b0d898197f8b2ab65952 to your computer and use it in GitHub Desktop.
Save edheltzel/b0d898197f8b2ab65952 to your computer and use it in GitHub Desktop.
hacks for wordpress dashboard
<?php
/*-----------------------------------------------------------------------------------*/
/* UPDATE EMAIL ADDRESS FOR WORDPRESS
/* ----------------------------------------------------------------------------------*/
// AUTO-DETECT THE SERVER
function rdm_filter_wp_mail_from($email){
// START OF CODE LIFTED FROM WORDPRESS CORE
$sitename = strtolower( $_SERVER['SERVER_NAME'] );
if ( substr( $sitename, 0, 4 ) == 'www.' ) {
$sitename = substr( $sitename, 4 );
}
// END OF CODE LIFTED FROM WORDPRESS CORE
$myfront = "web-master@";
$myback = $sitename;
$myfrom = $myfront . $myback;
return $myfrom;
}
add_filter("wp_mail_from", "rdm_filter_wp_mail_from");
// FROM EMAIL NAME
function rdm_filter_wp_mail_from_name($from_name){
return "Web Master";
}
add_filter("wp_mail_from_name", "rdm_filter_wp_mail_from_name");
//add duplicate button to posts, pages and custom post types
function rdm_duplicate_post_as_draft(){
global $wpdb;
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'rdm_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
wp_die('No post to duplicate has been supplied!');
}
// get the original post id
$post_id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
// and all the original post data then
$post = get_post( $post_id );
/*
* if you don't want current user to be the new post author,
* then change next couple of lines to this: $new_post_author = $post->post_author;
*/
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
// if post data exists, create the post duplicate
if (isset( $post ) && $post != null) {
// new post data array
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
// insert the post by wp_insert_post() function
$new_post_id = wp_insert_post( $args );
// get all current post terms ad set them to the new post draft
$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy);
for ($i=0; $i<count($post_terms); $i++) {
wp_set_object_terms($new_post_id, $post_terms[$i]->slug, $taxonomy, true);
}
}
// duplicate all post meta
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos)!=0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
// finally, redirect to the edit post screen for the new draft
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
}
else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}
add_action( 'admin_action_rdm_duplicate_post_as_draft', 'rdm_duplicate_post_as_draft' );
// Add the duplicate link to action list for post_row_actions
function rdm_duplicate_post_link( $actions, $post ) {
if (current_user_can('edit_posts')) {
$actions['duplicate'] = '<a href="admin.php?action=rdm_duplicate_post_as_draft&amp;post=' . $post->ID . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
}
return $actions;
}
// posts filter
add_filter( 'post_row_actions', 'rdm_duplicate_post_link', 10, 2 );
// page filter
add_filter('page_row_actions', 'rdm_duplicate_post_link', 10, 2);
// custom post type filter
add_filter('{post type name}_row_actions', 'rdm_duplicate_post_link', 10, 2);
// REMOVE SEO FILTER IN DASHBOARD
add_filter( 'wpseo_use_page_analysis', '__return_false' );
/*-----------------------------------------------------------------------------------*/
/* REMOVE PERSONAL OPTIONS FROM PROFILE PAGE
/* ----------------------------------------------------------------------------------*/
// removes the `profile.php` admin color scheme options
remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
if ( ! function_exists( 'rdm_remove_personal_options' ) ) {
// removes the leftover 'Visual Editor', 'Keyboard Shortcuts' and 'Toolbar' options.
function rdm_remove_personal_options( $subject ) {
$subject = preg_replace( '#<h3>Personal Options</h3>.+?/table>#s', '', $subject, 1 );
return $subject;
}
function rdm_profile_subject_start() {
ob_start( 'rdm_remove_personal_options' );
}
function rdm_profile_subject_end() {
ob_end_flush();
}
}
add_action( 'admin_head-profile.php', 'rdm_profile_subject_start' );
add_action( 'admin_footer-profile.php', 'rdm_profile_subject_end' );
class RDM_User_Caps {
// add some filters
function RDM_User_Caps(){
add_filter( 'editable_roles', array(&$this, 'editable_roles'));
add_filter( 'map_meta_cap', array(&$this, 'map_meta_cap'),10,4);
}
// remove 'Administrator' from the list of roles if the current user is not an admin
function editable_roles( $roles ){
if( isset( $roles['administrator'] ) && !current_user_can('administrator') ){
unset( $roles['administrator']);
}
return $roles;
}
// don't allow users to edit or delete unless they are an admin
function map_meta_cap( $caps, $cap, $user_id, $args ){
switch( $cap ){
case 'edit_user':
case 'remove_user':
case 'promote_user':
if( isset($args[0]) && $args[0] == $user_id )
break;
elseif( !isset($args[0]) )
$caps[] = 'do_not_allow';
$other = new WP_User( absint($args[0]) );
if( $other->has_cap( 'administrator' ) ){
if(!current_user_can('administrator')){
$caps[] = 'do_not_allow';
}
}
break;
case 'delete_user':
case 'delete_users':
if( !isset($args[0]) )
break;
$other = new WP_User( absint($args[0]) );
if( $other->has_cap( 'administrator' ) ){
if(!current_user_can('administrator')){
$caps[] = 'do_not_allow';
}
}
break;
default:
break;
}
return $caps;
}
}
$rdm_user_caps = new RDM_User_Caps();
//REMOVE ALL DASHBOARD WIDGETS
function remove_dashboard_widgets() {
global $wp_meta_boxes;
// *For right now information widget
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
// *For recent comments listing widget
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
// *For quick press widget
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
// *For recent drafts widget
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
// *For Plugin information widget
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
// *For incoming links information widget
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
// *For WordPress blog news widget
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
// *For other WordPress news widget
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );
// REMOVE ADMIN POST PAG COLUMNS
function custom_post_columns($defaults) {
unset($defaults['comments']);
return $defaults;
}
add_filter('manage_posts_columns', 'custom_post_columns');
function custom_pages_columns($defaults) {
unset($defaults['comments']);
return $defaults;
}
add_filter('manage_pages_columns', 'custom_pages_columns');
//REMOVE TOP LEVEL ADMIN MENU ITEMS
/*-----------------------------------------------------------------------------------*/
/* REAMOVE TOP LEVEL admin menu items
/* ----------------------------------------------------------------------------------*/
function remove_menu_items() {
global $menu;
$restricted = array(__('Tools'), __('Customize') );
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){
unset($menu[key($menu)]);}
}
}
function remove_customize() {
$customize_url_arr = array();
$customize_url_arr[] = 'customize.php'; // 3.x
$customize_url = add_query_arg( 'return', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'customize.php' );
$customize_url_arr[] = $customize_url; // 4.0 & 4.1
if ( current_theme_supports( 'custom-header' ) && current_user_can( 'customize') ) {
$customize_url_arr[] = add_query_arg( 'autofocus[control]', 'header_image', $customize_url ); // 4.1
$customize_url_arr[] = 'custom-header'; // 4.0
}
if ( current_theme_supports( 'custom-background' ) && current_user_can( 'customize') ) {
$customize_url_arr[] = add_query_arg( 'autofocus[control]', 'background_image', $customize_url ); // 4.1
$customize_url_arr[] = 'custom-background'; // 4.0
}
foreach ( $customize_url_arr as $customize_url ) {
remove_submenu_page( 'themes.php', $customize_url );
}
}
add_action( 'admin_menu', 'remove_customize', 999);
/*-----------------------------------------------------------------------------------*/
/* ADMIN DASHBOARD - FOOTER
/* ----------------------------------------------------------------------------------*/
function remove_footer_admin () {
echo '<span id="footer-thankyou">Developed By:<a href="http://rainydaymedia.net" target="_blank" title="Rainy Day Media, LLC.">Rainy Day Media, LLC.</a></span>';
}
add_filter('admin_footer_text','remove_footer_admin');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment