Last active
June 11, 2019 05:55
-
-
Save joetm/5c4b638487fa5db8443f to your computer and use it in GitHub Desktop.
Solutions for frequent WordPress problems
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 | |
/* | |
* Solutions for frequent wordpress problems | |
*/ | |
// remove the emoji code | |
function disable_wp_emojicons() { | |
// all actions related to emojis | |
remove_action( 'admin_print_styles', 'print_emoji_styles' ); | |
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); | |
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); | |
remove_action( 'wp_print_styles', 'print_emoji_styles' ); | |
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); | |
remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); | |
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); | |
// filter to remove TinyMCE emojis | |
add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' ); | |
} | |
add_action( 'init', 'disable_wp_emojicons' ); | |
// remove the <link rel='https://api.w.org/' href='http://mydomain.com/wp-json/' /> in the header | |
remove_action('wp_head', 'rest_output_link_wp_head', 10); | |
// remove_action('wp_head', 'wp_oembed_add_discovery_links', 10); | |
// remove the wp-embed.min.js in the footer | |
function disable_embeds_init() { | |
// Remove the REST API endpoint. | |
remove_action('rest_api_init', 'wp_oembed_register_route'); | |
// Turn off oEmbed auto discovery. | |
// Don't filter oEmbed results. | |
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10); | |
// Remove oEmbed discovery links. | |
remove_action('wp_head', 'wp_oembed_add_discovery_links'); | |
// Remove oEmbed-specific JavaScript from the front-end and back-end. | |
remove_action('wp_head', 'wp_oembed_add_host_js'); | |
} | |
add_action('init', 'disable_embeds_init', 9999); | |
// disable REST API | |
add_filter('rest_enabled', '_return_false'); | |
add_filter('rest_jsonp_enabled', '_return_false'); | |
//nuke the stupid admin bar on the frontend | |
function off_admin_bar(){return false;} | |
add_filter('show_admin_bar','off_admin_bar'); | |
//prevent wp-pagenavi to inject the CSS | |
add_action('wp_print_styles', 'my_deregister_styles', 100); | |
function my_deregister_styles() { | |
wp_deregister_style('wp-pagenavi'); | |
} | |
//prevent akismet from injecting a javascript on EVERY page | |
add_action('wp_print_scripts', 'my_deregister_scripts', 100); | |
function my_deregister_scripts() { | |
wp_deregister_script('akismet-form'); | |
if (is_page('submit') || is_page('screenshot-upload')) { | |
wp_register_script( 'akismet-form', '/akismet-form.js'); | |
//wp_enqueue_script( 'my-akismet-script', '/akismet-form.js', array('jquery','akismet-form')); | |
} | |
} | |
/* | |
this is not working and I do not know why... | |
//change the post title for a single post | |
function overwrite_post_title($title, $sep) { | |
global $post; | |
//if ( is_single($post->ID) && !is_attachment() ) | |
$title = "Title here"; | |
return $title; | |
} | |
add_filter( 'wp_title', 'overwrite_post_title', 10, 2); | |
*/ | |
//allow filtering for empty content in post queue | |
add_filter('wp_sucks_hard','filter_empty_content'); | |
function content_empty_filter_where($where = ''){ | |
$where .= "AND post_status = 'pending' | |
AND post_type = 'post' | |
AND trim(coalesce(post_content, '')) <>''"; | |
} | |
function filter_empty_content() | |
{ | |
add_filter('posts_where', 'content_empty_filter_where'); | |
$wp_query = new WP_Query( | |
array( | |
'post_status' => "pending", | |
'post_type' => 'post', | |
'posts_per_page' => 10, | |
'paged' => intval($qv), | |
) | |
); | |
remove_filter('posts_where', 'content_empty_filter_where'); | |
return $wp_query; | |
} | |
//extract street view camera information from URL | |
function get_camera_angle($url, $embed = false){ | |
//camera defaults | |
$camera = array( | |
1 => '12', | |
2 => '221.3', | |
3 => '', | |
4 => '0', | |
5 => '5.0', | |
); | |
//$camera[1]: hemisphere: map arrangement, 11=upper half Street View and lower half map, 12=mostly Street View with corner map | |
//$camera[2]: heading | |
//$camera[3]: empty | |
//$camera[4]: zoom | |
//$camera[5]: pitch | |
if(!$url) return $camera; | |
//camera | |
$camera = array(); | |
$found = preg_match('~&cbp=(-?\d*),(-?\d+\.?\d*),(-?\d*\.?\d*),(-?\d+),(-?\d+\.?\d*)~', $url, $camera); | |
if($found) | |
{ | |
//validation | |
//for($i = 1; $i < 5; $i++) | |
//$camera[$i] = floatval($camera[$i]); | |
//zoom zero looks ugly! | |
if($camera[4]=='0') $camera[4] = 1; | |
$camera[4] = floatval($camera[4]) * 1.8; | |
//hemisphere check | |
//if($camera[1] == '13') | |
//reverse camera tilt for embed | |
//if($embed && $camera[1] < 0) | |
// $camera[5] = -1 * $camera[5]; | |
} | |
else | |
{ | |
//try to get the camera info from the new link format | |
$matches = array(); | |
/* | |
https://www.google.com/maps/@6.253567,-75.570244,3a, | |
41.8y,309.35h,88.27t | |
/data=!3m4!1e1!3m2!1sRgEjExgvRm63dsi2JvzD2g!2e0 | |
*/ | |
if( preg_match('~,(\-?\d+\.?\d*)y,(\-?\d+\.?\d*)h,(\-?\d+\.?\d*)t~', $url, $matches) ) | |
{ | |
$camera[2] = $matches[2]; | |
//zoom control | |
if(intval($matches[1]) > 20) | |
$camera[4] = 1; | |
else | |
$camera[4] = 3; | |
$camera[5] = round(90 - $matches[3], 2); | |
} | |
} | |
return $camera; | |
} //get_camera_angle | |
//extract coordinates from a street view url | |
function get_coords($url){ | |
$matches = array(); | |
//***CASE 1: (Street View) | |
//http://maps.google.com/?ie=UTF8&ll=42.353242,-99.284355&spn=0,73.300781&t=h&z=4&layer=c&cbll=39.353842,-100.189355&panoid=l4MtZ86H1-nVKJbrATrONw&cbp=12,342.31,,0,5 | |
$found = preg_match("~&cbll=(\-?\d+\.?\d*),(\-?\d+\.?\d*)~si", $_POST['submission']['url'], $matches); | |
if($found) | |
{ | |
//echo "CASE 1: StreetView"; | |
return array( | |
array(1 => $matches[1], 2 => $matches[2]), | |
'classic' | |
); | |
} | |
else | |
{ | |
//NEW STREET VIEW LINK FORMAT! | |
//https://www.google.com/maps/@30.604732,-5.684273,3a,15y,177.38h,81.82t/data=!3m4!1e1!3m2!1sgs2wASH69rQWaQ8EsYonUA!2e0!6m1!1e1 | |
$found = preg_match("~@(\-?\d+\.?\d*),(\-?\d+\.?\d*)~si", $_POST['submission']['url'], $matches); | |
if($found) | |
{ | |
return array( | |
array(1 => $matches[1], 2 => $matches[2]), | |
'new' | |
); | |
} | |
else | |
{ | |
//***CASE 2: | |
//21?51'23.46"S, 44? 1'44.80"W | |
//or | |
//41?12'27.84"N, 16?18'40.15"E | |
$found = preg_match("~(\d+\s*\D+\d*\s*\'?\s*\d*\s*\.?\s*\d*\s*\"?\s*[N|S])\s*,\s*(\d+\s*\D+\s*\d*\s*\'?\s*\d*\s*\.?\s*\d*\s*\"?\s*[E|W])~i", $_POST['submission']['url'], $matches); | |
// (\d+)\D+(\d+)\D+(\d+)\.(\d+)\W(\w+)\W+(\d+)\D+(\d+)\D+(\d+)\.(\d+)\W(\w) | |
if($found) | |
{ | |
//echo "CASE 2: 20?52'23.46\"S, 40? 2'44.80\"W<br />"; | |
//get the coords to filter out the crap | |
for($i = 1; $i<=2; $i++) | |
{ | |
//reset | |
$temp = array(); | |
$found = preg_match("~(\d+)\s*\D+\s*(\d*)\s*\'?\s*(\d*)\s*\.?\s*(\d*)\s*\"?\s*([N|S|W|E])~iU", $matches[$i], $temp); | |
$matches[$i] = number_format( $temp[1]+ //degrees | |
+($temp[2]/60)+//min | |
(($temp[3]+($temp[4]/100))/3600) //sec | |
, 5); //five decimals | |
if($temp[5]=='S' || $temp[5]=='W') | |
$matches[$i] = $matches[$i] * (-1); | |
} | |
return array( | |
array(1 => $matches[1], 2 => $matches[2]), | |
'degree' | |
); | |
} | |
else | |
{ | |
//***CASE 3: (Google Maps) | |
//http://maps.google.com/?ie=UTF8&ll=30.0225,-92.627068&spn=42.445866,73.300781&t=h&z=17 | |
//okay | |
$found = preg_match("~&ll=(\-?\d+\.?\d*),(\-?\d+\.?\d*)~si", $_POST['submission']['url'], $matches); | |
if($found) //found streetview coordinates | |
{ | |
//echo "CASE 3: Google Maps"; | |
return array( | |
array(1 => $matches[1], 2 => $matches[2]), | |
'map' | |
); | |
} | |
else | |
{ | |
//***CASE 4: | |
//-11.123 , -12.1234 | |
$found = preg_match("~^(\-?\d+\.?\d*)\s*,\s*(\-?\d+\.?\d*)$~si", $_POST['submission']['url'], $matches); | |
if($found) | |
{ | |
//echo "CASE 4: -11.123 , -12.1234"; | |
return array( | |
array(1 => $matches[1], 2 => $matches[2]), | |
'decimal' | |
); | |
} | |
} | |
} | |
} | |
} | |
} | |
//enable viewing of pending posts for guests | |
function guest_enable_hidden_single_post($query){ | |
//if (is_user_logged_in()) return; | |
//user is not logged | |
// ($query->is_single() || $query->is_category() ) && | |
if ( $query->is_main_query() ) | |
{ | |
//allowed post statuses for guest | |
$query->set('post_status', array('publish','pending')); | |
} | |
} | |
function guest_reload_hidden_single_post($posts){ | |
global $wp_query, $wpdb; | |
//if (is_user_logged_in()) return $posts; | |
//user is not logged | |
// && ($wp_query->is_single() || $wp_query->is_category() ) | |
if ( !$wp_query->post_count && $wp_query->is_main_query() ) | |
{ | |
$posts = $wpdb->get_results($wp_query->request); | |
} | |
return $posts; | |
} | |
//allow guests to view single posts even if they have not post_status="publish" | |
add_filter('pre_get_posts','guest_enable_hidden_single_post'); | |
//reload hidden posts | |
add_filter('the_posts','guest_reload_hidden_single_post'); | |
//allow uploads of additional photos for each post | |
//create a custom post type | |
add_action( 'init', 'create_photo_type' ); | |
function create_photo_type() { | |
register_post_type( 'photo_upload', | |
array( | |
'labels' => array( | |
'name' => __( 'Photos' ), | |
'singular_name' => __( 'Photo' ), | |
'edit_item' => __( 'Edit Photos' ), | |
), | |
'public' => true, | |
'publicly_queryable' => false, | |
'exclude_from_search' => true, | |
'has_archive' => true, | |
'capability_type' => 'post', | |
'show_ui' => false, | |
'show_in_nav_menus' => false, | |
'hierarchical' => false, | |
'supports' => array( 'comments' ), | |
'rewrite' => array('slug' => 'photos') | |
) | |
); | |
} | |
/* | |
//add links to admin bar | |
function add_sumtips_admin_bar_link() { | |
global $wp_admin_bar; | |
if ( !is_super_admin() || !is_admin_bar_showing() ) | |
return; | |
$wp_admin_bar->add_menu( array( | |
'id' => 'sumtips_link', | |
'title' => __( 'Submit'), | |
'href' => __('http://domain.com/submit/'), | |
) ); | |
} | |
add_action('admin_bar_menu', 'add_sumtips_admin_bar_link', 99); | |
*/ | |
//a simple link that allows to delete a post from the frontend | |
function wp_delete_post_link($link = 'delete', $before = '', $after = '') | |
{ | |
global $post; | |
// if ( $post->post_type == 'page' ) { | |
// if ( !current_user_can( 'edit_page', $post->ID ) ) | |
// return; | |
// } else { | |
if ( !current_user_can( 'edit_post', $post->ID ) ) | |
return; | |
// } | |
$link = "<a href='" . wp_nonce_url( get_bloginfo('url') . "/blog/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>"; | |
echo $before . $link . $after; | |
} | |
//is_admin variable | |
$is_admin = false; | |
if(function_exists('is_admin') && is_admin()) $is_admin = true; | |
//remove shortlink meta crap | |
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); | |
//don't add stupid <p> tags to content | |
//remove_filter ('the_content', 'wpautop'); | |
//don't add stupid <p> tags to comments | |
remove_filter ('comment_text', 'wpautop'); | |
//enable thumbnails in theme | |
if (function_exists('add_theme_support')) { | |
add_theme_support('post-thumbnails'); | |
} | |
//calculate the distance between two coordinates | |
function distance_calc($lat1, $lng1, $lat2, $lng2) | |
{ | |
$pi80 = M_PI / 180; | |
$lat1 *= $pi80; | |
$lng1 *= $pi80; | |
$lat2 *= $pi80; | |
$lng2 *= $pi80; | |
$r = 6372.797; // mean radius of Earth in km | |
$dlat = $lat2 - $lat1; | |
$dlng = $lng2 - $lng1; | |
$a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2); | |
$c = 2 * atan2(sqrt($a), sqrt(1 - $a)); | |
$km = $r * $c; | |
return $km; | |
} | |
//wp-post-ratings / supercache fix | |
function get_rating_ajax($id) { | |
if(function_exists('the_ratings')) //plugin enabled | |
{ | |
$nonce = wp_create_nonce('my_rating'); | |
echo '<div id="ratings-wrap-'.$id.'"><img src="/img/loading-ratings.gif" alt="" /></div><script type="text/javascript"><!--'; | |
echo 'jQuery(document).ready(function($){$.ajax({type : "GET",'; | |
echo 'url : "'.get_option('home').'/wp-content/plugins/wp-postratings/wp-postratings.php",'; | |
echo 'data : { wp_ajaxaction : "jquery", post_id : '.$id.', _ajax_check : "'.$nonce.'"},'; | |
echo 'success : function(response){$("#ratings-wrap-'.$id.'").html(response);}'; | |
echo '});});//-->'; | |
echo '</script>'; | |
} | |
} | |
//remove pingback header | |
function remove_x_pingback($headers) { | |
unset($headers['X-Pingback']); | |
return $headers; | |
} | |
add_filter('wp_headers', 'remove_x_pingback'); | |
// @param string $str The text string to split | |
// @param integer $words The number of words to extract. Defaults to 15 | |
function word_split($str,$words=20) { | |
$arr = preg_split("/[\s]+/", $str,$words+1); | |
$arr = array_slice($arr,0,$words); | |
return join(' ',$arr); | |
} | |
function compare_dates($date1, $date2='') { | |
if(empty($date2)) $date2 = time(); | |
$blocks = array( | |
array('name'=>'year','amount' => 60*60*24*365 ), | |
array('name'=>'month','amount' => 60*60*24*31 ), | |
array('name'=>'week','amount' => 60*60*24*7 ), | |
array('name'=>'day','amount' => 60*60*24 ), | |
array('name'=>'hour','amount' => 60*60 ), | |
array('name'=>'minute','amount' => 60 ), | |
array('name'=>'second','amount' => 1 ) | |
); | |
$diff = abs($date1-$date2); | |
$levels = 2; | |
$current_level = 1; | |
$result = array(); | |
foreach($blocks as $block) | |
{ | |
if ($current_level > $levels) {break;} | |
if ($diff/$block['amount'] >= 1) | |
{ | |
$amount = floor($diff/$block['amount']); | |
if ($amount>1) {$plural='s';} else {$plural='';} | |
$result[] = $amount.' '.$block['name'].$plural; | |
$diff -= $amount*$block['amount']; | |
$current_level++; | |
} | |
} | |
return implode(' ',$result).' ago'; | |
} | |
/* | |
//change src of attachment images! | |
//function chg_image_tag($html, $id , $alt, $title){ | |
// | |
// $post_url = "XXX"; | |
// | |
// $html = preg_replace("~\"[\w\./]*\"~", $post_url, $html); | |
// | |
// return $html; | |
//} | |
//add_filter('get_image_tag','chg_image_tag',10,2); | |
*/ | |
//remove traces of the site being wordpress by exchanging javascript files | |
add_action('init', 'remheadlink'); | |
function remheadlink() { | |
remove_action('wp_head', 'rsd_link'); | |
remove_action('wp_head', 'wlwmanifest_link'); | |
remove_action('wp_head', 'wp_generator'); | |
//use newest jquery | |
if(!is_admin()){ | |
wp_deregister_script('jquery'); | |
wp_register_script('jquery', "http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js", false, ''); | |
wp_enqueue_script('jquery'); | |
} | |
//exchange the postratings.js without editing the plugin | |
if(function_exists('the_ratings') && !is_admin()){ | |
wp_deregister_script('wp-postratings'); | |
wp_register_script('wp-postratings', "/postratings-js.js", false, '1.50'); | |
wp_enqueue_script('wp-postratings'); | |
wp_deregister_style('wp-postratings'); | |
wp_register_style('wp-postratings', "/postratings-css.css", false, '1.50'); | |
wp_enqueue_style('wp-postratings'); | |
} | |
} | |
//filter posts being tagged with a single tag | |
add_action('get_header', 'block_singletag'); | |
function singletag_wordpress_groupby($groupby) | |
{ | |
if(empty($groupby)) $groupby = "wp_posts.ID"; | |
return $groupby . " | |
HAVING COUNT( tag_terms.term_id ) <> 1 | |
OR MAX( tag_terms.term_id ) <> 1122 "; | |
} | |
function singletag_wordpress_join($join) | |
{ | |
return $join . " INNER JOIN wp_term_relationships AS tag_term_relationships ON (wp_posts.ID = tag_term_relationships.object_id) | |
INNER JOIN wp_term_taxonomy AS tag_term_taxonomy ON (tag_term_relationships.term_taxonomy_id = tag_term_taxonomy.term_taxonomy_id AND tag_term_taxonomy.taxonomy = 'post_tag') | |
INNER JOIN wp_terms AS tag_terms ON (tag_term_taxonomy.term_id = tag_terms.term_id) "; | |
} | |
function block_singletag() | |
{ | |
if(is_home() || is_tag() || (is_single() && !is_page(22552)) || (!is_page(22552) && !has_tag(array('articles','videos')))) //not submission queue or article | |
{ | |
//exclude the single tag from showing up | |
//$includetags = array(107,1127,1140,1141,932,3,1151,1143,1139,1142,1316,1138,950); | |
$excludetags = array(599,1122); | |
//posts_groupby hook to remove single tag posts | |
//full query: | |
//SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts | |
// INNER JOIN wp_term_relationships AS tag_term_relationships ON (wp_posts.ID = tag_term_relationships.object_id) | |
// INNER JOIN wp_term_taxonomy AS tag_term_taxonomy ON (tag_term_relationships.term_taxonomy_id = tag_term_taxonomy.term_taxonomy_id AND tag_term_taxonomy.taxonomy = 'post_tag') | |
// INNER JOIN wp_terms AS tag_terms ON (tag_term_taxonomy.term_id = tag_terms.term_id) | |
//WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID HAVING COUNT( tag_terms.term_id ) <> 1 OR MAX( tag_terms.term_id ) <> 1122 ORDER BY wp_posts.post_date DESC LIMIT 0, 10 | |
//1: add join tags into query (to receive the above query) | |
add_filter('posts_join', 'singletag_wordpress_join', 10); | |
//2: exclusion of single tag | |
add_filter('posts_groupby', 'singletag_wordpress_groupby', 10); | |
} | |
} | |
//kill the wordpress database connection page is done loading! | |
add_action('shutdown','close_up_shop'); | |
function close_up_shop() { | |
@mysql_close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment