Home  >  Article  >  CMS Tutorial  >  Fifty Actions for WordPress - Demo 50 Examples (from 41 to 50)

Fifty Actions for WordPress - Demo 50 Examples (from 41 to 50)

WBOY
WBOYOriginal
2023-09-03 17:25:071332browse

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.

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 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