search
HomeCMS TutorialWordPressFifty Actions for WordPress - Demo 50 Examples (from 41 to 50)

WordPress 的五十个动作 - 演示 50 个示例(从 41 到 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

The

login_head action helps us control the 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.

Sometimes, we may want to control the footer of the admin panel - not the footer section itself, but the section before the 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to add a comment box to WordPressHow to add a comment box to WordPressApr 20, 2025 pm 12:15 PM

Enable comments on your WordPress website to provide visitors with a platform to participate in discussions and share feedback. To do this, follow these steps: Enable Comments: In the dashboard, navigate to Settings > Discussions, and select the Allow Comments check box. Create a comment form: In the editor, click Add Block and search for the Comments block to add it to the content. Custom Comment Form: Customize comment blocks by setting titles, labels, placeholders, and button text. Save changes: Click Update to save the comment box and add it to the page or article.

How to copy sub-sites from wordpressHow to copy sub-sites from wordpressApr 20, 2025 pm 12:12 PM

How to copy WordPress subsites? Steps: Create a sub-site in the main site. Cloning the sub-site in the main site. Import the clone into the target location. Update the domain name (optional). Separate plugins and themes.

How to write a header of a wordpressHow to write a header of a wordpressApr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

How to display wordpress commentsHow to display wordpress commentsApr 20, 2025 pm 12:06 PM

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

How to upload source code for wordpressHow to upload source code for wordpressApr 20, 2025 pm 12:03 PM

You can install the FTP plug-in through WordPress, configure the FTP connection, and then upload the source code using the file manager. The steps include: installing the FTP plug-in, configuring the connection, browsing the upload location, uploading files, and checking that the upload is successful.

How to copy wordpress codeHow to copy wordpress codeApr 20, 2025 pm 12:00 PM

How to copy WordPress code? Copy from the admin interface: Log in to the WordPress website, navigate to the destination, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code. Copy from a file: Connect to the server using SSH or FTP, navigate to the theme or plug-in file, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code.

What to do if there is an error in wordpressWhat to do if there is an error in wordpressApr 20, 2025 am 11:57 AM

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

How to close comments with wordpressHow to close comments with wordpressApr 20, 2025 am 11:54 AM

How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool