A simple way to create a slug:

function slug($s) 
{
  return( strtolower( str_replace(" ", "-", preg_replace("/[^a-z A-Z0-9]/", "", $s) ) ) );
}

In WordPress, you can organise the category with a relationship parent/child. This means that a single post belongs to multiple categories. Let’s see how to get the parent category of a post:

function get_parent_category( $post_id ) {
  $cats = wp_get_object_terms($post_id, 'category', ['fields' => 'all'] );
  foreach( $cats as $key => $cat ) { 
    if( $cat->parent === 0) { 
      return $cat;
    }
  }
  return null;
}

Hi there, have a look to this beautiful 3D interactive animation makes with Papervision. Click on the image below to open the animation.

Card

Get code here

Sometimes, you may wish to check a POST variable and when it is not set then get a default value. The first approach could be:

$var = isset( $_POST[ 'var_post' ] ) ? $_POST[ 'var_post' ] : 'default';

Read More

Administrator or subscriber? Let’s see a quick way to check it!

global $user_ID;
//
$capabilities = get_usermeta( $user_ID, 'wp_capabilities');
//
if ( is_array( $capabilities ) ) 
{
  if( $capabilities['Administrator'] == 1 || $capabilities['administrator'] == 1 ) {
    echo "You are an Administrator";
  }
}


Shorter

global $user_ID;
//
$capabilities = get_usermeta( $user_ID, 'wp_capabilities');
// $admin will be true when administrator
$admin = ( $capabilities['Administrator'] == 1 || $capabilities['administrator'] == 1 );


Of course, you can check also the rest of WordPress roles, such as “subscriber” for example.

Let’s see how to convert a date from yyyy-mm-dd hh:mm:ss to yyyymmddhhmmss. This can be useful when you’ll use sort functions for example (such as asort()).

Elegant:

/**
 * Format from "yyyy-mm-dd hh:mm:ss" to "yyyymmddhhmmss"
 */
function plainDate( $d ) 
{
  return( preg_replace( '/(-|:|\040)/', '', $d ) );
}


Obvious:

/**
 * Format from "yyyy-mm-dd hh:mm:ss" to "yyyymmddhhmmss"
 */
function plainDate( $d ) 
{
  return( str_replace( ' ', '', str_replace('-','', str_replace(':', '', $d) ) ) );
}


Insane:

function plainDate( $d ) 
{
  $pd = explode(' ', $d);
  $dd = explode('-', $pd[0]);
  $hp = explode(':', $pd[1]);
  return( $dd[0].$dd[1].$dd[2].$hp[0].$hp[1].$hp[2] );
}


Have a look at the code below:

// mickey is undefined
var mickey;
var spidey = mickey || '5';

// you  will see "5"
alert( spidey );

Also, you can use this tip in:

function myFUnc( param ) 
{
  var newParam = param || 'default';
  // ...
}

The double pipe is great, don’t you? However, you have to be careful, because this tip can be dangerous. See some example below:

var a = 0; // zero or false can be dangerous
var b = a || '5';

alert( a ); // you will see "5"

var a = false;
var b = a || '5';

alert( b ); // you will see "5"

var a = 1;
var b = a || '5';

alert( b ); // you  will see "1"

Does anyone how to cut a long string by words?

function wordCut($c, $l) 
{
  $c= explode(' ',$c);
  for($i=0; $i<$l; $i++) $r[$i] = $c[$i];
  $r= implode(' ', $r).'...';
  return $r;
}
ret-01

“C’era una volta” un tempo in cui la parola Personal Computer non aveva significato. Era il tempo delle console, piccoli marchingegni che si collegavano alla TV, i precursori delle Playstation di oggi. E nelle riviste se ne vedevano davvero di tutti i colori: Intellivision, Atari, Philips, grandi macchine – per l’epoca – che garantivano divertimento unito a stupore. Ed ecco le prime riviste, come l’annuario di VideoGiochi (nella foto) del 1984.  Le pubblicità, accattivanti per l’epoca, mostravano una tecnologia mai vista prima. C’era la Colecovision, con il suo Video Game System, «una console domestica per videogames su cartucce standard Colecovision», all’eccezionale prezzo di 485.000 Lire.

Read More