├── .htaccess
├── README.md
├── change_db_prefix.sql
├── functions-admin.php
├── functions-general.php
├── functions-housekeeping.php
├── functions-search.php
├── gravity-forms
└── gforms-autologin.php
├── post-actions.php
├── rawsql-change-or-replace.sql
├── rawsql-cleanup-comments.sql
├── rawsql-delete.sql
├── rawsql-insert.sql
├── rawsql-randoms.sql
├── rawsql-select.sql
├── sanitize-slug-stopwords.php
├── taxonomy-radio-vs-checklist.php
└── woo-cleanup.sql
/.htaccess:
--------------------------------------------------------------------------------
1 | # HTTPS redirect
2 |
3 | RewriteEngine On
4 | RewriteCond %{HTTP:X-Forwarded-Proto} !https
5 | RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
6 |
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | WordPress Snippits
2 | ==================
3 |
4 | A collection Useful snippits I use on WordPress 3+ projects.
5 |
6 | If you are looking for my contributions to the [WordPress StackExchange](https://github.com/codearachnid/WPSE) or [WordPress Scripts Async](https://github.com/codearachnid/wp-scripts-async)
7 |
--------------------------------------------------------------------------------
/change_db_prefix.sql:
--------------------------------------------------------------------------------
1 | # these statements will update your options and user meta tables to persist
2 | # user permissions and ties to the database tables
3 | #
4 | # set the {old_prefix} to your current prefix of the tables
5 | # set the {new_prefix} to the prefix of the tables you wish to migrate
6 | # this assumes you will run the table rename queries to migrate to the new prefix
7 |
8 | UPDATE {old_prefix}options SET option_name='{new_prefix}user_roles' WHERE option_name='{old_prefix}user_roles';
9 | UPDATE {old_prefix}usermeta SET meta_key='{new_prefix}autosave_draft_ids' WHERE meta_key='{old_prefix}autosave_draft_ids';
10 | UPDATE {old_prefix}usermeta SET meta_key='{new_prefix}capabilities' WHERE meta_key='{old_prefix}capabilities';
11 | UPDATE {old_prefix}usermeta SET meta_key='{new_prefix}metaboxorder_post' WHERE meta_key='{old_prefix}metaboxorder_post';
12 | UPDATE {old_prefix}usermeta SET meta_key='{new_prefix}user_level' WHERE meta_key='{old_prefix}user_level';
13 | UPDATE {old_prefix}usermeta SET meta_key='{new_prefix}usersettings' WHERE meta_key='{old_prefix}usersettings';
14 | UPDATE {old_prefix}usermeta SET meta_key='{new_prefix}usersettingstime' WHERE meta_key='{old_prefix}usersettingstime';
15 | UPDATE {old_prefix}usermeta SET meta_key='{new_prefix}user-settings' WHERE meta_key='{old_prefix}user-settings';
16 | UPDATE {old_prefix}usermeta SET meta_key='{new_prefix}user-settings-time' WHERE meta_key='{old_prefix}user-settings-time';
17 | UPDATE {old_prefix}usermeta SET meta_key='{new_prefix}dashboard_quick_press_last_post_id' WHERE meta_key='{old_prefix}dashboard_quick_press_last_post_id';
18 |
--------------------------------------------------------------------------------
/functions-admin.php:
--------------------------------------------------------------------------------
1 | remove_menu( 'wp-logo' );
14 | $wp_admin_bar->remove_menu( 'view-site' );
15 | }
16 | add_action( 'wp_before_admin_bar_render', 'wp_modify_admin_bar' );
17 |
18 | // restrict wp-admin access to subscribers
19 | function restrict_access_admin_panel(){
20 | global $current_user;
21 | get_currentuserinfo();
22 | if ($current_user->user_level < 4) {
23 | wp_redirect( get_bloginfo('url') );
24 | exit;
25 | }
26 | }
27 | add_action('admin_init', 'restrict_access_admin_panel', 1);
28 |
29 | // disable browser nag/upgrade warnings
30 | function disable_browser_upgrade_warning() {
31 | remove_meta_box( 'dashboard_browser_nag', 'dashboard', 'normal' );
32 | }
33 | add_action( 'wp_dashboard_setup', 'disable_browser_upgrade_warning' );
34 |
35 | // increase default (30) limit on custom fields
36 | add_filter( 'postmeta_form_limit' , 'customfield_limit_increase' );
37 | function customfield_limit_increase( $limit ) {
38 | $limit = 60;
39 | return $limit;
40 | }
41 |
42 | // disable autosaving in editor screen
43 | function disableAutoSave(){
44 | wp_deregister_script('autosave');
45 | }
46 | add_action( 'wp_print_scripts', 'disableAutoSave' );
47 |
48 | // remove curly quotes
49 | remove_filter('the_content', 'wptexturize');
50 |
51 | // replace wp-login.php logo
52 | function wp_badge_login_logo() {
53 | echo '';
54 | }
55 | add_action('login_head', 'wp_badge_login_logo');
56 |
57 | // remove custom fields metabox
58 | function remove_default_page_screen_metaboxes() {
59 | remove_meta_box( 'postcustom','post','normal' );
60 | }
61 | add_action('admin_menu','remove_default_page_screen_metaboxes');
62 |
63 | // remove revision list from editor
64 | function remove_revisions_metabox() {
65 | remove_meta_box( 'revisionsdiv','post','normal' );
66 | }
67 | add_action('admin_menu','remove_revisions_metabox');
68 |
69 | // add subscript & superscript to tinymce
70 | function enable_more_buttons($buttons) {
71 | $buttons[] = 'sub';
72 | $buttons[] = 'sup';
73 | return $buttons;
74 | }
75 | add_filter("mce_buttons_3", "enable_more_buttons");
76 |
77 | // remove default profile fields from admin
78 | function hide_profile_fields( $contactmethods ) {
79 | unset($contactmethods['aim']);
80 | unset($contactmethods['jabber']);
81 | unset($contactmethods['yim']);
82 | return $contactmethods;
83 | }
84 | add_filter('user_contactmethods','hide_profile_fields',10,1);
85 |
86 | // restrict author (user) to see only authored posts
87 | function posts_for_current_author($query) {
88 | global $pagenow;
89 | if( 'edit.php' != $pagenow || !$query->is_admin )
90 | return $query;
91 | if( !current_user_can( 'manage_options' ) ) {
92 | global $user_ID;
93 | $query->set('author', $user_ID );
94 | }
95 | return $query;
96 | }
97 | add_filter('pre_get_posts', 'posts_for_current_author');
98 |
99 | // restrict user to only see media they have uploaded
100 | function current_user_files_only( $wp_query ) {
101 | if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) {
102 | if ( !current_user_can( 'level_5' ) ) {
103 | global $current_user;
104 | $wp_query->set( 'author', $current_user->id );
105 | }
106 | }
107 | }
108 | add_filter('parse_query', 'current_user_files_only' );
109 |
110 | // disable ability to use captions
111 | add_filter( 'disable_captions', create_function( '$a','return true;' ) );
112 |
113 | // remove the theme editor submenu
114 | function remove_editor_menu() {
115 | remove_action('admin_menu', '_add_themes_utility_last', 101);
116 | }
117 | add_action('_admin_menu', 'remove_editor_menu', 1);
118 |
119 | // disable html editor for roles below admin
120 | function disable_html_editor() {
121 | global $current_user;
122 | get_currentuserinfo();
123 | if ($current_user->user_level != 10) {
124 | echo '';
125 | }
126 | }
127 | add_filter( 'wp_default_editor', create_function('', 'return "tinymce";') );
128 | add_action( 'admin_head', 'disable_html_editor' );
129 |
130 | // remove ability for anyone not "admin" to swap themes
131 | function slt_lock_theme() {
132 | global $submenu, $userdata;
133 | get_currentuserinfo();
134 | if ( $userdata->ID != 1 ) {
135 | unset( $submenu['themes.php'][5] );
136 | unset( $submenu['themes.php'][15] );
137 | }
138 | }
139 | add_action( 'admin_init', 'slt_lock_theme' );
140 |
141 | // add a custom message to the login screen
142 | function custom_login_message() {
143 | $message = '
Welcome, please read our terms of service before you register.
';
144 | return $message;
145 | }
146 | add_filter('login_message', 'custom_login_message');
147 |
148 | // automatically categorize and tag posts when saved
149 | function update_post_terms( $post_id ) {
150 | if ( $parent = wp_is_post_revision( $post_id ) )
151 | $post_id = $parent;
152 | $post = get_post( $post_id );
153 | if ( $post->post_type != 'post' )
154 | return;
155 | // add a tag
156 | wp_set_post_terms( $post_id, 'new tag', 'post_tag', true );
157 | // add a category
158 | $categories = wp_get_post_categories( $post_id );
159 | $newcat = get_term_by( 'name', 'Some Category', 'category' );
160 | array_push( $categories, $newcat->term_id );
161 | wp_set_post_categories( $post_id, $categories );
162 | }
163 | add_action( 'wp_insert_post', 'update_post_terms' );
164 |
165 | // restrict upload file types
166 | function restrict_mime($mimes) {
167 | $mimes = array(
168 | 'jpg|jpeg|jpe' => 'image/jpeg',
169 | 'gif' => 'image/gif',
170 | );
171 | return $mimes;
172 | }
173 | add_filter('upload_mimes','restrict_mime');
174 |
175 | // add total members to “Right Now” dashboard widget
176 | function dashboard_wp_user_count() {
177 | global $wpdb;
178 | $user_c = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
179 | ?>
180 |
188 | tags around images within the_content
7 | function filter_tags_on_images($content){
8 | return preg_replace( '/\s*()?\s*(
)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content );
9 | }
10 | add_filter( 'the_content', 'filter_ptags_on_images' );
11 |
12 | // remove WP generator tags in head
13 | remove_action( 'wp_head', 'wp_generator' );
14 |
15 | // replace WP badging
16 | function replace_wp_generator() { return ''; }
17 | add_filter('the_generator', 'replace_wp_generator');
18 |
19 | // get_the_time() - makes up for a lack of return the_time functionality
20 | function return_the_time( $d = '' ) {
21 | return apply_filters( 'the_time', get_the_time( $d ), $d );
22 | }
23 |
24 | // display time like twitter
25 | function the_time_like_twitter( $type = 'post' ) {
26 | $d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';
27 | return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');
28 | }
29 |
30 | // check if post's assigned categories are descendants of target categories
31 | function post_is_in_descendant_category( $cats, $_post = null ) {
32 | foreach ( (array) $cats as $cat ) {
33 | // get_term_children() accepts integer ID only
34 | $descendants = get_term_children( (int) $cat, 'category' );
35 | if ( $descendants && in_category( $descendants, $_post ) )
36 | return true;
37 | }
38 | return false;
39 | }
40 |
41 | // set favicon with Gravatar
42 | function gravatar_favicon() {
43 | $GetTheHash = md5(strtolower(trim(get_bloginfo('admin_email'))));
44 | return 'http://www.gravatar.com/avatar/' . $GetTheHash . '?s=16';
45 | }
46 | function favicon() {
47 | echo '';
48 | }
49 | add_action('wp_head', 'favicon');
50 |
51 | // notify user when role is changed
52 | function user_role_update( $user_id, $new_role ) {
53 | $site_url = get_bloginfo( 'wpurl' );
54 | $user_info = get_userdata( $user_id );
55 | $to = $user_info->user_email;
56 | $subject = "Role changed: {$site_url}";
57 | $message = "Hello {$user_info->display_name} your role has changed on {$site_url} congratulations you are now an {$new_role}.";
58 | wp_mail( $to, $subject, $message );
59 | }
60 | add_action( 'set_user_role', 'user_role_update', 10, 2);
61 |
62 | // get related posts by author
63 | function related_author_posts($atts) {
64 | global $authordata, $post;
65 | extract( shortcode_atts( array(
66 | 'return' => false, // if set to true will hand over raw records
67 | 'author' => $authordata->ID,
68 | 'post__not_in' => array( $post->ID ),
69 | 'posts_per_page' => 5
70 | ), $atts ) );
71 | $related_posts = get_posts( array( 'author' => $author, 'post__not_in' => $post__not_in, 'posts_per_page' => $posts_per_page ) );
72 | if( $return ) {
73 | return $related_posts;
74 | } else {
75 | $output = '';
80 | return $output;
81 | }
82 | }
83 |
84 | // show query performance in footer comment
85 | function wp_query_performance( $visible = false ) {
86 | $stat = sprintf( '%d queries in %.3f seconds, using %.2fMB memory',
87 | get_num_queries(),
88 | timer_stop( 0, 3 ),
89 | memory_get_peak_usage() / 1024 / 1024
90 | );
91 | echo $visible ? $stat : "" ;
92 | }
93 | add_action( 'wp_footer', 'wp_query_performance', 20 );
94 |
95 | // exclude categories from RSS feed
96 | function exclude_categories_from_rss($query) {
97 | if ($query->is_feed) {
98 | $query->set('cat','-20,-21,-22'); // hardcode category ids
99 | }
100 | return $query;
101 | }
102 | add_filter('pre_get_posts','exclude_categories_from_rss');
103 |
104 | // insert wmode parameter into oEmbeds
105 | function insert_wmode_opaque( $html, $url, $args ) {
106 | if ( strpos( $html, '|', '', $html, 1 );
108 | if ( strpos( $html, '