wordpress启动
Developers work hard building plugins and themes that benefit users. Whether it’s an eCommerce system, booking plugin or gallery showcase for example, there’s a dedicated developer or two that’s worked hard bringing everything together.
开发人员会努力构建有益于用户的插件和主题。 例如,无论是电子商务系统,预订插件还是画廊展示柜,都有一个或两个专门的开发人员努力工作,将所有内容整合在一起。
Documentation on the other hand sadly becomes an after-thought. It is often left to either a scarce few sentences on the WordPress plugin directory, a series of notes on GitHub or if you’re lucky a poorly maintained external website (that may vanish at any moment).
另一方面,可悲的是文档成为事后的想法。 它通常在WordPress插件目录上只留下很少的几句话,在GitHub上提供了一系列说明,或者如果您很幸运,外部网站的维护不善(随时可能消失)。
WordPress comes with an in-built Contextual Help Screen that can be accessed from anywhere in the admin dashboard. You’ve probably seen this before, it likes to hide up in the top right hand corner of your page (toggled with the ‘help’ button).
WordPress带有内置的上下文帮助屏幕 ,可以从管理仪表板中的任何位置进行访问。 您可能以前已经看过,它喜欢隐藏在页面的右上角(用“帮助”按钮切换)。
![contextual help screen sample](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/05/1462818776contextual_help_screen_sample-1024x165.jpg)
Today I will cover how you can use the WordPress contextual help screens to document important parts of your theme/plugin, such as commonly used settings, shortcodes or other functionality.
今天,我将介绍如何使用WordPress上下文帮助屏幕记录主题/插件的重要部分,例如常用设置,短代码或其他功能。
与WP_Screen交互 (Interacting with WP_Screen)
To add your new help tab to your admin pages you will be interacting with the WP_Screen
class which is referenced when WordPress loads any admin area.
要将新的帮助选项卡添加到管理页面,您将与WordPress加载任何管理区域时引用的WP_Screen
类进行交互。
Instead of using the WP_Screen
class directly we’ll be using get_current_screen()
function to tell us what admin page we are currently viewing. This tells us some important info, such as if we’re on a post type or taxonomy and what’s our base file.
而不是直接使用WP_Screen
类,我们将使用get_current_screen()
函数来告诉我们当前正在查看的管理页面。 这告诉我们一些重要的信息,例如我们是否使用帖子类型或分类法,以及我们的基本文件是什么。
将WordPress上下文帮助屏幕添加到所有管理区域 (Adding WordPress Contextual Help Screens to All Admin Areas)
Adding our help screen is a straight forward process. Once we have access to the current screen object we call the add_help_tab
method of the WP_Screen
class.
添加我们的帮助屏幕是一个简单的过程。 一旦可以访问当前屏幕对象,就可以调用WP_Screen
类的add_help_tab
方法。
function add_context_menu_help(){
//get the current screen object
$current_screen = get_current_screen();
//content for help tab
$content = '<p>Im a help tab, woo!</p>';
//register our main help tab
$current_screen->add_help_tab( array(
'id' => 'sp_basic_help_tab',
'title' => __('Basic Help Tab'),
'content' => $content
)
);
//register our secondary help tab (with a callback instead of content)
$current_screen->add_help_tab( array(
'id' => 'sp_help_tab_callback',
'title' => __('Help Tab With Callback'),
'callback' => 'display_help_tab'
)
);
}
add_action('admin_head', 'add_context_menu_help');
//function used to display the second help tab
function display_help_tab(){
$content = '<p>This is text from our output function</p>';
echo $content;
}
There are two different ways of creating your output for the help tab, one is defining the content inline and passing it to the add_help_tab
method, the other is outlining the name of a function to be used for the output. Both will function perfectly.
有两种不同的方法来为帮助选项卡创建输出,一种是内联定义内容并将其传递给add_help_tab
方法,另一种是概述要用于输出的函数的名称。 两者都将正常运行。
You just need to supply your tabs id
, title
and the content and WordPress does the rest.
您只需要提供标签id
, title
和内容,WordPress就会完成其余工作。
有选择地添加上下文帮助 (Selectively Adding Contexual Help)
Often you don’t want to just dump your help tabs universally across every page of the back-end, you really only want to display these when they are relevant (for example when viewing an edit screen for a custom post type or a listing of taxonomy terms etc).
通常,您不希望将帮助标签普遍地转储到后端的每个页面上,您实际上只希望在相关标签时才显示它们(例如,在查看自定义帖子类型或列表列表的编辑屏幕时)分类术语等)。
This is where you take advantage of the current screen item collected with get_current_screen()
to detect where you are and to selectively target your help screens.
在这里,您可以利用通过get_current_screen()
收集的当前屏幕项目来检测自己所在的位置并有选择地定位帮助屏幕。
For our example, let’s say we only want to add our help tab when we are viewing items from a custom ‘book’ post type (when we are looking at our list of book items).
对于我们的示例,假设我们仅在查看自定义“书籍”帖子类型的项目时(当我们查看书籍项目列表时)添加帮助标签。
function add_help_screen_to_books(){
//get the current screen object
$current_screen = get_current_screen();
//show only on book listing page
if($current_screen->post_type == 'book' && $current_screen->base == 'edit'){
$content = '';
$content .= '<p>This is a help tab, you can add <strong>whatever</strong> it is you like here, such as instructions </p>';
$current_screen->add_help_tab( array(
'id' => 'sp_book_help_tab',
'title' => __('Book Help Tab'),
'content' => $content
)
);
}
}
add_action('admin_head', 'add_help_screen_to_books');
The above help tab will only be shown when viewing the admin listing for the book content type. We do this by looking at the current post_type
and the base
values (base tells us that we are on an admin edit screen).
仅当查看书籍内容类型的管理员列表时,才会显示以上帮助标签。 我们通过查看当前的post_type
和base
值(base告诉我们我们在管理员编辑屏幕上)来完成此操作。
![contextual help screen book](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/05/1462818851contextual_help_screen_book-1024x149.jpg)
奖励–更改上下文帮助侧栏 (Bonus – Changing the Contextual Help Sidebar)
Here’s another neat thing you can do with the contextual help menu. You can customize the sidebar to display extra information.
您可以使用上下文帮助菜单执行另一项整洁的操作。 您可以自定义边栏以显示其他信息。
One thing to look out for is the order in which you call set_help_sidebar
. This method must be called after help tabs have been added otherwise nothing will happen.
要注意的一件事是调用set_help_sidebar
的顺序。 添加帮助选项卡后必须调用此方法,否则将不会发生任何事情。
A simple way is to use the admin_head
hook with different priorities to ensure that by the time you call set_help_sidebar
your tabs have been registered.
一种简单的方法是使用具有不同优先级的admin_head
挂钩,以确保在您调用set_help_sidebar
您的选项卡已注册。
Let’s go ahead and add our own custom sidebar. Note here that it’s only displayed when we are editing single posts (of any type such as posts, pages or books).
让我们继续添加自己的自定义侧边栏。 请注意,此处仅在我们编辑单个帖子(任何类型的帖子,页面或书籍)时显示。
//adds a sidebar to the help context menu
function add_help_sidebar(){
//get the current screen object
$current_screen = get_current_screen();
//show only on listing / single post type screens
if($current_screen->base == 'edit' || $current_screen->base == 'post'){
$current_screen->add_help_tab( array(
'id' => 'sp_book_sample',
'title' => __('Book Help Tab'),
'content' => '<p>This is a simple help tab, hi </p>'
)
);
//add the help sidebar (outputs a simple list)
$current_screen->set_help_sidebar(
'<ul><li>Here is a list item</li><li>Here is a second item</li><li>Final item</li></ul>'
);
}
}
add_action('admin_head', 'add_help_sidebar');
See how the sidebar changes? You can put whatever it is you want there.
看到侧边栏如何变化? 您可以将所需的任何东西放在这里。
![contextual help screen sidebar](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/05/1462818913contextual_help_screen_sidebar.jpg)
笔记和有用的提示 (Notes and Helpful Hints)
Several online sources, including the WordPress codex suggest hooking your function onto one of the load-{name}
hooks. For example, hooking into when a post is loaded by using add_action('load-post.php')
. This might work fine, however, I’ve found attaching it to the admin_head
action works just as well and it loads for each page (letting you determine inside your function if you should add your help tabs or not).
包括WordPress Codex在内的一些在线资源建议将您的函数挂接到load-{name}
挂接之一上。 例如,使用add_action('load-post.php')
挂接何时加载帖子。 这可能很好用,但是,我发现将它附加到admin_head
操作上也能正常工作,并且会在每个页面上加载(让您确定是否要添加帮助选项卡的功能)。
You can use anything you want in these contextual help screens. You might want to outline the options for our shortcode (that will be displayed only on screens where the Visual Editor is displayed such as on posts). Or you might want to extensively use the contextual menus and add quick features like viewing your recent posts and their comments.
您可以在这些上下文帮助屏幕中使用任何所需的内容。 您可能要概述我们的短代码的选项(这些短代码将仅在显示Visual Editor的屏幕(例如帖子)上显示)。 或者,您可能想广泛使用上下文菜单并添加快速功能,例如查看您最近的帖子及其评论。
包装全部 (Wrapping It All Up)
WordPress contextual help menus are great. They give are easy to use and you can customize the way they provide information to your users. They have been around since back in WordPress 3.3 and are the perfect place to store helpful information at a glance.
WordPress上下文帮助菜单很棒。 它们提供的易于使用,您可以自定义它们向用户提供信息的方式。 自WordPress 3.3以来,它们就存在了,并且是一目了然地存储有用信息的理想场所。
For your next theme or plugin, you should definitely consider adding as much relevant information as possible into these sections. It’s much easier for users than hunting down information on the plugin repository/external third party websites.
对于下一个主题或插件,您绝对应该考虑在这些部分中添加尽可能多的相关信息。 对于用户而言,比在插件存储库/外部第三方网站上查找信息要容易得多。
翻译自: https://www.sitepoint.com/wordpress-contextual-help-screens/
wordpress启动