WordPress 插件开发时在插件列表页的几个常用Hook及用例

从来没见过这么漂亮的女律师,做你的当事人一定会很幸福。学会坚强,做一只沙漠中永不哭泣的骆驼!

plugin_action_links

默认的,wordpress插件在插件列表页(wp-admin/plugins.php)的插件名称下面只有启用/禁用 和编辑两个选项,如果想添加链接进去,我们就得用 plugin_action_links 这个Hook了,用例截图:

代码:

1
2
3
4
5
6
7
8
9
10
11
/**
 * Plugin action links
 */
function cwp_plugin_action_links( $links, $file ) {
    $start_link = '<a rel="nofollow noopener noreferrer" href="' . admin_url( 'index.php?page=coolwp-get-started' ) . '">' . esc_html__( 'Start', 'cwp' ) . '</a>';
    $add_on_links ='<a rel="nofollow noopener noreferrer" href="'.CWP_DEV_PLUGIN_ADD_ON_URL.'">' . esc_html__( 'Add Ons', 'cwp' ) . '</a>';
    if ( $file == 'wp-develop-mode/wp-develop-mode.php' )
         array_unshift( $links, $start_link ,$add_on_links);
    return $links;
}
add_filter( 'plugin_action_links', 'cwp_plugin_action_links', 10, 2 );

/** * Plugin action links */ function cwp_plugin_action_links( $links, $file ) { $start_link = '<a rel="nofollow noopener noreferrer" href="' . admin_url( 'index.php?page=coolwp-get-started' ) . '">' . esc_html__( 'Start', 'cwp' ) . '</a>'; $add_on_links ='<a rel="nofollow noopener noreferrer" href="'.CWP_DEV_PLUGIN_ADD_ON_URL.'">' . esc_html__( 'Add Ons', 'cwp' ) . '</a>'; if ( $file == 'wp-develop-mode/wp-develop-mode.php' ) array_unshift( $links, $start_link ,$add_on_links); return $links; } add_filter( 'plugin_action_links', 'cwp_plugin_action_links', 10, 2 );

plugin_row_meta

在Wordpress插件描述(也就是插件名称右侧)下的默认链接有:显示为作者名称的作者链接、显示为插件站点的插件链接,如果想在这里添加链接,就得使用 plugin_row_meta 这个Hook了,用例截图:

上图用对比的方式说明了问题:图中上面的是Wordpress核心团队的导入插件,下面是我的示例插件,可以看出来示例插件比正常的插件多了两个链接:‘Getting Started’和‘Add-ons’,下面是用例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * Plugin row meta links
 */
function cwp_plugin_row_meta( $input, $file ) {
    if ( $file != 'wp-develop-mode/wp-develop-mode.php' )
        return $input;
    $links = array(
        '<a rel="nofollow noopener noreferrer" href="' . admin_url( 'index.php?page=coolwp-get-started' ) . '">' . esc_html__( 'Getting Started', 'cwp' ) . '</a>',
        '<a rel="nofollow noopener noreferrer" href="'.CWP_DEV_PLUGIN_ADD_ON_URL.'">' . esc_html__( 'Add Ons', 'cwp' ) . '</a>',
    );
    $input = array_merge( $input, $links );
    return $input;
}
add_filter( 'plugin_row_meta', 'cwp_plugin_row_meta', 10, 2 );

/** * Plugin row meta links */ function cwp_plugin_row_meta( $input, $file ) { if ( $file != 'wp-develop-mode/wp-develop-mode.php' ) return $input; $links = array( '<a rel="nofollow noopener noreferrer" href="' . admin_url( 'index.php?page=coolwp-get-started' ) . '">' . esc_html__( 'Getting Started', 'cwp' ) . '</a>', '<a rel="nofollow noopener noreferrer" href="'.CWP_DEV_PLUGIN_ADD_ON_URL.'">' . esc_html__( 'Add Ons', 'cwp' ) . '</a>', ); $input = array_merge( $input, $links ); return $input; } add_filter( 'plugin_row_meta', 'cwp_plugin_row_meta', 10, 2 );

after_plugin_row_

这个hook不常用,不过并不是没用,它可以用来在插件行(这里的插件行是指上述插件内容所在的行)后面添加内容,用例截图:

上面截图中的“Activate your license for automatic upgrades. Need a license? Purchase one”就是通过这个Hook添加的内容,用例代码如下:

1
2
3
4
5
6
7
8
9
10
11
/**
*Add your content after wordpress plugin row
*在插件列表插件项目后增加升级/输入、购买许可证的提示
*/
function cwp_add_notice_after_row(){
    $license_key = trim( get_option ('my_plugin_license_key'));/*getting the plugin license */
    $license_key_setting_page = admin_url('options-general.php?page=my-plugin-license-setting-page-slug' );/*define your license setting page link*/
    if(!$license_key)/*if the license is null or false,display the following plugin meta after this plugin row*/
        echo '</tr><tr class="plugin-update-tr"><td colspan="3"><div class="update-message"><a rel="nofollow noopener noreferrer" href="'.$license_key_setting_page.'">'.__('Activate your license','cwpsl').'</a> '.__('for automatic upgrades. Need a license?','cwpsl').' <a rel="nofollow noopener noreferrer" href="http://suoling.net" target="_new">'.__('Purchase one','cwpsl').'</a></div></td>';
}
add_action('after_plugin_row_'.plugin_basename(__FILE__), 'cwp_add_notice_after_row');

/** *Add your content after wordpress plugin row *在插件列表插件项目后增加升级/输入、购买许可证的提示 */ function cwp_add_notice_after_row(){ $license_key = trim( get_option ('my_plugin_license_key'));/*getting the plugin license */ $license_key_setting_page = admin_url('options-general.php?page=my-plugin-license-setting-page-slug' );/*define your license setting page link*/ if(!$license_key)/*if the license is null or false,display the following plugin meta after this plugin row*/ echo '</tr><tr class="plugin-update-tr"><td colspan="3"><div class="update-message"><a rel="nofollow noopener noreferrer" href="'.$license_key_setting_page.'">'.__('Activate your license','cwpsl').'</a> '.__('for automatic upgrades. Need a license?','cwpsl').' <a rel="nofollow noopener noreferrer" href="http://suoling.net" target="_new">'.__('Purchase one','cwpsl').'</a></div></td>'; } add_action('after_plugin_row_'.plugin_basename(__FILE__), 'cwp_add_notice_after_row');

希望本文能对你的插件开发有所帮助!

本文WordPress 插件开发时在插件列表页的几个常用Hook及用例到此结束。没有命中注定的结局,仅有不够努力的过程。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress做公司官网好吗?会不会显得档次很低?

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?