The last 2 codes!

Hello guys!

I’m pretty sure you all know how busy life can get at times. Well, as most of you might have noticed, I didn’t get a chance to fin­ish the “31 Days of Cod­ing”! I was very close though. I was two codes away from fin­ish­ing it, and here I am, about 2 and a half weeks later.

So what I’m going to do, is I’m going to post the two codes that I missed, so that will com­plete the “puzzle”.

The first one is how to cre­ate short URLs for Twit­ter, which only allows 140 char­ac­ters in their posts. Here’s how to do that:

On the functions.php file of your theme, paste the fol­low­ing code:

function subzane_shorturl($atts) {
	extract(shortcode_atts(array(
		'url' => '',
		'name' => '',
), $atts));
$request = 'http://u.nu/unu-api-simple?url=' . urlencode($url);
$short_url = file_get_contents($request);
	if (substr($short_url, 0, 4) == 'http')    {
		$name = empty($name)?$short_url:$name;
		return '<a href="'.$short_url.'">'.$name.'</a>';
	} else {
		$name = empty($name)?$url:$name;
		return '<a href="'.$url.'">'.$name.'</a>';
	}
}
add_shortcode('shorturl', 'subzane_shorturl');

Then, when mak­ing a post, enter this wher­ever you would like the short­link to appear:

[shorturl name="shortcode" url="http://codex.wordpress.org/Shortcode_API"]

All you have to do is replace the url in the above code, and you’re all set.

The next code, and the last code of the “31 Days of Cod­ing” series is the Admin­is­tra­tion notes. We will show you how to put admin­is­tra­tion notes in your posts, in other words, notes that only you and another admin can see. A reg­u­lar vis­i­tor can­not see them. Paste the fol­low­ing in the functions.php file of your theme:

function my_formatter($content) {
	$new_content = '';
	$pattern_full = '{(\[raw\].*?\[/raw\])}is';
	$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
	$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

	foreach ($pieces as $piece) {
		if (preg_match($pattern_contents, $piece, $matches)) {
			$new_content .= $matches[1];
		} else {
			$new_content .= wptexturize(wpautop($piece));
		}
	}

	return $new_content;
}

remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');

add_filter('the_content', 'my_formatter', 99);

Then, when mak­ing a new post, put the fol­low­ing wher­ever you would like your notes to appear:

[raw]This portion of text will not be automatically formatted by WP.[/raw]

Now, all you got to do is replace the text inside the [raw] tags, and you’re all set.

Ladies and gen­tle­men, this con­cludes the last post of “31 Days of Cod­ing”! Again, i’m sorry for the delays !

  • Erick

    Hey dude, thanks a lot. Nice job…

    Like or Dis­like: Thumb up 1 Thumb down 0

  • http://wpalb.com Kla­jdi

    You’re wel­come and Erik, and thanks

    Like or Dis­like: Thumb up 0 Thumb down 0

blog comments powered by Disqus