Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Gists for an upcoming blog post titled "Add A Custom Wordpress Admin Contextual Help Menu To Your Plugin Or Theme"
<?
add_action('init', 'cjr_register_books_cpt');
add_action('admin_menu', 'cjr_add_book_settings');
/**
* Registers the Books CPT to WordPress. Must be called in 'init' action hook.
*/
function cjr_register_books_cpt()
{
$args = array(
'public' => true,
'label' => 'Books'
);
register_post_type('book', $args);
add_action('load-post-new.php', 'cjr_add_help_tabs');
add_action('load-post.php', 'cjr_add_help_tabs');
}
/**
* Adds an admin page under Settings
*/
function cjr_add_book_settings()
{
$page = add_options_page(
'Book Settings', // Page Title
'Books', // Menu Title
'manage_options', // Capability
'cjr-books', // Slug
'cjr_render_book_settings' // Callback
);
add_action("load-$page", 'cjr_add_help_tabs');
}
/**
* Renders the content of our custom admin page
*/
function cjr_render_book_settings()
{
/***/
}
/**
* Adds the help tabs to the current page
*/
function cjr_add_help_tabs()
{
$screen = get_current_screen();
if ('post' === $screen->base && 'book' !== $screen->post_type) return;
$tabs = array(
array(
'title' => 'All About Books',
'id' => 'cjr-books-about',
'content' => '<p>Books are pretty awesome...</p>'
),
array(
'title' => 'More About Books',
'id' => 'cjr-books-more',
'callback' => 'cjr_get_more_tab'
)
);
foreach($tabs as $tab) {
$screen->add_help_tab($tab);
}
$screen->set_help_sidebar('<a href="#">More info!</a>');
}
/**
* Outputs the content for the 'More About Books' Help Tab
*/
function cjr_get_more_tab()
{ ?>
<p>Many long paragraphs about books...</p>
<? }
<?
add_action('init', 'cjr_register_books_cpt');
add_action('admin_menu', 'cjr_add_book_settings');
/**
* Registers the Books CPT to WordPress. Must be called in 'init' action hook.
*/
function cjr_register_books_cpt()
{
$args = array(
'public' => true,
'label' => 'Books'
);
register_post_type('book', $args);
}
/**
* Adds an admin page under Settings. Must be called in 'admin_menu' action hook.
*/
function cjr_add_book_settings()
{
$page = add_options_page(
'Book Settings', // Page Title
'Books', // Menu Title
'manage_options', // Capability
'cjr-books', // Slug
'cjr_render_book_settings' // Callback
);
}
/**
* Renders the content of our custom admin page
*/
function cjr_render_book_settings()
{
/***/
}
<?
function cjr_register_books_cpt()
{
// previous code
add_action('load-post-new.php', 'cjr_add_help_tabs');
add_action('load-post.php', 'cjr_add_help_tabs');
}
function cjr_add_book_settings()
{
// previous code
add_action("load-$page", 'cjr_add_help_tabs');
}
/**
* Adds the help tabs to the current page
*/
function cjr_add_help_tabs()
{
$screen = get_current_screen();
$tabs = array(
array(
'title' => 'All About Books',
'id' => 'cjr-books-about',
'content' => '<p>Books are pretty awesome...</p>'
),
array(
'title' => 'More About Books',
'id' => 'cjr-books-more',
'callback' => 'cjr_get_more_tab'
)
);
foreach($tabs as $tab) {
$screen->add_help_tab($tab);
}
$screen->set_help_sidebar('<a href="#">More info!</a>');
}
/**
* Outputs the content for the 'More About Books' Help Tab
*/
function cjr_get_more_tab()
{ ?>
<p>Many long paragraphs about books...</p>
<? }
<?
function cjr_add_help_tabs()
{
$screen = get_current_screen();
if ('post' === $screen->base && 'book' !== $screen->post_type) return;
// previous code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment