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 finish the “31 Days of Coding”! I was very close though. I was two codes away from finishing 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 complete the “puzzle”.
The first one is how to create short URLs for Twitter, which only allows 140 characters in their posts. Here’s how to do that:
On the functions.php file of your theme, paste the following 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 making a post, enter this wherever you would like the shortlink 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 Coding” series is the Administration notes. We will show you how to put administration notes in your posts, in other words, notes that only you and another admin can see. A regular visitor cannot see them. Paste the following 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 making a new post, put the following wherever 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 gentlemen, this concludes the last post of “31 Days of Coding”! Again, i’m sorry for the delays !
-
Erick
-
http://wpalb.com Klajdi

