jQuery(document).ready(function($) {
$('a[href^="http://"]').filter(function(){
// filter out links that have the same domain name as the current page
return this.hostname && this.hostname !== location.hostname;
})
// add a CSS class of "external" to each external link (for styling)
.addClass("external")
// inform visitor that link will open in new window
.attr('title', function(){ return this.title + ' - Link will open in new window' })
// open link in new window once clicked
.click( function() {
window.open(this.href);
return false;
});
});
Code
From WordPress plugins to complex form processing and data validation, writing code started as a hobby and has become a passion. Focusing mainly on PHP scripts with some jQuery thrown in for good measure, these are some of the select scripts that I've written. These are just a few samples, more scripts can be found among the tutorials and articles in the Notes section of this site.
External Link Script
A handy little script to open external links in a standards-compliant way without using the "_target" attribute.
Raw HTML in WordPress Posts
Encodes text in posts between special quicktags
function dbdb_quick_encode($content_text) {
$charset = get_bloginfo('charset');
$replaced_text = preg_replace('#(<!--encode-->)(.*?)(<!--/encode-->)#isme', "'$1'.str_replace(array('<!--nextpage-->', '<!--more-->'), array('<!--nextpage-->', '<!--more-->'), '$2').'$3'", $content_text);
foreach($replaced_text as $k => $v ) {
$encoded_text[$k] = str_replace(array('\"'),array('"'), $v);
}
return $encoded_text;
};
add_filter('wp_insert_post_data','dbdb_quick_encode', 1, 1);
function dbdb_post_encode($text) {
$text = str_replace(array("\r\n", "\r"), "\n", $text);
$text = preg_replace_callback("#(<!--encode-->)(.*?)(<!--/encode-->)#is", 'dbdb_code_encode', $text);
return $text;
};
add_filter('the_content','dbdb_post_encode', 1, 1);
function dbdb_code_encode( $matches ) {
$charset = get_bloginfo('charset');
$text = trim($matches[2]);
$text = str_replace(array('<!--nextpage-->', '<!--more-->'), array('<!--nextpage-->', '<!--more-->'), $text);
$text = htmlspecialchars($text, ENT_QUOTES, $charset);
$text = str_replace('[','[', $text);
$text = str_replace(array("\r\n", "\r"), "\n", $text);
$text = preg_replace("#\n\n\n+#", "\n\n", $text);
if ( "<!--encode-->" != $matches[1] ) {
$text = $matches[1].$text.$matches[3];
}
return $text;
};
Comment Validation
An anti-spam validation script for WordPress comment forms.
/**
* Anti-spam measure: checks Post Title entered by user against Post Title in database
*/
$redirect = $_POST['redirect_to'];
// Get the post's ID
$get_post_ID = (int) $_POST['comment_post_ID'];
// Get Post data
$get_post_ID = get_post($get_post_ID);
// Init a variable for the post title
$title = $get_post_ID->post_title;
// Count the words in the Post Title (returns an array)
$title_words = str_word_count($title, 1);
// Init a variable for the first word in the Post Title
$first_title_word = array_slice( $title_words, 0, 1);
// Init a variable for the title the user entered
$ptitle = $_POST['ptitle'];
// Count the words in the title the user entered(returns an array)
$ptitle_words = str_word_count($ptitle, 1);
// Init a variable for the first word in the title the user entered
$first_ptitle_word = array_slice( $ptitle_words, 0, 1);
// Case-sensitive match the titles
if ( $first_title_word != $first_ptitle_word ) {
// unset all cookies used for validation
setcookie('err_ptitle', '', time() - 30, COOKIEPATH, COOKIE_DOMAIN);
// ..define an error message to display to alert the user,
$msg = 'The title you entered is incorrect. Please re-enter the validation info requested.';
// ..set a temporary cookie to display error msg (for 10 sec.) on Post page
setcookie('err_ptitle', $msg, time() + 10, COOKIEPATH, COOKIE_DOMAIN);
wp_redirect( $redirect . '#comment-error' );
exit;
}