Fifty Actions for WordPress - Demo 50 Examples (from 41 to 50)
If you’ve been following our series, you know that we’re in the final stretch of looking at 50 Actions for WordPress. For those of you just joining us, I highly recommend checking out the previous article (as this article continues where we left off) as well as the articles linked to each of the previous articles.
This will give you an idea of where we are now.
let us start!
Injected into the plug-in management page
Plugins also have requirements: they may require in-page scripts or styles for their own options pages. Content can be injected into the tag of a specific plugin page using the admin_head-(plugin_page)
action.
Add styles to your plugin management page
If you need to add some CSS styling to the plugin’s options page, the following code will help you:
<?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: https://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 with your own plugin (tools_page_myplugin/myplugin
), and you're good to go!
Process it before processing the Ping
"Ping" is one of the oldest features of WordPress, and the pre_ping
action allows us to process a ping before it is processed.
Disable self-ping
WordPress does not differentiate between internal and external links when it comes to pinging. To disable self-pinging, you can use 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.
Use get_header()
function
When the template calls the get_header()
function, the get_header
operation is called, which is a function that is very suitable for modifying the WordPress front-end header.
Activate simple maintenance mode
If you are in a hurry and don't have time to install the "Maintenance Mode" plugin and set its options, you can simply use the code below and issue wp_die()
to everyone except the administrator mistake:
<?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 administrators) have the 'activate_plugins'
ability, the site will be closed to everyone except administrators.
Tampering with the login page<head>
The login_head
action helps us control the <head>
tag on the login page.
Eliminate the shaking effect of incorrect credentials
If you don't like the "shaking" effect that occurs when users submit incorrect login information, you can remove it using the following function:
<?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 ?>
But I like this effect.
Using the Dashboard Footer
Sometimes, we may want to control the footer of the admin panel - not the footer section itself, but the section before the </body>
tag. admin_footer
does exactly that.
Add quick styles for post status
Having a consistent set of colors is one of the things that makes a WordPress admin panel beautiful, but I don’t think there’s any harm in some color coding for content that needs to be visually separated (such as different post statuses).
If you are like me and want to be able to distinguish published posts from drafts or other post statuses, use the following code:
<?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/ ?>
Queue scripts and styles in login page
We can use wp_enqueue_scripts
to queue content to the front end, or we can use admin_enqueue_scripts
to queue content to the backend. What about the login page? You guessed it: login_enqueue_scripts
is our hook this time!
Change the logo above the login form
I love the WordPress logo, but I don’t think it should be displayed every time a user logs into my site. If you're thinking the same thing, you can replace the WordPress logo with your own using this helpful snippet:
<?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 theme’s /images/
folder, and you’re good to go!
Add custom columns to user list
Do you know the user list in the "All Users" page of the admin panel? The manage_users_custom_column
action allows us to add a new custom column to this list with the help of the accompanying filter.
Show the user's registration date in one column
Suppose you need to check the registration dates of members in batches. You can check the database record each time you need that information, or you can use this code snippet to add extra columns to the user 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.
Activate using plugin
What do you do when you need to check if a plugin is activated in WordPress? Well, you use the activated_plugin
hook: this handy little action is triggered when the plugin is activated.
每次激活插件时向管理员发送电子邮件
假设您有很多客户网站(使用您的电子邮件地址安装),并且当客户在其网站上安装并激活新插件时,您需要收到通知。
只需使用此函数并将其挂接到 activated_plugins
即可:
<?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 ); } ?>
处理配色方案选项
自 WordPress 3.0 起,我们的管理面板有了“配色方案”,并且我们可以编辑、添加或删除配色方案。 admin_color_scheme_picker
操作使用户可以更改颜色方案。
删除更改配色方案的选项
这个例子不需要太多介绍:如果您需要剥夺用户更改配色方案的权利(例如,因为您有一个特殊的配色方案并且您不希望用户将其更改回来)为默认值),使用下面的代码片段删除该选项:
<?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/ ?>
嘿,我们刚刚从同名的动作挂钩中删除了一个函数。我知道,这很奇怪。
处理注销过程
用户登录、用户注销,当他们注销时,将调用 wp_logout
操作。
注销后将用户重定向到主页
从 WordPress 网站注销有点奇怪:您会被重定向到登录页面,就像 WordPress 需要您再次登录一样。以下是解决该问题并将用户注销时重定向到主页的方法:
<?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/ ?>
现在,每次用户注销时,他们都会看到主页而不是登录表单。
第五部分结束
我们刚刚完成了本文中的最后一批 50 个操作。我希望你喜欢它并从中学到新东西。在下一篇文章中,我们将快速浏览一下我们所看到的内容并结束该系列。
我也想听听你的想法。您对这些行动有何看法?在下面发表您的评论。如果您喜欢这篇文章,别忘了分享!
The above is the detailed content of Fifty Actions for WordPress - Demo 50 Examples (from 41 to 50). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Blogs are the ideal platform for people to express their opinions, opinions and opinions online. Many newbies are eager to build their own website but are hesitant to worry about technical barriers or cost issues. However, as the platform continues to evolve to meet the capabilities and needs of beginners, it is now starting to become easier than ever. This article will guide you step by step how to build a WordPress blog, from theme selection to using plugins to improve security and performance, helping you create your own website easily. Choose a blog topic and direction Before purchasing a domain name or registering a host, it is best to identify the topics you plan to cover. Personal websites can revolve around travel, cooking, product reviews, music or any hobby that sparks your interests. Focusing on areas you are truly interested in can encourage continuous writing

Recently, we showed you how to create a personalized experience for users by allowing users to save their favorite posts in a personalized library. You can take personalized results to another level by using their names in some places (i.e., welcome screens). Fortunately, WordPress makes it very easy to get information about logged in users. In this article, we will show you how to retrieve information related to the currently logged in user. We will use the get_currentuserinfo(); function. This can be used anywhere in the theme (header, footer, sidebar, page template, etc.). In order for it to work, the user must be logged in. So we need to use

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

In the past, we have shared how to use the PostExpirator plugin to expire posts in WordPress. Well, when creating the activity list website, we found this plugin to be very useful. We can easily delete expired activity lists. Secondly, thanks to this plugin, it is also very easy to sort posts by post expiration date. In this article, we will show you how to sort posts by post expiration date in WordPress. Updated code to reflect changes in the plugin to change the custom field name. Thanks Tajim for letting us know in the comments. In our specific project, we use events as custom post types. Now

One of our users asked other websites how to display the number of queries and page loading time in the footer. You often see this in the footer of your website, and it may display something like: "64 queries in 1.248 seconds". In this article, we will show you how to display the number of queries and page loading time in WordPress. Just paste the following code anywhere you like in the theme file (e.g. footer.php). queriesin

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.
