In the previous part of this series, we started going through 50 WordPress actions, selected among hundreds and we started by reviewing 10 of them. In this tutorial, we're going to see another batch and do examples with each.
Let's begin!
Handling Default WordPress Styles
WordPress has many CSS files for its back-end and front-end to use. With the wp_default_styles
action, we can monkey with the default styles of WordPress.
Removing ie.css
From the WordPress Admin Panel
If you're the only one using the administration panel of your WordPress website and you're not using Internet Explorer, there's no need to load the IE-fixer CSS file, right?
You can use the code below to get rid of the ie.css
:
<?php add_action( 'wp_default_styles', 'wp_default_styles_example' ); function wp_default_styles_example( $wp_styles ) { $wp_styles->remove( 'ie' ); } ?>
Actually, I'm not even sure that WordPress needs this file anymore – after all, ie.css
fixes things that looks bad on IE7 and below and as far as I know, the usage percentage of IE7 has dropped below 1%.
We should suggest a patch, don't you think?
Handling the get_footer()
Function
If you need to work with the get_footer()
function, you don't need to look any further – you can use the action with the same name, get_footer
!
Injecting In-page JavaScript Into the Footer
Let's say you have some jQuery goodness you need to print at the footer of your web pages. You can use these bits of code to make it work:
<?php add_action( 'get_footer', 'get_footer_example' ); function get_footer_example( $name ) { if ( 'new' == $name ) { ?> <script> (function( $ ) { //put all your jQuery goodness in here. })( jQuery ); </script> <?php } } // Example Source: http://codex.wordpress.org/Plugin_API/Action_Reference/get_footer ?>
There. We used some JavaScript code for this example but you can practically run any code to your footer.
Initializing the Admin Panel
This handy little function fires every time an admin page is visited, so it has many different uses. Get creative!
Keeping Non-Admin Users Away From the Admin Panel
Let's say you don't want your subscribers to be able to visit the administration panel and you don't have any contributors, authors or editors. To redirect all the non-admin users to homepage, you can use the code snippet below:
<?php add_action( 'admin_init', 'admin_init_example', 1 ); function admin_init_example() { if ( ! current_user_can( 'manage_options' ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { wp_redirect( site_url() ); exit; } } // Example Source: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_init ?>
If you want, you can change the redirection adddress from your homepage to something else: Just delete the site_url()
bit and enter the desired address with single quotes (like 'http://www.google.com/'
).
Stepping in to the Process of Authentication
The inline documentation defines this action simply with these sentence: "Runs to authenticate a user when they log in."
Allowing Email Addresses as Usernames While Logging In
WordPress doesn't allow users to log in with their email addresses – you have to remember your username. If your user base tends to forget their usernames, you can use the code snippet below and tell your users that they can log in with their email addresses, too:
<?php add_action( 'wp_authenticate', 'wp_authenticate_example' ); function wp_authenticate_example( $username ) { $user = get_user_by( 'email', $username ); if ( ! empty( $user->user_login ) ) { $username = $user->user_login; } return $username; } // Example Source: http://bavotasan.com/2011/log-in-to-wordpress-using-an-email-address/ ?>
Now, your users can enter their email addresses instead of their usernames.
Playing With the Login Form
The login_form
action lets us manipulate the output of the classic WordPress login form.
Displaying a Warning in the Login Form
In the previous example, we showed you how to let your users use their email addresses instead of usernames. If you're not a fan of this behavior, however, you can warn your users about the fact that they can't use their email addresses:
<?php add_action( 'login_form', 'login_form_example' ); function login_form_example() { echo '<p><strong>Remember:</strong> You must enter your username, not your email address!</p>'; } ?>
Of course, you can put other warnings like "Don't click on the 'Remember Me' checkbox if you're on a shared computer!" or a funny one like "If you're under threat by a robber while you're logging in, enter your password backwards and WordPress will automatically call the police – but wait, if you're reading this, then the bad guy will read it too... RUN!". You can also use HTML.
Handling Admin Menu Items
Defined as "runs after the basic admin panel menu structure is in place", the admin_menu
action lets us add or remove menu items (and sub-menu items) into/from the WordPress admin panel's menu.
Removing Menu Items That Shouldn't Be Seen by Clients
It's a familiar scenario for freelance WordPress developers: A client needs access to a certain plugin's "Options" page, but they shouldn't be touching any settings in WordPress' own "Options" pages.
In short, there are pages that clients need to access and there are ones that they shouldn't access. The example below helps us remove menu items from the main admin menu:
<?php if ( ! current_user_can( 'manage_options' ) ) { add_action( 'admin_menu', 'admin_menu_example' ); } function admin_menu_example() { remove_menu_page( 'index.php' ); //Dashboard remove_menu_page( 'edit.php' ); //Posts remove_menu_page( 'upload.php' ); //Media remove_menu_page( 'edit.php?post_type=page' ); //Pages remove_menu_page( 'edit-comments.php' ); //Comments remove_menu_page( 'themes.php' ); //Appearance remove_menu_page( 'plugins.php' ); //Plugins remove_menu_page( 'users.php' ); //Users remove_menu_page( 'tools.php' ); //Tools remove_menu_page( 'options-general.php' ); //Settings } // Example Source: http://wpsnippy.com/remove-top-level-wordpress-dashboard-menu/ ?>
Comment out or delete the lines you don't want, and you're good to go!
Working With the wp()
Function
Let's see what the documentation says about this action:
"Executes after the query has been parsed and post(s) loaded, but before any template execution, inside the main WordPress function wp(). Useful if you need to have access to post data but can't use templates for output."
In short, it fires off after the query's been loaded. Simple, like its name.
A Quick Way to Schedule Cron Jobs in WordPress
While cron jobs are usually hooked to a plugin activation hook, we can also use the wp
action to hook our cron jobs to. Let's see the example provided by the Codex:
<?php add_action( 'wp', 'prefix_setup_schedule' ); /** * On an early action hook, check if the hook is scheduled - if not, schedule it. */ function prefix_setup_schedule() { if ( ! wp_next_scheduled( 'prefix_hourly_event' ) ) { wp_schedule_event( time(), 'hourly', 'prefix_hourly_event'); } } add_action( 'prefix_hourly_event', 'prefix_do_this_hourly' ); /** * On the scheduled action hook, run a function. */ function prefix_do_this_hourly() { // do something every hour } // Example Source: http://codex.wordpress.org/Function_Reference/wp_schedule_event ?>
Notice that there's another action named prefix_hourly_event
– that action is created automatically in the same code snippet, right inside the wp_schedule_event()
function, as its third parameter.
Controlling the <head>
in the Admin Panel Pages
There are various hooks (actions and filters) that have "variables" in their names. The admin_head-(page_name)
action is one of them, which is called in the <head>
for a specific admin page which is defined in the variable.
Changing the Columns Count in the Dashboard
I use a 22'' monitor and since WordPress version 3.8, I'm forced to use 4-column Dashboard which is kind of annoying for me. I'm not sure why I can't set a number of columns like I could earlier, but I found a quick solution to the issue:
<?php add_action( 'admin_head-index.php', 'admin_head_index_php_example' ); function admin_head_index_php_example() { add_screen_option( 'layout_columns', array( 'max' => 3, 'default' => 3 ) ); } // Example Source: http://wpsnippy.com/bring-back-dashboard-screen-layout-options-wordpress-3-8/ ?>
Now I can change the number of columns like we used to – as long as my screen width allows. I still can't choose over 2 columns on my laptop, but I think I can live with that.
Modify the Toolbar Before It's Rendered
The WordPress Toolbar, formerly Admin Bar, is a great and useful navigation element that helps us in both front-end and back-end. And the wp_before_admin_bar_render
action helps us interact with it before it's rendered.
Adding a New Item to the Toolbar
If you want to provide a quick link for your clients to reach you, you can use these lines of code to add a link to their site's Toolbar:
<?php add_action( 'wp_before_admin_bar_render', 'wp_before_admin_bar_render_example' ); function wp_before_admin_bar_render_example() { global $wp_admin_bar; $wp_admin_bar->add_node( array( 'id' => 'contact-designer', 'title' => 'Contact Designer', 'href' => 'http://barisunver.com.tr/contact/', 'meta' => array( 'target' => '_blank' ) ) ); } ?>
Easy, right? You can use the add_node()
function again to create as many links you like.
Processing Profile Updates
The profile_update
hook lets us fetch and work with the user data right after it's updated in the database.
Informing the User About the Profile Update
Let's say that you want to inform users each time they update their profiles. With the help of our handy action and a small function, you can do it:
<?php add_action( 'profile_update', 'profile_update_example' ); function profile_update_example( $user_id ) { $site_url = get_bloginfo( 'name' ); $user_info = get_userdata( $user_id ); $user_name = $user_info->display_name; $user_email = $user_info->user_email; $subject = "Profile updated"; $message = "Hello $user_name,\n\nYour profile has been updated! Please contact us if you're not the one who changed your profile.\n\nThank you for visiting $site_name."; wp_mail( $user_email, $subject, $message ); } // Example Source: http://wpsnipp.com/index.php/functions-php/send-email-notification-when-profile-updates/ ?>
In my opinion, this is a simple yet effective security measure. That said, it wouldn't be effective at all if a potential hacker changes the user's email address, since the email will be sent to the new email address.
End of Part Two
We went through the second batch of 50 actions in this article. I hope you liked and learned new things from it. See you in the next one!
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