Category Archives: WordPress

Disable HTML in WordPress comments

By default WordPress allows users to include HTML tags on comments. This is bad because users can even add hyperlinks in their comments and this can lead to spam comments. To disable HTML tags in WordPress comments add this line to the functions.php file: add_filter( ‘pre_comment_content’, ‘esc_html’ );add_filter( ‘pre_comment_content’, ‘esc_html’ );

How to Disable Post Revisions in WordPress

To disable post revisions in WordPress and reduce database file size, you just need to edit the file wp-config.php and add this line (above the “ABSPATH” otherwise it won’t work): define(‘WP_POST_REVISIONS’, false );define(‘WP_POST_REVISIONS’, false ); Or alternatively you can limit the number of revisions to store: define( ‘WP_POST_REVISIONS’, 3);define( ‘WP_POST_REVISIONS’, 3); To delete post revisions […]

Create a screenshot of a website with PHP

Using the WordPress mShot API we can generate thumbnails of websites: function capture_website_screenshot( $website, $saveas ) { $url = "http://s.wordpress.com/mshots/v1/http%3A%2F%2F".$website."%2F?w=250"; $image = file_get_contents( $url ); if ( stripos( $image, "JFIF" ) ) { $f = @fopen($saveas, ‘w’); if (!$f) { return false; } else { $bytes = fwrite($f, $image); fclose($f); } } else { return […]

WordPress get Page ID outside the Loop

The recommended way to get the Page ID outside the loop is: $page_object = get_queried_object(); $page_id = get_queried_object_id();$page_object = get_queried_object(); $page_id = get_queried_object_id();

Fixed height and vertical scrollbar on SyntaxHighlighter Evolved

With SyntaxHighlighter Evolved you can post syntax-highlighted code on your site. You can set a fixed height and vertical scrollbar by adding this code: <style type="text/css"> .syntaxhighlighter { width: 500px; height: 450px; overflow-y: auto !important; overflow-x: auto !important;} </style><style type="text/css"> .syntaxhighlighter { width: 500px; height: 450px; overflow-y: auto !important; overflow-x: auto !important;} </style> At the […]

Organize uploads folder by Post ID, Slug, Author, Media Type

Very useful code snippet that can help you to organize the uploads folder of WordPress by post_name (aka slug), post_author, post_id or media type (such as videos, images, documents, etc). This is an example of custom uploads folders that can be created: /wp-content/uploads/post-slug/ /wp-content/uploads/123/ /wp-content/uploads/user-admin/ /wp-content/uploads/videos/ /wp-content/uploads/documents/ /wp-content/uploads/others/ /wp-content/uploads/images/ /wp-content/uploads/documents//wp-content/uploads/post-slug/ /wp-content/uploads/123/ /wp-content/uploads/user-admin/ /wp-content/uploads/videos/ /wp-content/uploads/documents/ /wp-content/uploads/others/ […]

Configure FastCGI_Cache for WordPress and Nginx

Edit the vHost file of your WordPress blog as follow: # Define cache path, size, memory name, inactive time fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:100m inactive=60m; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale updating error timeout invalid_header http_500; fastcgi_temp_path /var/cache/nginx/tmp;   # Add a custom header that shows if the page is cached or not # X-Cache: HIT = cached | […]

Secure WordPress with Nginx

Example of vHost config for a secured WordPress website: server { listen 80; server_name website.com; # Redirect non-www to www (website.com -> www.website.com) return 301 http://www.$server_name$request_uri; }   server { listen 80; server_name www.website.com; access_log /var/www/website.com/logs/access.log main; error_log /var/www/website.com/logs/error.log warn; root /var/www/website.com/public/htdocs; index index.html index.htm index.php;   # Do not log robots.txt location = /robots.txt […]

Lighttpd rewrite rules for WordPress sites

Rewrite rules compatible with WordPress permalinks: # Handle 404 errors server.error-handler-404 = "/index.php"   # Rewrite rules url.rewrite-final = (   # Exclude some directories from rewriting "^/(wp-admin|wp-includes|wp-content|gallery2)/(.*)" => "$0",   # Exclude .php files at root from rewriting "^/(.*.php)" => "$0",   # Handle permalinks and RSS feeds "^/(.*)$" => "/index.php/$1" ) # Handle […]

Display excerpt in Twenty Twelve WordPress Theme

Edit the file content.php and find this line: <?php if ( is_search() ) : // Only display Excerpts for Search ?><?php if ( is_search() ) : // Only display Excerpts for Search ?> Change that line to: <?php if ( is_search() || is_home() || is_category() || is_tag() ) : // Only display Excerpts for Search […]