If you've been following with us through this series, then you know we're in the homestretch in looking at our 50 actions of WordPress. For those who are just joining us, I urge you to check out the previous article (as this picks up right where it left off) as well as the articles linked from each one prior.
That will bring you up to speed with where we're headed now.
Let's begin!
Injecting to the <head>
of Plugin Admin Pages
Plugins have needs, too: They may need in-page scripts or styles for their own option pages. With the admin_head-(plugin_page)
action, it's possible to inject things into the <head>
tag for specific plugin pages.
Adding Styling to Your Plugin Admin Page
If you ever need to add some CSS styling to your plugin's options page, the code below will help you with that:
<?php add_action( 'admin_head-tools_page_myplugin/myplugin', 'admin_head_plugin_page_example' ); function admin_head_plugin_page_example() { echo '<style type="text/css">' . '/* your style here */' . '</style>'; } // Example Source: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_head-(plugin_page) ?>
Change the commented-out line with your CSS code, replace the second part of the action name (tools_page_myplugin/myplugin
) with your own plugin and you're good to go!
Handling Pings Before They're Processed
"Pings" are one of the oldest features of WordPress and the pre_ping
action lets us handle the pings before they're processed.
Disable Self-Pings
WordPress doesn't distinguish internal links from external links, when it comes to pings. To disable self-pings, you can utilize this handy little code snippet:
<?php add_action( 'pre_ping', 'pre_ping_example' ); function pre_ping_example( &$links ) { foreach ( $links as $l => $link ) { if ( 0 === strpos( $link, get_home_url() ) ) { unset( $links[ $l ] ); } } } // Example Source: http://wpdevsnippets.com/remove-slef-pings/ ?>
From now on, your WordPress installation will not ping its own posts.
Working With the get_header()
Function
The get_header
action is called when the template calls the get_header()
function, making it perfect for functions that modify the header of WordPress' front-end.
Activate a Simple Maintenance Mode
If you're in a hurry and don't have time to install a "maintenance mode" plugin and set its options, you can simply use the code below and give a wp_die()
error to everyone except administrators:
<?php add_action( 'get_header', 'get_header_example' ); function get_header_example() { if ( ! current_user_can( 'activate_plugins' ) ) { wp_die( 'The website will be back soon.' ); } } // Example Source: http://wp-snippets.com/articles/7-code-snippets-you-should-use-on-every-site/ ?>
Since only administrators (and super admins) have the 'activate_plugins'
capability, the website will be shut down to everyone except admins.
Tampering With the <head>
of the Login Page
The login_head
action helps us control the <head>
tag on the login page.
Removing the Shake Effect for Incorrect Credentials
If you don't like that "shake" effect when a user submits incorrect login information, you can use the function below to remove it:
<?php add_action( 'login_head', 'login_head_example' ); function login_head_example() { remove_action( 'login_head', 'wp_shake_js', 12 ); } // Example Source: http://wordpress.org/support/topic/plugin-simplemodal-login-remove-shake-effect ?>
I like the effect, though.
Working With the Footer of the Dashboard
On occasion, we might want to control the footer of our admin panels – not the footer section per se, but the part before the </body>
tag. The admin_footer
action does exactly that.
Adding Quick Styles for Post Statuses
Having a consistent set of colors is one of the reasons that makes WordPress' admin panel beautiful, but I don't think there's any harm in some color-coding for things that need to be separated visually – like different post statuses.
If you're like me and want to be able to distinguish published posts from drafts or other post statuses, use the code below:
<?php add_action( 'admin_footer', 'admin_footer_example' ); function admin_footer_example() { echo '<style type="text/css"> .status-draft { background-color: #FCE3F2; } .status-pending { background-color: #87C5D6; } .status-future { background-color: #C6EBF5; } .status-private { background-color: #F2D46F; } </style>'; } // Example Source: http://wpsnipp.com/index.php/functions-php/change-admin-postpage-color-by-status-draft-pending-published-future-private/ ?>
Enqueuing Scripts and Styles in the Login Page
We can enqueue stuff to the frontend with wp_enqueue_scripts
, and we can enqueue stuff to the back end with admin_enqueue_scripts
. What about the login page? You guessed it: This time the login_enqueue_scripts
is our hook!
Changing the Logo Above the Login Form
I like the WordPress logo, but I don't think it should be shown every time my users log in to my websites. If you think the same way, you can replace the WordPress logo with your own by using this useful code snippet below:
<?php add_action( 'login_enqueue_scripts', 'login_enqueue_scripts_example' ); function login_enqueue_scripts_example() { echo '<style type="text/css">' . '#login h1 a {' . 'background-image: url(' . get_bloginfo( 'template_directory' ) . '/images/login-logo.png);' . 'padding-bottom: 30px;' . '}' . '</style>'; } // Example Source: http://wpsnippy.com/add-custom-login-logo-in-your-wordpress-blog/ ?>
Put the login-logo.png
file into the /images/
folder of your theme and you're good to go!
Adding Custom Columns to the Users List
You know the user list in the "All Users" page in the admin panel? The manage_users_custom_column
action allows us add new custom columns to that list with the help of an accompanying filter.
Displaying Registration Dates of Users in a Column
Suppose you need to see your members' registration dates in bulk. You can check your database records each time you need that information, or you can use this code snippet to add an extra column to the Users list:
<?php add_action( 'manage_users_custom_column', 'manage_users_custom_column_example', 10, 3 ); add_filter( 'manage_users_columns', 'manage_users_columns_example' ); // create a new column named "Zip Code" function manage_users_columns_example( $columns ) { $columns['user_registered'] = __( 'Registration Date', 'theme-name' ); return $columns; } // fill the column cells with the registration dates function manage_users_custom_column_example( $value, $column_name, $user_id ) { if ( 'user_registered' == $column_name ) { $userdata = get_userdata( $user_id ); return $userdata->user_registered; } } // Example Source (Idea): http://tommcfarlin.com/add-custom-user-meta-during-registration/ ?>
Now you know more about your members.
Working With Plugin Activations
What do you do when you need to check when a plugin is activated in WordPress? Well, you use the activated_plugin
hook: This handy little action is fired on plugin activation.
Sending an Email to the Admin Each Time a Plugin Is Activated
Suppose you have lots of client websites (which were installed with your email address) and you need to be informed when clients installs and activates a new plugin on their websites.
Just use this function and hook it to activated_plugins
and you're good to go:
<?php add_action( 'activated_plugin', 'activated_plugin_example', 10, 2); function activated_plugin_example( $plugin, $network_activation ) { $to = get_option( 'admin_email' ); $subject = 'A plugin has been activated'; $body = "Hey,\n\nThe following plugin has just been activated:\n\n$plugin\n\nCheers!"; wp_mail( $to, $subject, $body ); } ?>
Handling the Color Scheme Options
Since WordPress 3.0, we have "color schemes" for the admin panel and we're allowed to edit, add or remove the color schemes. And the admin_color_scheme_picker
action makes it possible for users to change the color scheme.
Removing the Option to Change Color Schemes
This example doesn't need much introduction: If you ever need to take away the right to change color schemes from your users (say, because you have a special color scheme and you don't want your users to change it back to the default), use the code snippet below to remove the option:
<?php if( is_admin() ) { remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' ); } // Example Source: http://wpsnipp.com/index.php/functions-php/remove-admin-color-scheme-picker-from-profile/ ?>
Hey, we just removed a function from an action hook with the same name. I know, it's weird.
Handling the Process of Logging Out
Users log in, users log out, and when they log out, the wp_logout
action is called.
Redirecting User to the Homepage After Logout
Logging out from a WordPress website is kind of weird: You're redirected to the login page, like WordPress needs you to log in again. Here's how you fix the situation and redirect users to homepage when they log out:
<?php add_action( 'wp_logout', 'wp_logout_example' ); function wp_logout_example() { wp_redirect( home_url() ); exit(); } // Example Source: http://wpsnippy.com/auto-redirect-users-after-logout/ ?>
Now each time a user logs out, they will see the the homepage instead of the login form.
End of Part Five
We've just completed through through the last batch of 50 actions in this article. I hope you liked and learned new things from it. In the next article, we're going to have a quick look at what we've seen and close the series.
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