If you’d prefer your Log In/Out links use different verbiage, fear not! You can change it:
add_filter( 'loginout', 'log2sign' ); function log2sign( $link ) { $link = str_replace( 'Log ', 'Sign ', $link ); return $link; }
If you’d prefer your Log In/Out links use different verbiage, fear not! You can change it:
add_filter( 'loginout', 'log2sign' ); function log2sign( $link ) { $link = str_replace( 'Log ', 'Sign ', $link ); return $link; }
This is probably more practical if you deal with a lot of special characters, perhaps in foreign languages. But if you want to make sure particular characters are removed from file name when uploaded, you can. As always, WordPress makes this easy.
add_filter( 'sanitize_file_name_chars', 'add_chars_to_filter', 10, 2 ); function add_chars_to_filter ( $special_chars, $filename_raw ) { $special_chars[] = 'e'; return $special_chars; }
This particular is example is useless as-is, unless you have something against our favorite vowel ‘e.’
Did you know that WordPress will create a robots.txt file for you – well, sorta…
If you don’t create an actual robots.txt file, WordPress will create a virtual one for you, meaning that if yoursite.com/robots.txt is requested, WordPress will serve up robots.txt page, even though it doesn’t actually exist on your server.
Now that we’ve got that covered. How can you edit your virtual robots file? Easy
add_filter( 'robots_txt', 'robots_mod', 10, 2 );
function robots_mod( $output, $public ) {
$output .= "Disallow: /topsecret/\n";
return $output;
}
If you’re running a private blog and you want to make sure all logged-out visitors are redirected to the sign-in page, this will do the trick.
add_action( 'template_redirect', 'logged_in_users_only' ); function logged_in_users_only() { if ( !is_user_logged_in() ) wp_redirect( wp_login_url() ); }
If you want users to come back to the front page after logging in, you can pass an argument to the wp_login_url()
function.
add_action( 'template_redirect', 'logged_in_users_only' ); function logged_in_users_only() { if ( !is_user_logged_in() ) wp_redirect( wp_login_url( home_url() ) ); }
This will only redirect users who were initially redirected to the login screen from the front-end. If the user went directly to the login screen – the filter above won’t have been triggered, so they’ll be taken to the administration side.
If you want to change what meta boxes are shown and hidden by default instead of trying to walk a client through the process of customizing their own screen, you can use this code as a starting point:
add_filter( 'default_hidden_meta_boxes', 'change_default_hidden', 10, 2 ); function change_default_hidden( $hidden, $screen ) { if ( 'page' == $screen->id ) { $hidden = array_flip($hidden); unset($hidden['authordiv']); //show author box $hidden = array_flip($hidden); $hidden[] = 'pageparentdiv'; //hide page attributes } return $hidden; }
I needed a very simple way to custom styles or scripts to the <head>, and I didn’t really want to customize the theme.
add_action( 'wp_head', 'wp_head_custom_field' ); function wp_head_custom_field() { if ( is_single() ) { global $post; $html = get_post_meta( $post->ID, 'wp_head', true ); echo $html; } }
With this, I just needed to add my pre-formatted javascript or css to a custom field called ‘wp_head’ and it would then be added to the <head> just like I needed. I added the is_single()
as a precaution.
I wanted to find a way to make it just a little bit more obvious when I switched between the network and site admins. But I didn’t want to have to do a lot of actual customizing. So using built in styles, I found I could just swap the color schemes.
add_action( 'admin_print_styles', 'admin_color_swap', 99 ); function admin_color_swap() { if ( is_network_admin() ) { global $current_user; get_currentuserinfo(); $selected = $current_user->admin_color; $swapped = array( 'fresh' => 'classic', 'classic' => 'fresh' ); $new = $swapped[$selected]; echo '<link rel="stylesheet" type="text/css" href="/wp-admin/css/colors-'.$new.'.css" />'; } }
We often add custom meta boxes to a page or post in order to save specific kinds of data. In some cases, the best place to add those fields really isn’t in some box that gets added to the end of the page. The option needs to be in a bit more prominent location.
Fortunately, it’s possible to add text or form elements to the “Publish” meta box.
add_action( 'post_submitbox_misc_actions', 'article_or_box' );
add_action( 'save_post', 'save_article_or_box' );
function article_or_box() {
global $post;
if (get_post_type($post) == 'post') {
echo '<div class="misc-pub-section misc-pub-section-last" style="border-top: 1px solid #eee;">';
wp_nonce_field( plugin_basename(__FILE__), 'article_or_box_nonce' );
$val = get_post_meta( $post->ID, '_article_or_box', true ) ? get_post_meta( $post->ID, '_article_or_box', true ) : 'article';
echo '<input type="radio" name="article_or_box" id="article_or_box-article" value="article" '.checked($val,'article',false).' /> <label for="article_or_box-article" class="select-it">Article</label><br />';
echo '<input type="radio" name="article_or_box" id="article_or_box-box" value="box" '.checked($val,'box',false).'/> <label for="article_or_box-box" class="select-it">Box</label>';
echo '</div>';
}
}
function save_article_or_box($post_id) {
if (!isset($_POST['post_type']) )
return $post_id;
if ( !wp_verify_nonce( $_POST['article_or_box_nonce'], plugin_basename(__FILE__) ) )
return $post_id;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
if ( 'post' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) )
return $post_id;
if (!isset($_POST['article_or_box']))
return $post_id;
else {
$mydata = $_POST['article_or_box'];
update_post_meta( $post_id, '_article_or_box', $_POST['article_or_box'], get_post_meta( $post_id, '_article_or_box', true ) );
}
}
What I needed at one time was a way to mark a post that would determine whether it would be displayed in a regular article style, or in a special box style. Nowadays, I can use post formats. But the point is, you could put other options there. Perhaps an expiration date field.
If you need to force users to use the HTML uploader, you can do so with this
add_filter( 'flash_uploader', 'force_html_uploader' ); function force_html_uploader( $flash ) { remove_action('post-html-upload-ui', 'media_upload_html_bypass' ); return false; }
The remove_action()
portion removes the “You are using the Browser uploader” text. The rest is what forces the flash setting to be false.