If you're just joining us, we're currently working through 50 (of the many) actions that are available in WordPress. In the last post, we covered 21 - 30 so we'll keep the pace going with another set in this post.
If you haven't already, I recommend going back and reading the introduction and then reading through the following post so that you're brought up to speed with everything that we've covered thus far.
With that said, let's begin!
Controlling the Default JavaScript Code
WordPress has many scripts in its core that we can load. The wp_default_scripts
action lets us interact with the default JavaScript files.
Removing "jQuery Migrate" From a WordPress Website
If you use jQuery code and you're absolutely sure that your code is compatible with jQuery version 1.9 or later, then you don't need the jQuery Migrate plugin which supports deprecated jQuery code. Here's how you can deregister it:
<?php add_action( 'wp_default_scripts', 'wp_default_scripts_example' ); function wp_default_scripts_example( &$scripts ) { if ( ! is_admin() ) { $scripts->remove( 'jquery' ); $scripts->add( 'jquery', false, array( 'jquery-core' ) ); } } // Example Source: http://aahacreative.com/2013/08/05/remove-jquery-migrate-wordpress-36/ ?>
Just to be sure, test every jQuery-related output in your frontend and make sure they still work. You don't want to break your website.
The <head>
of Your Front-End
This action is fired off in the wp_head()
function so you could insert stuff into the <head>
element of the front-end pages of your website.
Utilizing Open Graph in Your Website
Open Graph is a very important protocol to help big guys like Facebook, Google, and Twitter understand your pages. If you provide information with the Open Graph protocol, you can define featured images, titles, summaries and such, and help them build structured and valid data for your pages.
Let's see how we can utilize this neat protocol and inject Open Graph-related metadata in our pages' <head>
s:
<?php add_action( 'wp_head', 'wp_head_example' ); function wp_head_example() { global $post; // default image $site_logo = get_stylesheet_directory_uri() . '/images/logo.png'; // homepage if ( is_home() ) { echo '<meta property="og:type" content="website" />'; echo '<meta property="og:url" content="' . get_bloginfo( 'url' ) . '" />'; echo '<meta property="og:title" content="' . esc_attr( get_bloginfo( 'name' ) ) . '" />'; echo '<meta property="og:image" content="' . $site_logo . '" />'; echo '<meta property="og:description" content="' . esc_attr( get_bloginfo( 'description' ) ) . '" />'; } // single post or page elseif ( is_singular() ) { echo '<meta property="og:type" content="article" />'; echo '<meta property="og:url" content="' . get_permalink() . '" />'; echo '<meta property="og:title" content="' . esc_attr( get_the_title() ) . '" />'; if ( has_post_thumbnail( $post->ID ) ) { $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); echo '<meta property="og:image" content="' . esc_attr( $image[0] ) . '" />'; } else echo '<meta property="og:image" content="' . $site_logo . '" />'; echo '<meta property="og:description" content="' . esc_attr( get_the_excerpt() ) . '" />'; } } // Example Source: http://wpdevsnippets.com/set-opengraph-meta-tags-fix-facebook-share/ ?>
Initializing the Theme
Each time a theme's files has been loaded for a WordPress page, the after_setup_theme
action is fired off. Since it's called on every page request, you can hook theme-related functions to this action.
Setting Up Theme-Specific WordPress Features
There are ways to enable WordPress features for themes like post formats or featured images, but the correct way would be to create a function for them and hook the function to the after_setup_theme
action.
<?php add_action( 'after_setup_theme', 'after_setup_theme_example' ); function after_setup_theme_example() { add_editor_style(); add_theme_support( 'post-formats', array( 'video', 'gallery' ) ); add_theme_support( 'post-thumbnails' ); } ?>
See, now our theme can have featured images, we can post videos or galleries, and our "Visual Editor" can have its own style file to make it feel more like the front-end.
Adding Custom Columns to the Media List
The manage_media_custom_column
action helps us add custom columns to the list of uploaded files in the Media Library.
Displaying the ID of Each Upload in a Column
I hate the process of looking up the ID of a featured image that needs to be excluded from a gallery in the same post. I really do. Luckily, I found this code snippet to show me the IDs of uploaded files in the Media Library:
<?php add_filter( 'manage_media_columns', 'manage_media_columns_example' ); add_action( 'manage_media_custom_column', 'manage_media_custom_column_example', 10, 2 ); function manage_media_columns_example( $columns ) { $columns[ 'wps_post_attachments_id' ] = __( 'ID', 'theme-name' ); return $columns; } function manage_media_custom_column_example( $column_name, $attachment_id ){ if ( 'wps_post_attachments_id' === $column_name ) { echo $attachment_id; } } // Example Source: http://wpsnipp.com/index.php/functions-php/media-library-admin-columns-with-attachment-id/ ?>
Hope you like it, too!
Comment Status Transitions
Every time a comment status changes ('approved', 'unapproved', 'spam' or 'trash'), an action hook named transition_comment_status
will be fired. After that, another action will be called with variables in its name: comment_(old_status)_to_(new_status)
. Let's see how the second action works.
E-Mailing the Commenter After Their Comment Is Approved
Suppose you prevent comments to be published without you approving them first; but you also need to inform your visitors about their comments being published when they do. You can email them the good news by using these lines of code as a plugin:
<?php add_action( 'comment_unapproved_to_approved', 'comment_unapproved_to_approved_example' ); function comment_unapproved_to_approved_example( $comment ) { $commenter_email = $comment->comment_author_email; $commenter_name = $comment->comment_author; $post_url = get_comment_link( $comment ); $subject = "Your comment is up!"; $message = "Hello $commenter_name,\n\nYour comment has been approved!You can view it below:\n\n$post_url\n\nThank you for sharing your ideas with us!"; wp_mail( $commenter_email, $subject, $message ); } ?>
Now the commenters will be informed when their comments are up. From there, they may visit your post to see their comments and maybe read others. A fantastic (and easy) way to get returning visitors!
Handling the Loading of Page Templates
WordPress uses what we call "page templates" to display different types of pages like a single post, the homepage, a 404 error, search results, archives and such. And the template_redirect
action fires off when WordPress decides which template will be used.
Redirecting to the Post Permalink if There's Only One Result
Want to save your visitors a click? The code below helps you in a unique way: If there's only one post in the search results, the user will see the page of the post instead of the search result.
A neat trick, if you ask me:
<?php add_action( 'template_redirect', 'template_redirect_example' ); function template_redirect_example() { if ( is_search() ) { global $wp_query; if ( 1 == $wp_query->post_count && 1 == $wp_query->max_num_pages ) { wp_redirect( get_permalink( $wp_query->posts['0']->ID ) ); exit; } } } // Example Source: http://www.elegantthemes.com/blog/tips-tricks/eight-useful-code-snippets-for-wordpress ?>
Now, if someone searches for "guacamole" and there's just one post that mentions guacamole, the visitor will automatically be redirected to that post.
Cool, huh?
Handling WordPress Feeds
Feeds are one of the oldest features in WordPress and they still work like a clock. With the do_feed
action (and other relevant actions), you can control how feeds are handled.
Removing WordPress Feeds Altogether
Removing feed links make feeds inaccessible for almost everyone, but if a visitor knows that you're using WordPress and they have knowledge how feeds work in WordPress, they might just try to add /feed/
after your website URL and reach the feeds. What if you actually need to disable the feeds? This code snippet will help you disable the feeds altogether:
<?php add_action( 'do_feed', 'do_feed_example', 1 ); add_action( 'do_feed_atom', 'do_feed_example', 1 ); add_action( 'do_feed_rdf', 'do_feed_example', 1 ); add_action( 'do_feed_rss2', 'do_feed_example', 1 ); add_action( 'do_feed_rss', 'do_feed_example', 1 ); function do_feed_example() { wp_die( __( '<h1>Feed not available, please visit our <a href="' . get_bloginfo( 'url' ) . '">Home Page</a>!</h1>' ) ); } // Example Source: http://wpdevsnippets.com/disable-rss-feed/ ?>
Easy, right?
Manipulating the Toolbar
The Toolbar (formerly Admin Bar) was introduced in version 3.1 of WordPress and has become the target for both hatred and love – some wants to remove it completely, some can't live without it. Anyway, the admin_bar_menu
action is the main hook for the Toolbar which loads necessary items into the bar.
Always Show the Toolbar to Everyone
The toolbar might come in handy if it doesn't look too bad with your website design – you can use the built-in search box and add new menu items like a link to log in or a link to the contact page.
The code snippet below demonstrates a toolbar which is always shown whether the visitor is logged in or not:
<?php add_action( 'admin_bar_menu', 'admin_bar_menu_example' ); add_filter( 'show_admin_bar', '__return_true' , 1000 ); function admin_bar_menu_example( $wp_admin_bar ) { if ( ! is_user_logged_in() ) { $wp_admin_bar->add_menu( array( 'title' => __( 'Log In' ), 'href' => wp_login_url() ) ); $wp_admin_bar->add_menu( array( 'title' => __( 'Contact' ), 'href' => 'http://www.mywebsite.com/contact/' ) ); } } // Example source: http://blog.ftwr.co.uk/archives/2011/01/05/always-show-admin-bar/ ?>
See how easy it is? Now your visitors can see "Log In" and "Contact" links along with the default search box.
Controlling the Default "Categories" Widget
You can display a list of categories with the wp_list_categories()
function. And this action (which has the same name with the function) helps us customize the output.
Removing the title
Attributes of Category Links
If you don't think that you need the title
attributes of the category links in the widget, you can remove them by using these lines of code in your project:
<?php add_action( 'wp_list_categories', 'wp_list_categories_example' ); function wp_list_categories_example() { return preg_replace( '` title="(.+)"`', '', $output ); } // Example Source: http://wpsnipp.com/index.php/cat/remove-title-attribute-from-wp_list_categories/ ?>
Handling the Search Form Before Processing
In order to use the built-in search feature, you need to use the get_search_form()
function. If you want to monkey with the function before the output is displayed, the pre_get_search_form()
is your guy.
Prepending Informational Text Before the Search Form
Let's say you have a blog where you review hundreds, maybe thousands of commercial products. To inform your visitors about they can enter their barcode numbers to get to the product they need, use the following code to add some informational text before the search box.
<?php add_action( 'pre_get_search_form', 'pre_get_search_form_example' ); function pre_get_search_form_example( $form ) { echo '<div class="search-info">Enter your barcode number below to access the item you want.</div>'; } ?>
Note that you can use this example with the example of the template_redirect
action so when visitors enters a barcode number, they automatically go to the product review.
End of Part Four
We went through the fourth batch of 50 actions in this article. I hope you liked and learned new things from it. Play around with what you've seen and then I'll see you in the next article.
I want to hear your thoughts, too. What do you think about these actions? Post your comments below. If you liked the article, don't forget to share it!
Comments