By Guru: Jean-Baptiste Jung

Is Jean-Baptiste Jung your favorite writer? Why not get everything Jean-Baptiste Jung publishes, all the tutorials, hacks, news, plugins, and themes, across all sites, delivered directly to you? Choose your method of delivery on the Gurus page.

Below are the latest articles from Jean-Baptiste Jung. The results are culled from all sources Workflow: WordPress follows.

  • WordPress function: Get category ID using category name

    As usual, let’s start by pasting the function in your functions.php file:

    function get_category_id($cat_name){
    $term = get_term_by(‘name’, $cat_name, ‘category’);
    return $term->term_id;
    }

    Once you saved the file, just call the function with your category name as a parameter. Example:

    $category_ID = get_category_id(‘WordPress Tutorials’);

    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress function: Get category ID using category name

  • How to display your average feed readers

    As usual, the first thing to do is to paste the function in your functions.php file:

    function get_average_readers($feed_id,$interval = 7){
    $today = date('Y-m-d', strtotime("now"));
    $ago = date('Y-m-d', strtotime("-".$interval." days"));
    $feed_url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feed_id."&dates=".$ago.",".$today;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $feed_url);
    $data = curl_exec($ch);
    curl_close($ch);
    $xml = new SimpleXMLElement($data);
    $fb = $xml->feed->entry['circulation'];

    $nb = 0;
    foreach($xml->feed->children() as $circ){
    $nb += $circ['circulation'];
    }

    return round($nb/$interval);
    }

    Once done, you can call the function wherever you want in your theme files. Pass your Feedburner feed id as a parameter:

    <?php
    $nb = get_average_readers('catswhocode');
    echo "I have ".$nb." RSS readers";
    ?>

    Code initially published on Cats Who Blog.
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!How to display your average feed readers

  • 10 life-saving PHP snippets

    Highlight specific words in a phraseSometimes, for example, when displaying search results, it is a great idea to highlight specific words. This is exactly what the following function can do:function highlight($sString, $aWords) {
    if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) {
    return false;
    }

    $sWords = implode (‘|’, $aWords);
    return preg_replace (‘@\b(‘.$sWords.’)\b@si’, ‘<strong style=”background-color:yellow”>$1</strong>’, $sString);
    }Source: http://www.phpsnippets.info/highlights-words-in-a-phraseGet your average Feedburner subscribersRecently, Feedburner counts had lots of problems and it’s hard to say that the provided info is still relevant. This code will grab your subscriber count from the last 7 days and will return the average.function get_average_readers($feed_id,$interval = 7){
    $today = date(‘Y-m-d’, strtotime(“now”));
    $ago = date(‘Y-m-d’, strtotime(“-”.$interval.” days”));
    $feed_url=”https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=”.$feed_id.”&dates=”.$ago.”,”.$today;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $feed_url);
    $data = curl_exec($ch);
    curl_close($ch);
    $xml = new SimpleXMLElement($data);
    $fb = $xml->feed->entry['circulation'];

    $nb = 0;
    foreach($xml->feed->children() as $circ){
    $nb += $circ['circulation'];
    }

    return round($nb/$interval);
    }Source: http://www.catswhoblog.com/how-to-get-a-more-relevant-feedburner-countAutomatic password creationAlthough I personally prefer…

  • Add categories or tags to post programatically

    The first thing to do is to create an array of categories id. Once done, you simply have to use the wp_set_object() function, which take 3 parameters: The post id, an array of categories to add to the post, and the taxonomy type (category in this example).

    $category_ids = array(4, 5, 6);
    wp_set_object_terms( $post_id, $category_ids, ‘category’);

    Adding tags to a post is extremely easy as well. The only difference with the code to add categories is the taxonomy “post_tag” instead of “category”.

    $tag_ids = array(7, 8, 9);
    wp_set_object_terms( $post_id, $tag_ids, ‘post_tag’);

    Thanks to WPProgrammer for this very cool snippet.
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!Add categories or tags to post programatically

  • Display dates as “time ago”, the easy way

    To display human readable dates on your blog, you have to use the human_time_diff() function. The following piece of code will show a post date like “Posted 6 days ago”.
    Paste it anywhere within the loop, save the file, and you’re done.

    Posted <?php echo human_time_diff(get_the_time(‘U’), current_time(‘timestamp’)) . ‘ ago’; ?>

    Credits: PHP Snippets.
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!Display dates as “time ago”, the easy way

  • WordPress hack: Use includes in your posts or pages

    The first step is to paste the following code in your function.php file:

    function digwp_includeContentShortcode($atts) {

    $thepostid = intval($atts[postidparam]);
    $output = '';

    query_posts("p=$thepostid&post_type=page");
    if (have_posts()) : while (have_posts()) : the_post();
    $output .= get_the_content($post->ID);
    endwhile; else:
    // failed, output nothing
    endif;
    wp_reset_query();

    return $output;

    }
    add_shortcode(“digwp_include”, “digwp_includeContentShortcode”);

    Once done, you can use the shortcode in your posts or pages, with the following syntax:
    [digwp_include postidparam=XXXX]
    postidparam is the ID of the post you want to include.
    Credits goes to Chris Coyier for this awesome shortcode!
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress hack: Use includes in your posts or pages

  • How to create a built-in contact form for your WordPress theme

    Getting ReadyYou can see the working form on my site PHP Snippets. It is a site of mine, so don’t hesitate to grab the RSS feed and follow it on Twitter if you want. Step 1: Creating the page templateThe first step is to create a page template. To do so, copy the page.php code into a new file named page-contact.php.We have to add a comment at the beginning of the contact.php file to make sure WordPress will treat the file as a page template. Here’s the code:<?php
    /*
    Template Name: Contact
    */
    ?>Your contact.php file should look like this:<?php
    /*
    Template Name: Contact
    */
    ?>

    <?php get_header() ?>

    <div id=”container”>
    <div id=”content”>
    <?php the_post() ?>
    <div id=”post-<?php the_ID() ?>” class=”post”>
    <div class=”entry-content”>
    </div><!– .entry-content ->
    </div><!– .post–>
    </div><!– #content –>
    </div><!– #container –>

    <?php get_sidebar() ?>
    <?php get_footer() ?>Step 2: Building the formNow, we have to create a simple…

  • How to programatically remove WordPress dashboard widgets

    Simply paste the following into your functions.php file. The code will remove all dashboard widgets, so you should comment lines related to wigets you’d like to keep.

    function remove_dashboard_widgets() {
    global $wp_meta_boxes;

    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts']);
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);

    }

    if (!current_user_can('manage_options')) {
    add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );
    }

    Thanks to NoScope for this code!
    By the way, did you had a look to my latest post on CatsWhoCode about WP 3.0 custom post types?
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!How to programatically remove WordPress dashboard widgets

  • How to create a side blog with WordPress 3.0

    Getting readySo, what are custom post types? That’s simple, custom post types are like a blog post or page, but of a custom defined type. As an example, I decided to list some promo codes on my other blog CatsWhoBlog. I could have used a good old static page, but updating it and adding new promo codes would have been a pain.So I created a custom post type, named coupon and a page template to list all coupons. It’s as simple as that, and now managing coupons & promo codes is extremely easy:Creating the post typeOk, let’s code. The first thing to do is to create a custom post type. To do so, pick up your theme functions.php file, and add the following:function create_my_post_types() {
    register_post_type(‘coupons’,
    array(
    ‘label’ => __(‘Coupons’),
    ’singular_label’ => __(‘Coupon’),
    ‘public’ => true,
    ’supports’ => array(
    ‘title’,
    ‘excerpt’,
    ‘comments’,
    ‘custom-fields’
    ),
    ‘rewrite’ => array(
    ’slug’ => ‘coupons’,…

  • How to automatically create a custom field when a post is published

    Paste the code below into your functions.php file. The only thing you have to do is to edit the cutsom field name on line 6.

    add_action(‘publish_page’, ‘add_custom_field_automatically’);
    add_action(‘publish_post’, ‘add_custom_field_automatically’);
    function add_custom_field_automatically($post_ID) {
    global $wpdb;
    if(!wp_is_post_revision($post_ID)) {
    add_post_meta($post_ID, ‘field-name’, ‘custom value’, true);
    }
    }

    Thanks to wpCanyon for this cool tip!
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!How to automatically create a custom field when a post is published

  • WordPress hack: Display post thumbnail in your RSS feed

    Simply paste the following code in your functions.php file. The post thumbnail should be visible once you saved the file.

    function diw_post_thumbnail_feeds($content) {
    global $post;
    if(has_post_thumbnail($post->ID)) {
    $content = '<div>' . get_the_post_thumbnail($post->ID) . '</div>' . $content;
    }
    return $content;
    }
    add_filter('the_excerpt_rss', 'diw_post_thumbnail_feeds');
    add_filter('the_content_feed', 'diw_post_thumbnail_feeds');

    Thanks to Jeff Starr for this great tip!
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress hack: Display post thumbnail in your RSS feed

  • 10 PHP code snippets for working with strings

    ;)

    Automatically remove html tags from a stringOn user-submitted forms, you may want to remove all unnecessary html tags. Doing so is easy using the strip_tags() function:$text = strip_tags($input, “”);Source: http://phpbuilder.com/columns/Jason_Gilmore060210.php3?page=2Get the text between $start and $endThis is the kind of function every web developer should have in their toolbox for future use: give it a string, a start, and an end, and it will return the text contained with $start and $end.function GetBetween($content,$start,$end){
    $r = explode($start, $content);
    if (isset($r[1])){
    $r = explode($end, $r[1]);
    return $r[0];
    }
    return ”;
    }Source: http://www.jonasjohn.de/snippets/php/get-between.htmTransform URL to hyperlinksIf you leave a URL in the comment form of a WordPress blog, it will be automatically transformed into a hyperlink. If you want to implement the same functionality in your own website or web app, you can use the following code:
    $url = “Jean-Baptiste Jung (http://www.webdevcat.com)”;
    $url = preg_replace(“#http://([A-z0-9./-]+)#”, ‘$0′, $url);
    Source: http://phpbuilder.com/columns/Jason_Gilmore060210.php3?page=2Split text up into 140 char…

  • Best practices for coding HTML emails

    Keep it simple and lightweightIf you have to remember only one of all the tips I’m going to give you in this post, it should be this one. In fact, an html email is not a website, so you shouldn’t try to embed a website into an email.Some years ago, I used to work for a French TV channel and I often had to slice some PSD’s into html emails. The PSD’s contained gradients, funky fonts, and even animated gifs. As a result, the work (despite all efforts I’ve put in it) looked different from one email client to another, the fonts had to be replaced by Arial, and the whole email was extremely heavy and highly relied on images.On the other hand, a simple html email will loaded smoothly, and will be more pleasant to read. (Yoast.com Newsletter)Don’t abuse imagesAn image is worth a thousand words, but it may…

  • WordPress hack: Remove admin name in comments class

    This code simply have to be pasted in your functions.php file to work:

    function remove_comment_author_class( $classes ) {
    foreach( $classes as $key => $class ) {
    if(strstr($class, "comment-author-")) {
    unset( $classes[$key] );
    }
    }
    return $classes;
    }
    add_filter( 'comment_class' , 'remove_comment_author_class' );

    Thanks to C. Bavota for this interesting piece of code!
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress hack: Remove admin name in comments class

  • WordPress tip: Automatically empty Trash

    Simply open your wp-config.php file (located at the root of your WOrdPress install) and paste the following code:
    define(‘EMPTY_TRASH_DAYS’, 10 );
    The second parameter is when to empty trash, in days.
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress tip: Automatically empty Trash

  • Thematic WordPress Theme Toolbox: 10 extremely useful hooks

    If you’re looking for a tutorial on how to create a Thematic child theme, you should read this post.
    Add a favicon
    A favicon is a small image displayed by modern web browsers. It is a must have for all websites, because it allow your visitors to quickly visualize your site among others when they have lots of browser tabs open at the same time. This handy code will add your favicon to your theme. Make sure a favicon.png file is in your child theme images directory, and then paste the code in your functions.php file:
    function childtheme_favicon() { ?>
    <link rel=”shortcut icon” href=”<?php echo bloginfo(’stylesheet_directory’) ?>/images/favicon.png” />
    <?php }
    add_action(‘wp_head’, ‘childtheme_favicon’);
    Source:
    Ad an Internet Explorer specific stylesheet
    Who doesn’t hate Internet Explorer? Unfortunely, most clients will require developers to make their site IE-compliant. And the best way to do so is to use some conditionnal comments and a…

  • WordPress hack: Insert comments programatically

    The following code can be pasted anywhere on your theme files.
    Once this code is executed, it will add a new comment into WordPress database. The returned value is the comment ID, or 0 if a problem happenned.

    $data = array(
    ‘comment_post_ID’ => 1,
    ‘comment_author’ => ‘admin’,
    ‘comment_author_email’ => ‘admin@admin.com’,
    ‘comment_author_url’ => ‘http://www.catswhocode.com’,
    ‘comment_content’ => ‘Lorem ipsum dolor sit amet…’,
    ‘comment_author_IP’ => ‘127.0.0.1′,
    ‘comment_agent’ => ‘Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3′,
    ‘comment_date’ => date(‘Y-m-d H:i:s’),
    ‘comment_date_gmt’ => date(‘Y-m-d H:i:s’),
    ‘comment_approved’ => 1,
    );

    $comment_id = wp_insert_comment($data);

    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress hack: Insert comments programatically

  • WordPress hack: Get popular posts by comments count

    Simply paste the following code where you want to display your most popular posts to be displayed:

    $pop = $wpdb->get_results("SELECT id, post_title, comment_count FROM {$wpdb->prefix}posts WHERE post_type='post' ORDER BY comment_count DESC LIMIT 10");

    <ul>
    foreach($pop as $post) : ?>
    <li> <?php echo $post->post_title; ?> </li>
    <?php endforeach; ?>
    </ul>

    Thanks to Neil Skoglund for this nice piece of code!
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress hack: Get popular posts by comments count

  • 10 sites developers should have in their bookmarks

    Mysql Format DateMySQL Format Date helps you to format your dates using the MySQL DATE_FORMAT function. Just select a common date format and then change it to your suit your needs. The MySQL DATE_FORMAT code will be generated at the bottom of the page which you can then copy into your query. Visit site: http://www.mysqlformatdate.comScript SrcAre you tired of hunting the Internet in order to find the script tag for the latest version of the Javascript library of your choice? ScriptSrc.net has compiled all the latest versions of jQuery, Mootools, Prototype and more in a single page which lets you copy it in your browser clipboard with a single click. Visit site: http://scriptsrc.netEm ChartI never been a fan of ems in CSS files, but sometimes you have to deal with it. In that case, Em chart will translate ems to pixels so you’ll save time and hassle. Visit site: http://aloestudios.com/tools/emchartTwitter…

  • WordPress tip: Quickly secure plugin files

    Paste the following in your .htaccess file. Don’t forget to backup the file before edition!

    <Files ~ "\.(js|css)$">
    order allow,deny
    allow from all
    </Files>

    This recipe has been submitted by Greg Winiarski. Thanks for your contribution!
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress tip: Quickly secure plugin files

  • Top WordPress hacks of early 2010

    Display an incrementing number on each postI always loved how A List Apart numbers its posts. The following hack will let you do the same with your own blog, using a custom field.Implementing this hack is quite simple. First, paste the following function into your functions.php file:function updateNumbers() {
    global $wpdb;
    $querystr = “SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = ‘publish’ AND $wpdb->posts.post_type = ‘post’ “;
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    $counts = 0 ;
    if ($pageposts):
    foreach ($pageposts as $post):
    setup_postdata($post);
    $counts++;
    add_post_meta($post->ID, ‘incr_number’, $counts, true);
    update_post_meta($post->ID, ‘incr_number’, $counts);
    endforeach;
    endif;
    }

    add_action ( ‘publish_post’, ‘updateNumbers’ );
    add_action ( ‘deleted_post’, ‘updateNumbers’ );
    add_action ( ‘edit_post’, ‘updateNumbers’ );Once done, you can display the post number with the following code. Note that it have to be used within the loop.<?php echo get_post_meta($post->ID,’incr_number’,true); ?>Source: http://www.wprecipes.com/how-to-display-an-incrementing-number-next-to-each-published-postAllow your contributors to upload filesIf you’re like me, you have guest contributing articles on your blog and you…

  • Manipulating the DOM with jQuery: 10+ useful code snippets

    Add a CSS class to a specific elementA very clean way to change an element look and feel is to add a css class, instead of adding inline styles. Using jQuery, this is pretty easy to do:$(‘#myelement’).addClass(‘myclass’);Removing a CSS class from a specific elementIt’s great to be able to add some CSS classes, but we also need to know how to remove unwanted classes. The following line of code will do that:$(‘#myelement’).removeClass(‘myclass’);Check if a specific element has a CSS classIf your application or site is frequently adding and removing classes to a particular element, it can be very useful to be able to check out if an element has a certain CSS class.$(id).hasClass(class)Switch CSS using jQueryAs we saw with the previous examples, adding or removing css styles to an element is very simple using jQuery. But what if you want to completely remove the document css file and attach a…

  • WordPress tip: Insert custom content after each post

    You just have to paste the following code into your functions.php and save the file. Once done, custom content will be inserted below each of your posts.

    function add_post_content($content) {
    if(!is_feed() && !is_home()) {
    $content .= '<p>This article is copyright &copy; '.date('Y').'&nbsp;'.bloginfo('name').'</p>';
    }
    return $content;
    }
    add_filter('the_content', 'add_post_content');

    Thanks to Jeff Starr for the great snippet!
    By the way, if you need any kind of WordPress help I’m happy to inform you that I’m starting to work freelance. Don’t hesitate to contact me to get a quote for your new project!
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress tip: Insert custom content after each post

  • 8 useful code snippets to get started with WordPress 3.0

    How to create a custom post typeCustom post type are an incredible step forward for WordPress, because it will allow developers to create post types according to their needs. For now, we have posts, and pages. With WordPress 3.0, we’ll be able to create a new post type called products, where a client can sell his products only, while regular post for his blog.Creating a custom post type is easy: All you have to do is to open your theme functions.php file and paste the following:$args = array(
    ‘label’ => __(‘Products’),
    ’singular_label’ => __(‘Product’),
    ‘public’ => true,
    ’show_ui’ => true,
    ‘capability_type’ => ‘page’,
    ‘hierarchical’ => false,
    ‘rewrite’ => true,
    ‘query_var’ => ‘products’,
    ’supports’ => array(‘title’, ‘thumbnail’)
    );
    register_post_type( ‘product’ , $args );Once you saved the file, login to your WordPress dashboard and have a look at the navigation on the left: A new post type, named Products, has been added.…

  • How to display Twitter-like “time ago” on your WordPress blog

    The first thing to do is to create the function. To do so, paste the following into your functions.php file:

    function time_ago( $type = ‘post’ ) {
    $d = ‘comment’ == $type ? ‘get_comment_time’ : ‘get_post_time’;
    return human_time_diff($d(‘U’), current_time(‘timestamp’)) . ” ” . __(‘ago’);
    }

    Once done, you can use the function in your theme files:
    <?php echo time_ago(); ?>
    Thanks to UpThemes for this trick!
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!How to display Twitter-like “time ago” on your WordPress blog

  • WordPress tip: Allow contributors to upload files

    Nothng hard with this code: The only thing you have to do is to paste it in your functions.php file:

    if ( current_user_can(‘contributor’) && !current_user_can(‘upload_files’) )
    add_action(‘admin_init’, ‘allow_contributor_uploads’);

    function allow_contributor_uploads() {
    $contributor = get_role(‘contributor’);
    $contributor->add_cap(‘upload_files’);
    }

    Big thanks to Altaf Sayani for his contribution to WpRecipes!
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!WordPress tip: Allow contributors to upload files

  • Best practices for WordPress coding

    Think InternationalWhen coding a WordPress theme or plugin, you have to think about those that doesn’t speak English. Using the PHP language, two very useful functions are available for you to create a translatable theme or plugin:The first is the function _e(), which prints on screen the desired text, translated into the user language:_e(“Text to translate”, “textdomain”);The second is __() wich is basically the same than _e(), but returns the text:function say_something() {
    return __(“Text to translate”, “textdomain”);
    }By using those functions, you make your work translatable in any language, which is a very great thing for people that blog in another language than English.Additional resources:I18n for WordPress Developers: A complete guide about using the Gettext libraries and tools to internationalize your themes and plugins.How to make a translatable WordPress theme: A complete tutorial about creating a translatable WordPress theme.WordPress Plugin: Codestyling Localization: A plugin to manage localization files.Don’t reinvent…

  • How to display an incrementing number next to each published post

    The first thing to do is to paste the function into your functions.php file:

    function updateNumbers() {
    global $wpdb;
    $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = ‘publish’ AND $wpdb->posts.post_type = ‘post’ ";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    $counts = 0 ;
    if ($pageposts):
    foreach ($pageposts as $post):
    setup_postdata($post);
    $counts++;
    add_post_meta($post->ID, ‘incr_number’, $counts, true);
    update_post_meta($post->ID, ‘incr_number’, $counts);
    endforeach;
    endif;
    }

    add_action ( ‘publish_post’, ‘updateNumbers’ );
    add_action ( ‘deleted_post’, ‘updateNumbers’ );
    add_action ( ‘edit_post’, ‘updateNumbers’ );

    Once done, you can display the post nimber by pasting the following on your theme file, within the loop:
    <?php echo get_post_meta($post->ID,’incr_number’,true); ?>
    Credits goes to WordPress forums for this very cool piece of code!
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!How to display an incrementing number next to each published post

  • 10+ regular expressions for efficient web development

    Validate an URLIs a particular url valid? The following regexp will let you know./^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/Source: http://snipplr.com/view/19502/validate-a-url/Validate US phone numberThis regexp will verify that a US phone number is valid./^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/Source: http://snippets.dzone.com/posts/show/597Test if a password is strongWeak passwords are one of the quickest ways to get hacked. The following regexp will make sure that:Passwords will contain at least (1) upper case letterPasswords will contain at least (1) lower case letterPasswords will contain at least (1) number or special characterPasswords will contain at least (8) characters in lengthPassword maximum length should not be arbitrarily limited(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$Source: http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=297Get code within <?php and ?>If for some reason you need to grab all the code contained within the <?php and ?> tags, this regexp will do the job:<\?[php]*([^\?>]*)\?>Source: http://snipplr.com/view/12845/get-all-the-php-code-between/Match tel: urlsIn a recent post, I showed you how you can use iPhone special link prfixes to automatically call someone. This regular expression will match those tel: urls.^tel:((?:\+[\d().-]*\d[\d().-]*|[0-9A-F*#().-]*[0-9A-F*#][0-9A-F*#().-]*(?:;[a-z\d-]+(?:=(?:[a-z\d\[\]\/:&+$_!~*’().-]|%[\dA-F]{2})+)?)*;phone-context=(?:\+[\d().-]*\d[\d().-]*|(?:[a-z0-9]\.|[a-z0-9][a-z0-9-]*[a-z0-9]\.)*(?:[a-z]|[a-z][a-z0-9-]*[a-z0-9])))(?:;[a-z\d-]+(?:=(?:[a-z\d\[\]\/:&+$_!~*’().-]|%[\dA-F]{2})+)?)*(?:,(?:\+[\d().-]*\d[\d().-]*|[0-9A-F*#().-]*[0-9A-F*#][0-9A-F*#().-]*(?:;[a-z\d-]+(?:=(?:[a-z\d\[\]\/:&+$_!~*’().-]|%[\dA-F]{2})+)?)*;phone-context=\+[\d().-]*\d[\d().-]*)(?:;[a-z\d-]+(?:=(?:[a-z\d\[\]\/:&+$_!~*’().-]|%[\dA-F]{2})+)?)*)*)$Source:…

  • Automatically replace content in PRE tags by HTML Entities

    Just paste the following in your functions.php file. Once saved, all your content with <pre> and </pre> tags will be automatically replaced by html entities.

    function pre_entities($matches) {
    return str_replace($matches[1],htmlentities($matches[1]),$matches[0]);
    }
    //to html entities; assume content is in the "content" variable
    $content = preg_replace_callback(‘/<pre.*?>(.*?)<\/pre>/imsu’,pre_entities, $content);

    Credits goes to David Walsh for this very cool tip!
    By the way, I recently reviewed the new “WpSEO” plugin on CatsWhoBlog so you should definitely take a look!
    Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!Automatically replace content in PRE tags by HTML Entities