Posts tagged filter
# Here is a simple filter to automatically add a class attribute like widget-order-1 to all widgets within sidebars:
add_action('init', 'add_widget_order_class');
function add_widget_order_class() {
global $wp_registered_sidebars, $wp_registered_widgets;
$sidebars = wp_get_sidebars_widgets();
if (empty($sidebars))
return;
foreach ($sidebars as $sidebar_id => $widgets) {
if (empty($widgets))
continue;
foreach ($widgets as $i => $widget_id) {
$order = $i + 1;
$wp_registered_widgets[$widget_id]['classname'] .= ' widget-order-' . $order;
}
}
}
# All of the web browsers today have input autocomplete or auto-fill feature built-in and chances are that you have your WordPress username and password stored in the browser and the input fields are completed automatically upon visiting /wp-login.php. Wouldn’t it be nice if WordPress would log you in automatically if the fields have been pre-filled by the browser?
Here is a simple snippet of PHP and Javascript that checks if the input fields have been filled and submits the login form one second after the page has been loaded. All you have to do is append #quicklogin to your login bookmark URL so that it looks like http://example.com/wp-admin/#quicklogin and Javascript will do its magic. Add this to your functions.php.
add_action('login_footer', 'enable_admin_quick_login');
function enable_admin_quick_login() {
?>
<script type="text/javascript">
$url_hash = window.location.hash;
if ($url_hash.indexOf('quicklogin') != -1) {
setTimeout(function() {
if (document.loginform.user_login.value && document.loginform.user_pass.value) {
document.loginform.submit();
}
}, 1000);
}
</script>
<?php
}
# 
A “widow” in typography is defined as:
A paragraph-ending line that falls at the beginning of the following page/column, thus separated from the rest of the text.
If you are using the standard inline “Read more” links at the end of post excerpts on index and archive pages, here is a simple filter to ensure that a non-breaking space is added before the “Read more” link:
add_filter('the_content_more_link', 'prepend_non_breaking');
function prepend_non_breaking($more_link) {
return str_replace(' <a ', ' <a ', $more_link);
}
# Ever wanted to use something like example.com/backend or example.com/dash to access your WordPress dashboard or login area? Here is a simple snippet of PHP for your functionality plugin or functions.php to do just that — it uses standard WordPress URL rewrite API.
Update: turns out that in WordPress 3.4 there are new default redirects (/admin, /dashboard and /login) implemented in the core (see this ticket). So here is a better way to add your own redirects which will work only if you don’t have a page or post with the same name (slug) already:
Updated Version:
add_action('template_redirect', 'add_my_custom_redirects');
function add_my_custom_redirects() {
if (!is_404())
return;
$current_uri = untrailingslashit($_SERVER['REQUEST_URI']);
$my_admin_uris = array(
home_url('dash', 'relative'),
home_url('your-custom-uri', 'relative')
);
if (in_array($current_uri, $my_admin_uris)) {
wp_redirect(admin_url());
exit;
}
}
The Obsolete Version:
add_filter('generate_rewrite_rules', 'add_my_custom_rewrites');
function add_my_custom_rewrites($wp_rewrite) {
$my_rewrites = array(
'dash' => 'wp-admin'
);
$wp_rewrite->rules = $my_rewrites + $wp_rewrite->rules;
}
# 
add_filter('wp_list_categories', 'remove_category_link_prefix');
function remove_category_link_prefix($output) {
return str_replace('View all posts filed under ', '', $output);
}
# 
Photo credit added automatically to every photo with a caption (via putukrejums.lv)
Crediting and linking the source of any republished photo or illustration on the web is one of the most important best practices of web publishing. Unfortunately, there isn’t a standard way of doing it in WordPress and authors are left with their own decision on how and where to credit the original author or website. Read more »
# add_filter('get_previous_post_where', 'remove_stickies_adjacent');
add_filter('get_next_post_where', 'remove_stickies_adjacent');
function remove_stickies_adjacent($where) {
return $where . ' AND p.ID NOT IN (' . implode(',', get_option('sticky_posts')) . ')';
}
# Update: Ryan has made this into a plugin — Code Comments.
Add this to your theme’s functions.php to allow readers post fragments of code in their comments (wrapped in <code>...</code>) which are automatically encoded (think of < and <)
add_filter('pre_comment_content', 'encode_code_in_comment');
function encode_code_in_comment($source) {
$encoded = preg_replace_callback('/<code>(.*?)<\/code>/ims',
create_function(
'$matches',
'$matches[1] = preg_replace(
array("/^[\r|\n]+/i", "/[\r|\n]+$/i"), "",
$matches[1]);
return "<code>" . htmlentities($matches[1]) . "</code>";'
),
$source);
if ($encoded)
return $encoded;
else
return $source;
}
Worth noting:
- Everything wrapped in
<code>...</code> is encoded.
- Line breaks after opening
<code> and before closing </code> are removed in order to avoid unnecessary <br /> tags.