avatar

Andres Jaimes

Why you should not initialize a random generator with a static number

The following examples use random generated numbers initialized with a static seed. Never do it like this. Run the following examples multiple times to understand why it is important to have a good seed. PHP version: #!/usr/bin/php <?php srand(2); echo rand(0,10)."\n"; echo rand(0,5)."\n"; echo rand(0,4)."\n"; echo rand(0,6)."\n"; Java version: import java.util.*; /** * @see https://docs.oracle.com/javase/7/docs/api/java/util/Random.html */ public class RandomDemo { public static void main( String args[] ){ Random r = new Random(2); System.

WordPress Snippets

As a WordPress developer I have collected / created / curated many snippets that I’ve used in my projects. I hope you find them useful.  General HTML / WordPress tags Get your template’s directory <link href="<?php echo get_template_directory_uri(); ?>/style.css" rel="stylesheet" media="screen"> Check if this is the home page <?php if (is_home()) { ... } ?>  Remove the wp_generator tag in the Head section of your HTML I recommend you to add this line for security reasons.

Installing MongoDB/PHP Driver on CentOS 6

Since the module is not included with the default PHP installation, you have to download it from the official repository: cd mkdir mongo-php-driver cd mongo-php-driver curl https://codeload.github.com/mongodb/mongo-php-driver/zip/master > mongo-php-driver-master.zip Unzip it unzip mongo-php-driver-master.zip cd mongo-php-driver-master You need _phpize _to build the module. You can install it from the remi repository: wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm rpm -Uvh remi-release-6.rpm yum --enablerepo=remi install php-devel Configure and build phpize ./configure make all sudo make install Make sure the installation directory is the same as the PHP extension directory by running:

Installing the PHP/MongoDB extension on Mac OSX 10.8

Installing PHP/MongoDB extension is a two steps task on OSX: Install the autoconf tool required for compiling the extension Install the Mongo extension You have to install autoconf in order to avoid the following error: Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script. ERROR: `phpize’ failed Enough talk, hands on work…  Step 1. Install the autoconf tool Download the latest source version:

Creating Shortcodes in WordPress

Shortcodes enable developers to create special kinds of content, following the idea of macros. One very famous short code is “gallery” needed to insert a gallery of images into a post. The good thing about shortcode’s is that final users are free to decide where to insert them. WordPress’s Shortcode API handles all the tricky parsing, making developers life much easier. The following is a snippet for a Shortcode – in order to make it work you have to add it to your functions.

Add a Thumbnail to Admin Post Columns in WordPress

Here is a snippet to add 64×64 images to the Admin Post Columns in WordPress. Having thumbnails may result extremely helpful, specially if you have hundreds of posts. You have to copy and paste the following code into the functions.php file of your theme. function posts_columns($defaults){ $defaults['the_post_thumbs'] = __('Thumbs'); return $defaults; } function posts_custom_columns($column_name, $id){ if($column_name === 'the_post_thumbs'){ echo the_post_thumbnail( array(64, 64) ); } } add_filter('manage_posts_columns', 'posts_columns'); add_action('manage_posts_custom_column', 'posts_custom_columns', 10, 2);  Instead of array(64, 64) you can also use default image sizes like ‘featured_thumbnail’, ‘thumbnail’, ‘medium’, ‘large’ and ‘full’.

How to know if a hex color is light or dark

There is a simple way to know this. The following code takes white (#ffffff) divided it by two as our reference color. If the comparing color is lower than the reference color, then the given color is dark, otherwise it’s light color. function isLightOrDark($hexcolor) { return (hexdec($hexcolor) > 0xffffff/2) ? 'light color' : 'dark color'; }

IIS tips and tricks

Running multiple versions of the .NET framework in IIS When you install a new .NET framework in your IIS server some applications can stop working showing you the following error: Server Application Unavailable In the event viewer you will find this: the same IIS process. Please use the IIS Administration Tool to reconfigure your server to run the application in a separate process. This happens because different .NET frameworks can run in the same server, but cannot share the same application pool, so, to solve this: