Placing border inside of div and not on its edge

div{
  box-sizing:border-box;
  -moz-box-sizing:border-box;
  -webkit-box-sizing:border-box;
  width:100px;
  height:100px;
  border:1px solid red;
}

Featured Image Thumbnail Sizes

The default image sizes of WordPress are "thumbnail", "medium", "large" and "full" (the size of the image you uploaded). These image sizes can be configured in the WordPress Administration Media panel under Settings > Media. This is how you can use these default sizes with the_post_thumbnail():
the_post_thumbnail();                  // without parameter -> 'post-thumbnail'
 
the_post_thumbnail( 'thumbnail' );       // Thumbnail (default 150px x 150px max)
the_post_thumbnail( 'medium' );          // Medium resolution (default 300px x 300px max)
the_post_thumbnail( 'large' );           // Large resolution (default 640px x 640px max)
the_post_thumbnail( 'full' );            // Full resolution (original size uploaded)
 
the_post_thumbnail( array(100, 100) );  // Other resolutions

Remove space on file name before or after stored in databases

Use str_replace(), it will replace all occurrences of the search string with the replacement string.
str_replace(" ","",$oldname);

Onclick redirect to a link instead of using a href

The following works fine for me, let me know if you need more.
HTML
<button id="promoCodeSubmit" onclick="ClickEvent()">Apply</button>
<input type="hidden" id="promoCodeValue" value="1"/>
JS
function ClickEvent(){
    window.location.href='http://www.test.com';
}

Custom Css for category in wordpress


<div class="post <?php
foreach((get_the_category()) as $cat) {
echo $cat->cat_name . '';
} ?>">
...
</div>

How to Fix ‘Error 406′ or ‘Not Acceptable’ issue

Apache Mod Security update

Few weeks ago I started having “Not Acceptable! Error 406” on one of my WordPress sites when trying to save a post or a page. I kept getting the following message when trying to save a post.
Not Acceptable
An appropriate representation of the requested resource /wp-admin/post.php could not be found on this server
I tried many fixes and nothing seemed to have helped getting rid of the issue. So I decided to reinstall WordPress (How to Uninstall and Reinstall WordPress). Even the reinstall didn’t help! Later I found out that the “Not acceptable! Error 406″ occurs due to Mod Security updates on the server. So if you are having a similar problem then you can try one of the following methods to fix it.

Solution 1 for Fixing 406 Error

Backup your .htaccess file if you have one in the ‘wp-admin’ directory. Then make a ‘.htaccess’ file with the following content and upload it to ‘wp-admin’ directory.
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
You can use any text editor such as Notepad to create this file.
You will need to upload this .htaccedss file to your server. So if you don’t know how to upload a file to your server then check this tutorial on FTP.

Solution 2 for Fixing 406 Error

This is the solution that worked for me for my WordPress site.
Backup your .htaccess file if you have one in the public_html directory.
Open the .htaccess file with any text editor and observe the lines between the “# BEGIN WordPress” and “# END WordPress” tags. Make sure the lines look somewhat like the following. If not then update the file with the following content and upload it to the ‘public_html’ directory.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Hopefully one of these solutions help fix your “Not Acceptable” error. Good luck.

14 Wordpress hacks that every developer should know

1. Force Perfect JPG Images
WordPress automatically compresses images to 90 per cent of the original. While this isn’t such a big deal for most site owners, some people, like photographers, miss this extra 10 per cent.
To ensure the images on your site are at 100 per cent quality, add this to your theme’sfunctions.php file:
1
2
3
4
add_filter( 'jpg_quality', 'high_jpg_quality' );
function high_jpg_quality() {
return 100;
}

2. Proper URLs

It’s a good idea to ensure your URLs are properly formed and are free of invalid characters. You can do this with the esc_url() function:
1
2
$my_url = 'http://myawesomesite.com/?awesome=true';
$url = esc_url( $my_url );

3. Shortcodes in Text Widgets

Widgets are great, but you can make them even better by enabling shortcodes in them with this filter:
1
add_filter( 'widget_text', 'do_shortcode' );

4. Delay When Your Posts Go to RSS

Have you ever published a post and then realised there was a huge mistake in the first paragraph? It’s easy enough to fix the error, but it’s too late for all your subscribers – your post has already been published in their RSS feeds.
Delay when your posts are published to RSS and give yourself time to double-check your live posts. Add this snippet to your functions.php file:
1
2
3
4
5
6
7
8
9
10
11
function publish_later_on_feed($where) {
global $wpdb;
if ( is_feed() ) {
$time_now = gmdate('Y-m-d H:i:s');
$time_delay = '15'; // integer
$time_span = 'MINUTE'; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
$where = " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$time_now') > $time_delay ";
}
return $where;
}
add_filter('posts_where', 'publish_later_on_feed');
You can change the value of $delay to whatever length of time suits you.

5. Display Featured Images In RSS Feed

A picture tells a thousand words, as they say. Encourage subscribers to visit your site rather than just read your content in their RSS feed by displaying featured images by default:
1
2
3
4
5
6
7
add_filter('the_content_feed', 'rss_post_thumbnail');
function rss_post_thumbnail($content) {
global $post;
if( has_post_thumbnail($post->ID) )
$content = '<p>' . get_the_post_thumbnail($post->ID, 'thumbnail') . '</p>' . $content;
return $content;
}

6. Disable HTML in Comments

Spam can be a real pain. Do you get spam in the comments of your site that are littered with links to even more spam?
Disable HTML in your comments to prevent links to spam, bold text and other spammy tactics. Just add the following code to your functions.php file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {
    // convert everything in a comment to display literally
    $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
    // the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
    $incoming_comment['comment_content'] = str_replace( "'", '&apos;', $incoming_comment['comment_content'] );
    return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
    // Put the single quotes back in
    $comment_to_display = str_replace( '&apos;', "'", $comment_to_display );
    return $comment_to_display;
}
add_filter( 'preprocess_comment', 'plc_comment_post', '', 1 );
add_filter( 'comment_text', 'plc_comment_display', '', 1 );
add_filter( 'comment_text_rss', 'plc_comment_display', '', 1 );
add_filter( 'comment_excerpt', 'plc_comment_display', '', 1 );
// This stops WordPress from trying to automatically make hyperlinks on text:
remove_filter( 'comment_text', 'make_clickable', 9 );
Thanks to Peter Keung for this great snippet.

7. Shortcut to Your Site’s URL

WordPress has a simple function that allows you to quickly reference your URL. Not only will save you having to type out your URL time and time again, it will also save you having to change your files if you ever change domains.
1
<?php bloginfo('url'); ?>
You can then use the function like so:
1
<a href="<?php bloginfo('url'); >/about">About Our Company</a>

8. Recent Blog Posts on Homepage

Display your most recent blogs on your homepage without actually making your blog your homepage with this fantastic snippet:
1
2
3
4
5
6
7
<?php query_posts($query_string . '&showposts=5' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="story">
<div class="story-content">
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>
You can change &showposts=5 to however many posts you want to display.

9. Customize the Dashboard Logo

Add your own logo to the dashboard in the backend of WordPress to personalise your installation. This is a great tip for customizing client sites. Just paste the following code:
1
2
3
4
5
6
7
8
9
add_action('admin_head', 'custom_logo');

function custom_logo() {
echo '

<style type="text/css"><!--
#header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }
--></style>';
}

10. Remove Error Message on the Login Page

Whenever your users enter an incorrect login name or password, an error message is displayed on the login page alerting them to an incorrect piece of information. If a hacker were to correctly guess one of those pieces of information, the error message would help them identify what he/she got right.
Block hackers by adding the following filter:
1
add_filter('login_errors',create_function('$a', "return null;"));
This hack isn’t recommended for sites with multiple authors.

11. Remove Publicly Displayed Version of WordPress

Another anti-hack hack. This handy security by obscurity snippet will hide the version of WordPress you’re using, making it harder for hackers exploiting holes in older versions of WordPress.
1
2
3
4
5
6
7
<?php
// Remove the WP version for extra WordPress Security
function remove_wp_version(){ 
return ''; 
} 
add_filter('the_generator', 'remove_wp_version'); 
?>

12. Automatic Copyright Date in Footer

So many sites have outdated copyright information while others show just the current year as their copyright date.
Display your correct copyright date (e.g. © 2005-2014) with this code to yourfunctions.php file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function comicpress_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}
Next, you need to add this to your footer.php file where you would like to display the copyright date:
1
<?php echo comicpress_copyright(); ?>
This function looks for the date of your first post, and the date of your last post. Very cool!
Thanks to Philip M. Hofer (Frumph) of ComicPress for this snippet.

13. Set Default Editor

Do you prefer to use the HTML editor rather than the Visual Editor when writing posts? Make either of these options your default by adding either of the following lines to yourfunctions.php file:
1
2
3
4
5
# Visual Editor as default
add_filter( 'wp_default_editor', create_function('', 'return "tinymce";') );

# HTML Editor as default
add_filter( 'wp_default_editor', create_function('', 'return "html";') );

14. Redirect a User After Login

You can redirect users who login to your site to another URL based on their role using this snippet. Just add it to your functions.php file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php function redirect_user_on_role() { //retrieve current user info global $current_user; get_currentuserinfo(); //If login user role is Subscriber if ($current_user->user_level == 0)
{
wp_redirect( home_url() ); exit;
}
//If login user role is Contributor
else if ($current_user->user_level > 1)
{
wp_redirect( home_url() ); exit;
}
//If login user role is Editor
else if ($current_user->user_level >8)
{
wp_redirect( home_url() ); exit;
}
// For other rolse
else
{
$redirect_to = 'http://google.com/';
return $redirect_to;
}
}
add_action('admin_init','redirect_user_on_role');

?>
Thanks to WP-Snippets for this great snippet.