Skip to content

Instantly share code, notes, and snippets.

@tahirtaous
tahirtaous / Add Custom CSS to Anchors links in WordPress
Last active May 1, 2024 14:25
How to Add Custom CSS Class Directly to Menu Item (Anchors) Links in WordPress Theme
@tahirtaous
tahirtaous / .htaccess
Last active August 29, 2015 14:14 — forked from ahmadawais/.htaccess
#addthese two lines in the root folder of your old domain inside .htaccess file
RewriteEngine on
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]
@tahirtaous
tahirtaous / Hide Screen Options button WordPress dashboard
Last active October 8, 2018 12:46
hide screen Options and Help button from WordPress dashboard, useful when developing for clients
// hide screen options and Help Buttons from WordPress Dashboard
// tested WP 4.1 Jan 25 2015
// https://wordpress.org/support/topic/remove-items-from-screen-options
// paste this code to functions.php file
add_action( 'admin_head', 'remove_wordpress_cfields' );
function remove_wordpress_cfields() {
echo '<style>#screen-meta-links { display: none; }</style>';
}
@tahirtaous
tahirtaous / WordPress functions only for mobile users
Created January 25, 2015 14:30
This is a little-known built-in function of WordPress that detects whether a user is visiting on a mobile device or not and allows to display content accordingly. It is quite handy function for those willing to include a bit of code for only mobile users.//
// This code will show message only on mobile devices
// only mobile users will see this message
// Source: https://www.cms2cms.com/blog/6-useful-yet-relatively-unknown-wordpress-functions/
// Tested: WordPress 4.1 Jan 25 2015
<?php if( wp_is_mobile() ) : ?>
Visit our website on your desktop for a richer user experience
<?php endif ?>
@tahirtaous
tahirtaous / Last Modified Date or time WordPress
Created January 25, 2015 14:26
This function outputs the difference between two timestamps and is presented in a human readable format like “10 mins”, “1 hour”, “3 days”. It has some similarity with the popular Twitter “time ago” function, and is considered to be quite useful for displaying when the post was last modified.
// Source: https://www.cms2cms.com/blog/6-useful-yet-relatively-unknown-wordpress-functions/
// This function will generate something like this
// This post was published 2 weeks ago
// Tested : WordPress 4.1
<?php
echo 'This post was published ' . human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) . ' ago';
?>
@tahirtaous
tahirtaous / WordPress antispam email functions
Last active August 29, 2015 14:14
This is probably one of the most well-hidden functions in the WordPress codebase. This function aims to convert an email into HTML entities in order to mask email addresses from evil scrapers.
// Reference : https://www.cms2cms.com/blog/6-useful-yet-relatively-unknown-wordpress-functions/
// add this code to sidebar.php or anywhere you want to display email addess
// remove <?php and ?> if you are going to paste this in functions.php
// Tested WordPress 4.1 Jan 25 2015
<?php
$email = '[email protected]';
echo 'You can contact me at ' . antispambot( $email ) . ' any time';
@tahirtaous
tahirtaous / Remove version string, error messages for better WordPress Security
Last active August 29, 2015 14:13
Security through obscurity is the name of the game here. These functions do three different things. Remove the version string from the code. No point in telling folks what version we're running. Removes any error messages (Wrong Password, No Such User, etc.) from admin login screens When the admin posts a comment, a CSS class is added. This remo…
// Source http://wordpress.stackexchange.com/posts/28530/revisions
// Tested and worked on WordPress 4.1 Beta
//REMOVE VERSION STRING FROM HEADER
remove_action('wp_head', 'wp_generator');
//HIDE LOGIN ERROR MESSAGES (Wrong Password, No Such User etc.)
add_filter('login_errors',create_function('$a', "return null;"));
// Remove admin name in comments class
// Source: http://www.wprecipes.com/wordpress-hack-remove-admin-name-in-comments-class
@tahirtaous
tahirtaous / Add contact details to WordPress dashnoard for clients
Last active August 29, 2015 14:13
You can use this for client sites as a simple point of reference to contact you as the developer
// Wordpress Custom Admin Footer
// customize admin footer text. Add this to functions.php
// Source http://wordpress.stackexchange.com/posts/6005/revisions
// Modified by Tahir Taous
// customize admin footer text Will be visible in Dashboard
function custom_admin_footer() {
echo 'Created By <a href="//tahirtaous.com/contact">Tahir Taous</a> ';
}
@tahirtaous
tahirtaous / function to Unregister WP Default Widgets
Created January 11, 2015 19:18
WordPress has many default widgets such as recent post, calendar, meta, pages, rss and others, you can delete all widgets with this function. Add this to your functions.php file
// Source : http://wordpress.stackexchange.com/questions/1567/best-collection-of-code-for-your-functions-php-file
// unregister all default WP Widgets
function unregister_default_wp_widgets() {
unregister_widget('WP_Widget_Pages');
unregister_widget('WP_Widget_Calendar');
unregister_widget('WP_Widget_Archives');
unregister_widget('WP_Widget_Links');
unregister_widget('WP_Widget_Meta');
unregister_widget('WP_Widget_Search');
unregister_widget('WP_Widget_Text');
@tahirtaous
tahirtaous / Delete PostRevisions Without Plugin
Last active August 29, 2015 14:13
Delete WordPress post revisions without plug in with this Native function
// Open you functions.php file and paste the following code:
// Save the file and open your blog homepage to run the code. Once done, there’s no need to keep the code snippet in your
// functions.php file, as it will always delete all post revisions. So simply remove it.
// http://www.wprecipes.com/easily-delete-wordpress-post-revisions-using-your-fuctions-php-file
$wpdb->query( "
DELETE FROM $wpdb->posts
WHERE post_type = 'revision'
" );