Created
January 28, 2017 18:19
-
-
Save shubhendusaurabh/8d6dde5da9a319c6ca14c790c09e35f4 to your computer and use it in GitHub Desktop.
wordpress helpful functions
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
// Get Rid of WP Version Footprint Throughout Site | |
function ryu_remove_version() { | |
return ''; | |
} | |
add_filter('the_generator', 'ryu_remove_version'); | |
// Remove Footprints and Search Engine Confusion from wp_head Function | |
remove_action('wp_head', 'wlwmanifest_link'); | |
remove_action('wp_head', 'rsd_link'); | |
remove_action('wp_head', 'wp_generator'); | |
remove_action('wp_head', 'start_post_rel_link'); | |
remove_action('wp_head', 'index_rel_link'); | |
remove_action('wp_head', 'feed_links_extra', 3 ); | |
remove_action('wp_head', 'feed_links', 2 ); | |
remove_action('wp_head', 'parent_post_rel_link', 10, 0 ); | |
remove_action('wp_head', 'start_post_rel_link', 10, 0 ); | |
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); | |
// Remove Emoji CSS and JS from header | |
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); | |
remove_action( 'wp_print_styles', 'print_emoji_styles' ); | |
// Change the Wordpress Login Logo | |
add_action('login_head', 'ryu_custom_login_logo'); | |
function ryu_custom_login_logo() { | |
echo '<style type="text/css"> | |
h1 a { background-image:url('.get_template_directory_uri().'/images/login.png) !important; background-size: 311px 100px !important;height: 100px !important; width: 312px !important; margin-bottom: 30px !important; padding-bottom: 0 !important; } | |
.login form { margin-top: 10px !important; } | |
</style>'; | |
} | |
// Change the Login Logo Link | |
function ryu_url_login_logo(){ | |
return get_bloginfo( 'wpurl' ); | |
} | |
add_filter('login_headerurl', 'ryu_url_login_logo'); | |
// Change Login Logo Alt Text | |
function ryu_login_logo_url_title() { | |
return 'MySite.com Logo'; | |
} | |
add_filter( 'login_headertitle', 'ryu_login_logo_url_title' ); | |
// Change Dashboard Footer Text | |
function change_footer_admin() { | |
echo '<span id="footer-thankyou">My Company - Website design by <a href="#" target="_blank">Ryuzaki Eru</a></span>'; | |
} | |
add_filter('admin_footer_text', 'change_footer_admin'); | |
// Add a Dashboard Widget for Designer Contact Information | |
function ryu_add_dashboard_widgets() { | |
wp_add_dashboard_widget('wp_dashboard_widget', 'Theme Details', 'ryu_theme_info'); | |
} | |
add_action('wp_dashboard_setup', 'ryu_add_dashboard_widgets' ); | |
function ryu_theme_info() { | |
echo "<ul> | |
<li><strong>Developed By:</strong> My Company</li> | |
<li><strong>Website:</strong> <a href='#' target='_blank'>www.buildersociety.com</a></li> | |
<li><strong>Contact:</strong> <a href='mailto:#'>myemail@address</a></li> | |
<li><strong>Phone:</strong> (xxx) xxx-xxxx</li> | |
</ul>"; | |
} | |
// Remove replacement "fancy" quotations, etc. added by Wordpress | |
remove_filter ('single_post_title', 'wptexturize'); | |
remove_filter ('bloginfo', 'wptexturize'); | |
remove_filter ('wp_title', 'wptexturize'); | |
remove_filter ('category_description', 'wptexturize'); | |
remove_filter ('list_cats', 'wptexturize'); | |
remove_filter ('comment_author', 'wptexturize'); | |
remove_filter ('comment_text', 'wptexturize'); | |
remove_filter ('the_title', 'wptexturize'); | |
remove_filter ('the_content', 'wptexturize'); | |
remove_filter ('the_excerpt', 'wptexturize'); | |
// Remove Links From the Admin Bar | |
function ryu_admin_bar_render() { | |
global $wp_admin_bar; | |
$wp_admin_bar->remove_menu('comments'); | |
$wp_admin_bar->remove_menu('wp-logo'); | |
$wp_admin_bar->remove_menu('view-site'); | |
$wp_admin_bar->remove_menu('new-media'); | |
} | |
add_action( 'wp_before_admin_bar_render', 'ryu_admin_bar_render' ); | |
// Stop Empty Search Requests Redirecting to Homepage | |
function ryu_stop_search_redirect( $vars ) { | |
if( isset( $_GET['s'] ) && empty( $_GET['s'] ) ) | |
$vars['s'] = " "; | |
return $vars; | |
} | |
add_filter( 'request', 'ryu_stop_search_redirect' ); | |
// Adjust Yoast Breadcrumbs to Show "Here" Instead of Post Title | |
add_filter('wpseo_breadcrumb_single_link' ,'ryu_change_last_crumb', 10 ,2); | |
function ryu_change_last_crumb($link_output, $link ){ | |
$Path=$_SERVER['REQUEST_URI']; | |
$URI='http://www.mydomain.com'.$Path; | |
if( $link['url'] == $URI ) { | |
$link_output = 'Here'; | |
} | |
return $link_output; | |
} | |
// Unregister Some Default Widgets | |
function unregister_default_widgets() { | |
unregister_widget('WP_Widget_Calendar'); | |
unregister_widget('WP_Widget_Archives'); | |
unregister_widget('WP_Widget_Meta'); | |
unregister_widget('WP_Widget_Search'); | |
unregister_widget('WP_Widget_Recent_Comments'); | |
unregister_widget('WP_Widget_RSS'); | |
unregister_widget('WP_Widget_Tag_Cloud'); | |
unregister_widget('Twenty_Eleven_Ephemera_Widget'); | |
} | |
add_action('widgets_init', 'unregister_default_widgets', 11); | |
// Create Shortcodes | |
function ryuShortCode() { | |
return 'Whatever Code You Need Goes Here'; | |
} | |
add_shortcode('MyAdvertisement', 'ryuShortCode'); | |
//automatically clean up html wysiwyg editor by closing missing tags (ex. <p>, <a>, etc) [wp-admin] | |
function clean_bad_content($bPrint = false) { | |
global $post; | |
$szPostContent = $post->post_content; | |
$szRemoveFilter = array("~<p[^>]*>\s?</p>~", "~<a[^>]*>\s?</a>~", "~<font[^>]*>~", "~<\/font>~", "~style\=\"[^\"]*\"~", "~<span[^>]*>\s?</span>~"); | |
$szPostContent = preg_replace($szRemoveFilter, '', $szPostContent); | |
$szPostContent = apply_filters('the_content', $szPostContent); | |
if ($bPrint == false) return $szPostContent; | |
else echo $szPostContent; | |
} | |
function remove_default_userfields( $contactmethods ) { | |
unset($contactmethods['aim']); | |
unset($contactmethods['jabber']); | |
unset($contactmethods['yim']); | |
return $contactmethods; | |
} | |
add_filter('user_contactmethods','remove_default_userfields',10,1); | |
//resize images that are too big to the Large media size setting [speed] | |
function replace_uploaded_image($image_data){// if there is no large image : returnif(!isset($image_data['sizes']['large']))return $image_data; | |
$upload_dir = wp_upload_dir(); | |
$uploaded_image_location = $upload_dir['basedir'].'/'.$image_data['file']; | |
$large_image_location = $upload_dir['path'].'/'.$image_data['sizes']['large']['file']; | |
unlink($uploaded_image_location); | |
rename($large_image_location,$uploaded_image_location); | |
$image_data['width']= $image_data['sizes']['large']['width']; | |
$image_data['height']= $image_data['sizes']['large']['height']; | |
unset($image_data['sizes']['large']); | |
return $image_data; | |
} | |
add_filter('wp_generate_attachment_metadata','replace_uploaded_image'); | |
//remove emoji [speed] [footprint] | |
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); | |
remove_action( 'wp_print_styles', 'print_emoji_styles' ); | |
add_action('init','jquery_register'); | |
// register from google and for footerfunction jquery_register() { | |
if(!is_admin()) { | |
wp_deregister_script('jquery'); | |
wp_register_script('jquery',('https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js'),false,null,true); | |
wp_enqueue_script('jquery'); | |
} | |
} | |
//remove CSS from header | |
add_action( 'wp_print_styles', 'deregister_my_styles', 100 ); | |
function deregister_my_styles() { | |
wp_deregister_style( 'jetpack_css' ); | |
} | |
//add JS to footer | |
function add_js_to_footer() { | |
?> | |
<script> | |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','//www.google-analytics.com/analytics.js','ga'); | |
ga('create', 'UA-YOURNUMBERSHERE-1', 'auto'); | |
ga('send', 'pageview'); | |
</script> | |
<meta name="msvalidate.01" content="YOURNUMBERSHERE" /> | |
<?php | |
} | |
add_action( 'wp_footer', 'add_js_to_footer', 999 ); | |
//remove WP version string from header [security] [footprint] | |
remove_action('wp_head', 'wp_generator'); | |
//hide login error messages (ex. "Wrong Password") [security] [footprint] | |
add_filter('login_errors',create_function('$a', "return null;")); | |
//remove admin name in comments class [security] [footprint] | |
function remove_comment_author_class( $classes ) { | |
foreach( $classes as $key => $class ) { | |
if(strstr($class, "comment-author-")) { | |
unset( $classes[$key] ); | |
} | |
} | |
return $classes; | |
} | |
add_filter( 'comment_class' , 'remove_comment_author_class' ); | |
//remove WP version param from any enqueued scripts [security] [footprint] [speed] | |
function remove_wpversion_cssjs( $src ) { | |
if ( strpos( $src, 'ver=' ) ) | |
$src = remove_query_arg( 'ver', $src ); | |
return $src; | |
} | |
add_filter( 'style_loader_src', 'remove_wpversion_cssjs', 999 ); | |
add_filter( 'script_loader_src', 'remove_wpversion_cssjs', 999 ); | |
//change URL string from Author to Writer [footprint] | |
function new_author_base() { | |
global $wp_rewrite; | |
$author_slug ='sellers'; | |
$wp_rewrite->author_base = $author_slug; | |
} | |
add_action('init','new_author_base'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment