├── .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 | 181 | 182 | 183 | 184 | 185 | 186 | 187 |
Members
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, ' $link ) 128 | if ( 0 === strpos( $link, $home ) ) 129 | unset($links[$l]); 130 | } 131 | add_action( 'pre_ping', 'no_self_ping' ); 132 | 133 | // remove l10n.js javascript in WP 3+ 134 | function remove_l1on() { 135 | if ( !is_admin() ) { 136 | wp_deregister_script('l10n'); 137 | } 138 | } 139 | add_action( 'init', 'remove_l1on' ); 140 | 141 | // register jquery from Google CDN 142 | function goog_cdn_jquery_register() { 143 | if ( !is_admin() ) { 144 | wp_deregister_script( 'jquery' ); 145 | wp_register_script( 'jquery', ( 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js' ), false, null, true ); 146 | wp_enqueue_script( 'jquery' ); 147 | } 148 | } 149 | add_action( 'init', 'goog_cdn_jquery_register' ); 150 | 151 | // add login|logout link into menu 152 | function add_login_logout_link($items, $args) { 153 | $loginoutlink = wp_loginout('index.php', false); 154 | $items .= '
  • '. $loginoutlink .'
  • '; 155 | return $items; 156 | } 157 | add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2); 158 | 159 | // strip shortcodes from the_content on homepage 160 | function remove_shortcode_from_index($content) { 161 | if ( is_home() ) { 162 | $content = strip_shortcodes( $content ); 163 | } 164 | return $content; 165 | } 166 | add_filter('the_content', 'remove_shortcode_from_index'); 167 | 168 | // nofollow external links only, the_content and the_excerpt 169 | function my_nofollow($content) { 170 | return preg_replace_callback('/]+/', 'my_nofollow_callback', $content); 171 | } 172 | function my_nofollow_callback($matches) { 173 | $link = $matches[0]; 174 | $site_link = get_bloginfo('url'); 175 | if (strpos($link, 'rel') === false) { 176 | $link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link); 177 | } elseif (preg_match("%href=\S(?!$site_link)%i", $link)) { 178 | $link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link); 179 | } 180 | return $link; 181 | } 182 | add_filter('the_content', 'my_nofollow'); 183 | add_filter('the_excerpt', 'my_nofollow'); 184 | 185 | // add file type category within media library 186 | function modify_post_mime_types($post_mime_types) { 187 | $post_mime_types['application/x-shockwave-flash'] = array( 188 | __( 'mimeframe' ), 189 | __('Manage Flash'), 190 | _n_noop( 'Flash (%s)', 'Flash (%s)') 191 | ); 192 | return $post_mime_types; 193 | } 194 | add_filter('post_mime_types', 'modify_post_mime_types'); 195 | 196 | // convert URI, www, ftp, and emails into clickable links 197 | add_filter('the_content', 'make_clickable'); 198 | add_filter('the_excerpt', 'make_clickable'); 199 | 200 | // show user last login date 201 | function your_last_login($login) { 202 | global $user_ID; 203 | $user = get_userdatabylogin($login); 204 | update_usermeta($user->ID, 'last_login', current_time('mysql')); 205 | } 206 | function get_last_login($user_id) { 207 | $last_login = get_user_meta($user_id, 'last_login', true); 208 | $date_format = get_option('date_format') . ' ' . get_option('time_format'); 209 | $the_last_login = mysql2date($date_format, $last_login, false); 210 | echo $the_last_login; 211 | } 212 | add_action('wp_login','your_last_login'); 213 | // to display: 214 | global $userdata; 215 | get_currentuserinfo(); 216 | echo 'You last logged in:'; 217 | get_last_login($userdata->ID); 218 | 219 | 220 | // add "Read More" to the end of an excerpt 221 | function excerpt_readmore($more) { 222 | return '... ' . 'Read More' . ''; 223 | } 224 | add_filter('excerpt_more', 'excerpt_readmore'); 225 | 226 | // redirect to post if single result in category or tag 227 | function redirect_to_post(){ 228 | global $wp_query; 229 | if( is_archive() && $wp_query->post_count == 1 ){ 230 | the_post(); 231 | $post_url = get_permalink(); 232 | wp_redirect( $post_url ); 233 | } 234 | } add_action('template_redirect', 'redirect_to_post'); 235 | 236 | // sensor words in comments 237 | function wp_filter_comment($comment) { 238 | $replace = array( 239 | // 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS' 240 | 'foobar' => '*****', 241 | 'hate' => 'love', 242 | 'zoom' => 'zoom' 243 | ); 244 | $comment = str_replace(array_keys($replace), $replace, $comment); 245 | return $comment; 246 | } 247 | add_filter( 'pre_comment_content', 'wp_filter_comment' ); 248 | 249 | // Google doc's shortcode for non-browser friendly formats 250 | function wps_viewer($atts, $content = null) { 251 | extract(shortcode_atts(array( 252 | "href" => 'http://', 253 | "class" => '' 254 | ), $atts)); 255 | return ''.$content.''; 256 | } 257 | add_shortcode("doc", "wps_viewer"); 258 | // USAGE: 259 | // [doc class="psd" href="http://www.wpsnipp.com/file.psd"]my PSD file name[/doc] 260 | // [doc class="ai" href="http://www.wpsnipp.com/file.ai"]my AI file name[/doc] 261 | // [doc class="svg" href="http://www.wpsnipp.com/file.svg"]my SVG file name[/doc] 262 | 263 | // enable Gravatar hovercards 264 | function gravatar_hovercards() { 265 | wp_enqueue_script( 'gprofiles', 'http://s.gravatar.com/js/gprofiles.js', array( 'jquery' ), 'e', true ); 266 | } 267 | add_action('wp_enqueue_scripts','gravatar_hovercards'); 268 | 269 | // if available enable gzip through php 270 | if(extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler")) 271 | add_action('wp', create_function('', '@ob_end_clean();@ini_set("zlib.output_compression", 1);')); 272 | 273 | // extend cookie expiration for logged in users 274 | function logged_in( $expirein ) { 275 | return 604800; // 1 week in seconds 276 | } 277 | add_filter( 'auth_cookie_expiration', 'logged_in' ); 278 | 279 | // determine if post is older than (int) 280 | function is_old_post($days = 5) { 281 | $days = (int) $days; 282 | $offset = $days*60*60*24; 283 | if ( get_post_time() < date('U') - $offset ) 284 | return true; 285 | return false; 286 | } -------------------------------------------------------------------------------- /functions-housekeeping.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codearachnid/wordpress-snippits/31a91b48f4bd6804e8dac7ad668c4d869116d1ea/functions-housekeeping.php -------------------------------------------------------------------------------- /functions-search.php: -------------------------------------------------------------------------------- 1 | post_count == 1) { 17 | wp_redirect( get_permalink( $wp_query->posts['0']->ID ) ); 18 | } 19 | } 20 | } 21 | add_action('template_redirect', 'single_result'); 22 | 23 | // filter search results by post type 24 | function search_posts_filter( $query ){ 25 | if ($query->is_search){ 26 | $query->set('post_type',array('post','custom_post_type1', 'custom_post_type2')); 27 | } 28 | return $query; 29 | } 30 | add_filter('pre_get_posts','search_posts_filter'); 31 | 32 | // limit WP search to title field 33 | function wp_search_by_title_only( $search, &$wp_query ) 34 | { 35 | global $wpdb; 36 | if ( empty( $search ) ) 37 | return $search; // skip processing - no search term in query 38 | $q = $wp_query->query_vars; 39 | $n = ! empty( $q['exact'] ) ? '' : '%'; 40 | $search = 41 | $searchand = ''; 42 | foreach ( (array) $q['search_terms'] as $term ) { 43 | $term = esc_sql( like_escape( $term ) ); 44 | $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')"; 45 | $searchand = ' AND '; 46 | } 47 | if ( ! empty( $search ) ) { 48 | $search = " AND ({$search}) "; 49 | if ( ! is_user_logged_in() ) 50 | $search .= " AND ($wpdb->posts.post_password = '') "; 51 | } 52 | return $search; 53 | } 54 | add_filter( 'posts_search', 'wp_search_by_title_only', 500, 2 ); 55 | 56 | // customize search WHERE BY date/title filter 57 | function filter_where($where = '') { 58 | global $sdate, $edate, $ename; 59 | if ($ename != null) { 60 | $where .= " AND post_title LIKE '%" . $ename . "%'"; 61 | } 62 | if ($sdate != null) { 63 | $where .= " AND post_date >= '" . date('Y-m-d', strtotime($sdate)) . "'"; 64 | } 65 | if ($edate != null) { 66 | $where .= " AND post_date <= '" . date('Y-m-d', strtotime($edate ." +1 day")) . "'"; 67 | } 68 | return $where; 69 | } 70 | function filter_by_date($where = '') { 71 | global $fcstart, $fcend; 72 | if ($fcstart != null) { 73 | $where .= " AND post_date >= '" . date('Y-m-d', $fcstart) . "'"; 74 | } 75 | if ($fcend != null) { 76 | $where .= " AND post_date <= '" . date('Y-m-d', $fcend ." +1 day") . "'"; 77 | } 78 | return $where; 79 | } 80 | function filter_by_today($where = '') { 81 | $where .= " AND post_date >= '" . date('Y-m-d') . "'"; 82 | return $where; 83 | } 84 | 85 | // SETUP filter vars 86 | $fcstart = isset($_GET['start']) ? $_GET['start'] : date('Y-m-d'); 87 | $fcend = isset($_GET['end']) ? $_GET['end'] : date('Y-m-d'); 88 | $sdate = isset($_GET['sdate']) && $_GET['sdate'] != '' ? $_GET['sdate'] : date('Y-m-d'); 89 | $edate = isset($_GET['edate']) ? $_GET['edate'] : null; 90 | $eorderby = isset($_GET['orderby']) ? $_GET['orderby'] : 'date'; 91 | $eorder = isset($_GET['eorder']) ? $_GET['eorder'] : 'ASC'; 92 | $elimit = isset($_GET['elimit']) ? $_GET['elimit'] : get_option('posts_per_page'); 93 | $ename = isset($_GET['ename']) && $_GET['ename'] != 'Event Search' ? $_GET['ename'] : null; 94 | $etype = isset($_GET['etype']) ? $_GET['etype'] : get_category_by_slug('classes')->term_id; 95 | $pagetype = isset($_GET['page_type']) ? $_GET['page_type'] : ''; 96 | $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 97 | 98 | add_filter('posts_where', 'filter_where'); 99 | query_posts(array( 100 | 'cat' => $etype, 101 | 'paged'=>$paged, 102 | 'post_type' => 'tribe_events', 103 | 'orderby' => $eorderby, 104 | 'order' => $eorder, 105 | 'posts_per_page' => $elimit, 106 | 'post_status' => array('published') 107 | )); 108 | remove_filter('posts_where', 'filter_where'); -------------------------------------------------------------------------------- /gravity-forms/gforms-autologin.php: -------------------------------------------------------------------------------- 1 | $user_login, 'user_password' => $password, 'remember' => false)); 21 | 22 | if(!is_wp_error($result)) 23 | wp_redirect($redirect_url); 24 | 25 | } -------------------------------------------------------------------------------- /post-actions.php: -------------------------------------------------------------------------------- 1 | $post_id, 6 | 'post_parent' => $post_parent_id, 7 | ]); 8 | -------------------------------------------------------------------------------- /rawsql-change-or-replace.sql: -------------------------------------------------------------------------------- 1 | /* Change the destination URL of a WordPress site. */ 2 | UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl.com', 'http://www.newurl.com') WHERE option_name = 'home' OR option_name = 'siteurl'; 3 | UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl.com','http://www.newurl.com'); 4 | UPDATE wp_posts SET post_content = replace(post_content, ' http://www.oldurl.com ', ' http://www.newurl.com '); 5 | 6 | /* Search and replace post/page content by string. */ 7 | UPDATE wp_posts SET post_content = REPLACE (post_content, 'original', 'replace_with'); 8 | 9 | /* Change username */ 10 | UPDATE wp_users SET user_login = 'new_username' WHERE user_login = 'username'; 11 | 12 | /* Reset user password */ 13 | UPDATE wp_users SET user_pass = MD5('password') WHERE user_login = 'username' LIMIT 1; 14 | 15 | /* Change associated author on post/page */ 16 | UPDATE wp_posts SET post_author=(SELECT ID FROM wp_users WHERE user_login = 'username' LIMIT 1) WHERE post_author=(SELECT ID FROM wp_users WHERE user_login = 'username' LIMIT 1); 17 | 18 | /* Change post type (post to page, etc) */ 19 | UPDATE wp_posts SET post_type = 'new_type' WHERE post_type = 'old_type'; 20 | 21 | /* Enable/Disable pingbacks & trackbacks before a certain date */ 22 | UPDATE wp_posts SET ping_status = 'closed|open' WHERE post_date < '2012-01-01' AND post_status = 'publish'; 23 | 24 | /* Enable/Disable comments before a certain date */ 25 | UPDATE wp_posts SET comment_status = 'closed' WHERE post_date < '2010-01-01' AND post_status = 'publish'; 26 | 27 | /* cleanup bad characters */ 28 | UPDATE wp_posts SET post_content = REPLACE(post_content, 'Â', ''); 29 | UPDATE wp_posts SET post_content = REPLACE(post_content, '“', '“'); 30 | UPDATE wp_posts SET post_content = REPLACE(post_content, '”', '”'); 31 | UPDATE wp_posts SET post_content = REPLACE(post_content, '’', '’'); 32 | UPDATE wp_posts SET post_content = REPLACE(post_content, '‘', '‘'); 33 | UPDATE wp_posts SET post_content = REPLACE(post_content, '—', '–'); 34 | UPDATE wp_posts SET post_content = REPLACE(post_content, '–', '—'); 35 | UPDATE wp_posts SET post_content = REPLACE(post_content, '•', '-'); 36 | UPDATE wp_posts SET post_content = REPLACE(post_content, '…', '…'); 37 | UPDATE wp_comments SET comment_content = REPLACE(comment_content, 'Â', ''); 38 | UPDATE wp_comments SET comment_content = REPLACE(comment_content, '“', '“'); 39 | UPDATE wp_comments SET comment_content = REPLACE(comment_content, '”', '”'); 40 | UPDATE wp_comments SET comment_content = REPLACE(comment_content, '’', '’'); 41 | UPDATE wp_comments SET comment_content = REPLACE(comment_content, '‘', '‘'); 42 | UPDATE wp_comments SET comment_content = REPLACE(comment_content, '—', '–'); 43 | UPDATE wp_comments SET comment_content = REPLACE(comment_content, '–', '—'); 44 | UPDATE wp_comments SET comment_content = REPLACE(comment_content, '•', '-'); 45 | UPDATE wp_comments SET comment_content = REPLACE(comment_content, '…', '…'); 46 | -------------------------------------------------------------------------------- /rawsql-cleanup-comments.sql: -------------------------------------------------------------------------------- 1 | # remove all spam comments 2 | DELETE from wp_comments WHERE comment_approved = '0'; 3 | DELETE from wp_comments WHERE comment_approved = 'spam'; 4 | DELETE from wp_comments WHERE comment_approved = 'trash'; 5 | 6 | # remove entries which have no related entry in the main wp_comments table 7 | DELETE FROM wp_commentmeta WHERE comment_id NOT IN (SELECT comment_id FROM wp_comments); 8 | 9 | # if Akismet is installed & you don't care about dashboard stats - flush out akismet statuses 10 | DELETE FROM wp_commentmeta WHERE meta_key LIKE '%akismet%'; 11 | -------------------------------------------------------------------------------- /rawsql-delete.sql: -------------------------------------------------------------------------------- 1 | /* Delete all posts over X days (aged) - REPLACE X with (int) days */ 2 | DELETE FROM wp_posts WHERE post_type = 'post' AND DATEDIFF(NOW(), post_date) > X 3 | 4 | /* Delete post meta */ 5 | DELETE FROM wp_postmeta WHERE meta_key = 'YourMetaKey'; 6 | 7 | /* Delete all revisions */ 8 | DELETE a, b, c FROM wp_posts a LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id) LEFT JOIN wp_postmeta c ON (a.ID = c.post_id) WHERE a.post_type = 'revision'; 9 | 10 | /* Delete all unapproved & spam comments */ 11 | DELETE FROM wp_comments WHERE comment_approved = 0 12 | DELETE FROM wp_comments WHERE comment_approved = 'spam'; -------------------------------------------------------------------------------- /rawsql-insert.sql: -------------------------------------------------------------------------------- 1 | /* Add a custom field to all items of a particular type */ 2 | INSERT INTO wp_postmeta (post_id, meta_key, meta_value) SELECT ID AS post_id, 'meta_key' AS meta_key, 'meta_val' AS meta_value FROM wp_posts WHERE ID NOT IN (SELECT post_id FROM wp_postmeta WHERE meta_key = 'meta_key') AND post_type = 'page'; -------------------------------------------------------------------------------- /rawsql-randoms.sql: -------------------------------------------------------------------------------- 1 | #Generate random strings against a column 2 | 3 | UPDATE table_name SET column=concat( 4 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1), 5 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1), 6 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1), 7 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1), 8 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1), 9 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1), 10 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1), 11 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1) 12 | ) 13 | 14 | #Gerate random string for column of last inserted record 15 | SELECT @lid:=LAST_INSERT_ID(); 16 | UPDATE vehicles SET numberplate=concat( 17 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@lid)*4294967296))*36+1, 1), 18 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1), 19 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1), 20 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1), 21 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1), 22 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1), 23 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1), 24 | substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand(@seed)*36+1, 1) 25 | ) 26 | WHERE id=@lid; 27 | -------------------------------------------------------------------------------- /rawsql-select.sql: -------------------------------------------------------------------------------- 1 | /* Get all posts over X days (aged) - REPLACE X with (int) days */ 2 | SELECT * FROM wp_posts WHERE post_type = 'post' AND DATEDIFF(NOW(), post_date) > X 3 | 4 | /* Identify unused tags */ 5 | SELECT * From wp_terms term INNER JOIN wp_term_taxonomy tax ON term.term_id = tax.term_id WHERE tax.taxonomy = 'post_tag' AND tax.count=0; 6 | 7 | /* Export all comment author emails with no duplicates */ 8 | SELECT DISTINCT comment_author_email FROM wp_comments; -------------------------------------------------------------------------------- /sanitize-slug-stopwords.php: -------------------------------------------------------------------------------- 1 | $word) { 22 | if ( in_array($word, $stopwords) ) 23 | unset($slug[$k]); 24 | } 25 | return implode('-', $slug); 26 | } 27 | add_filter( 'sanitize_title', 'scrub_stop_words'); -------------------------------------------------------------------------------- /taxonomy-radio-vs-checklist.php: -------------------------------------------------------------------------------- 1 |