Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

原文の更新 #10

Merged
merged 4 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions plugins/administration-menus/sub-menus.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

To add a new Sub-menu to WordPress Administration, use the `add_submenu_page()` function.

add\_submenu\_page(
string $parent\_slug,
string $page\_title,
string $menu\_title,
```php
add_submenu_page(
string $parent_slug,
string $page_title,
string $menu_title,
string $capability,
string $menu\_slug,
string $menu_slug,
callable $function = ''
);
```

### Example

Expand All @@ -21,45 +23,47 @@ Lets say we want to add a Sub-menu “WPOrg Options” to the “Tools” Top-le

Note: We recommend wrapping your HTML using a `<div>` with a class of `wrap`.

function wporg\_options\_page\_html() {
```php
function wporg_options_page_html() {
// check user capabilities
if ( ! current\_user\_can( 'manage\_options' ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
<div class="wrap">
<h1><?php echo esc\_html( get\_admin\_page\_title() ); ?></h1>
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form action="options.php" method="post">
<?php
// output security fields for the registered setting "wporg\_options"
settings\_fields( 'wporg\_options' );
// output security fields for the registered setting "wporg_options"
settings_fields( 'wporg_options' );
// output setting sections and their fields
// (sections are registered for "wporg", each field is registered to a specific section)
do\_settings\_sections( 'wporg' );
do_settings_sections( 'wporg' );
// output save settings button
submit\_button( \_\_( 'Save Settings', 'textdomain' ) );
submit_button( __( 'Save Settings', 'textdomain' ) );
?>
</form>
</div>
<?php
}

[Expand full source code](#)[Collapse full source code](#)
```

**The second step** will be registering our WPOrg Options Sub-menu. The registration needs to occur during the `admin_menu` action hook.

function wporg\_options\_page()
```php
function wporg_options_page()
{
add\_submenu\_page(
add_submenu_page(
'tools.php',
'WPOrg Options',
'WPOrg Options',
'manage\_options',
'manage_options',
'wporg',
'wporg\_options\_page\_html'
'wporg_options_page_html'
);
}
add\_action('admin\_menu', 'wporg\_options\_page');
add_action('admin_menu', 'wporg_options_page');
```

For a list of parameters and what each do please see the [add\_submenu\_page()](https://developer.wordpress.org/reference/functions/add_submenu_page/) in the reference.

Expand Down Expand Up @@ -94,21 +98,21 @@ The process of handling form submissions within Sub-menus is exactly the same as

`add_submenu_page()` along with all functions for pre-defined sub-menus (`add_dashboard_page`, `add_posts_page`, etc.) will return a `$hookname`, which you can use as the first parameter of `add_action` in order to handle the submission of forms within custom pages:

function wporg\_options\_page() {
$hookname = add\_submenu\_page(
```php
function wporg_options_page() {
$hookname = add_submenu_page(
'tools.php',
'WPOrg Options',
'WPOrg Options',
'manage\_options',
'manage_options',
'wporg',
'wporg\_options\_page\_html'
'wporg_options_page_html'
);

add\_action( 'load-' . $hookname, 'wporg\_options\_page\_html\_submit' );
add_action( 'load-' . $hookname, 'wporg_options_page_html_submit' );
}

add\_action('admin\_menu', 'wporg\_options\_page');

[Expand full source code](#)[Collapse full source code](#)
add_action('admin_menu', 'wporg_options_page');
```

As always, do not forget to check whether the form is being submitted, do CSRF verification, [validation](https://developer.wordpress.org/plugins/security/data-validation/), and sanitization.
121 changes: 61 additions & 60 deletions plugins/administration-menus/top-level-menus.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

To add a new Top-level menu to WordPress Administration, use the [add\_menu\_page()](https://developer.wordpress.org/reference/functions/add_menu_page/) function.

<?php
add\_menu\_page(
string $page\_title,
string $menu\_title,
```php
add_menu_page(
string $page_title,
string $menu_title,
string $capability,
string $menu\_slug,
string $menu_slug,
callable $function = '',
string $icon\_url = '',
string $icon_url = '',
int $position = null
);
```

### Example

Expand All @@ -25,47 +26,43 @@ Note:

We recommend wrapping your HTML using a `<div>` with a class of `wrap`.

<?php
function wporg\_options\_page\_html() {
```php
function wporg_options_page_html() {
?>
<div class="wrap">
<h1><?php echo esc\_html( get\_admin\_page\_title() ); ?></h1>
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form action="options.php" method="post">
<?php
// output security fields for the registered setting "wporg\_options"
settings\_fields( 'wporg\_options' );
// output security fields for the registered setting "wporg_options"
settings_fields( 'wporg_options' );
// output setting sections and their fields
// (sections are registered for "wporg", each field is registered to a specific section)
do\_settings\_sections( 'wporg' );
do_settings_sections( 'wporg' );
// output save settings button
submit\_button( \_\_( 'Save Settings', 'textdomain' ) );
submit_button( __( 'Save Settings', 'textdomain' ) );
?>
</form>
</div>
<?php
}
?>

[Expand full source code](#)[Collapse full source code](#)
```

**The second step** will be registering our WPOrg menu. The registration needs to occur during the `admin_menu` action hook.

<?php
add\_action( 'admin\_menu', 'wporg\_options\_page' );
function wporg\_options\_page() {
add\_menu\_page(
```php
add_action( 'admin_menu', 'wporg_options_page' );
function wporg_options_page() {
add_menu_page(
'WPOrg',
'WPOrg Options',
'manage\_options',
'manage_options',
'wporg',
'wporg\_options\_page\_html',
plugin\_dir\_url(\_\_FILE\_\_) . 'images/icon\_wporg.png',
'wporg_options_page_html',
plugin_dir_url(__FILE__) . 'images/icon_wporg.png',
20
);
}
?>

[Expand full source code](#)[Collapse full source code](#)
```

For a list of parameters and what each do please see the [add\_menu\_page()](https://developer.wordpress.org/reference/functions/add_menu_page/) in the reference.

Expand All @@ -75,32 +72,30 @@ The best practice for portable code would be to create a Callback that requires/

For the sake of completeness and helping you understand legacy code, we will show another way: passing a `PHP file path` as the `$menu_slug` parameter with an `null` `$function` parameter.

<?php
add\_action( 'admin\_menu', 'wporg\_options\_page' );
function wporg\_options\_page() {
add\_menu\_page(
```php
add_action( 'admin_menu', 'wporg_options_page' );
function wporg_options_page() {
add_menu_page(
'WPOrg',
'WPOrg Options',
'manage\_options',
plugin\_dir\_path(\_\_FILE\_\_) . 'admin/view.php',
'manage_options',
plugin_dir_path(__FILE__) . 'admin/view.php',
null,
plugin\_dir\_url(\_\_FILE\_\_) . 'images/icon\_wporg.png',
plugin_dir_url(__FILE__) . 'images/icon_wporg.png',
20
);
}
?>

[Expand full source code](#)[Collapse full source code](#)
```

## Remove a Top-Level Menu

To remove a registered menu from WordPress Administration, use the [remove\_menu\_page()](https://developer.wordpress.org/reference/functions/remove_menu_page/) function.

<?php
remove\_menu\_page(
string $menu\_slug
```php
remove_menu_page(
string $menu_slug
);
?>
```

Warning:
Removing menus won’t prevent users accessing them directly.
Expand All @@ -110,21 +105,22 @@ This should never be used as a way to restrict [user capabilities](https://devel

Lets say we want to remove the “Tools” menu from.

<?php
add\_action( 'admin\_menu', 'wporg\_remove\_options\_page', 99 );
function wporg\_remove\_options\_page() {
remove\_menu\_page( 'tools.php' );
```php
add_action( 'admin_menu', 'wporg_remove_options_page', 99 );
function wporg_remove_options_page() {
remove_menu_page( 'tools.php' );
}
?>
```

Make sure that the menu have been registered with the `admin_menu` hook before attempting to remove, specify a higher priority number for [add\_action()](https://developer.wordpress.org/reference/functions/add_action/).
Make sure that the menu have been registered with the `admin_menu` hook before attempting to remove, specify a higher priority number for [add\_action()](https://developer.wordpress.org/reference/functions/add_action/) .

## Submitting forms

To process the submissions of forms on options pages, you will need two things:

1. Use the URL of the page as the `action` attribute of the form.
2. Add a hook with the slug, returned by `add_menu_page`.

3. Add a hook with the slug, returned by `add_menu_page`.

Note:
You only need to follow those steps if you are manually creating forms in the back-end. The [Settings API](https://developer.wordpress.org/plugins/settings/) is the recommended way to do this.
Expand All @@ -133,7 +129,9 @@ You only need to follow those steps if you are manually creating forms in the ba

Use the `$menu_slug` parameter of the options page as the first parameter of `[menu_page_url()](https://developer.wordpress.org/reference/functions/menu_page_url/)`. By the function will automatically escape URL and echo it by default, so you can directly use it within the `<form>` tag:

<form action="<?php menu\_page\_url( 'wporg' ) ?>" method="post">
```php
<form action="<?php menu_page_url( 'wporg' ) ?>" method="post">
```

### Processing the form

Expand All @@ -146,26 +144,29 @@ Note:

With the return parameter and action in mind, the example from above would like this:

add\_action( 'admin\_menu', 'wporg\_options\_page' );
function wporg\_options\_page() {
$hookname = add\_menu\_page(
```php
add_action( 'admin_menu', 'wporg_options_page' );
function wporg_options_page() {
$hookname = add_menu_page(
'WPOrg',
'WPOrg Options',
'manage\_options',
'manage_options',
'wporg',
'wporg\_options\_page\_html',
plugin\_dir\_url(\_\_FILE\_\_) . 'images/icon\_wporg.png',
'wporg_options_page_html',
plugin_dir_url(__FILE__) . 'images/icon_wporg.png',
20
);

add\_action( 'load-' . $hookname, 'wporg\_options\_page\_submit' );
add_action( 'load-' . $hookname, 'wporg_options_page_submit' );
}

[Expand full source code](#)[Collapse full source code](#)
```

You can program `wporg_options_page_submit` according to your needs, but keep in mind that you must manually perform all necessary checks, including:

1. Whether the form is being submitted (`'POST' === $_SERVER['REQUEST_METHOD']`).
2. [CSRF verification](https://developer.wordpress.org/plugins/security/nonces/)
3. Validation
4. Sanitization

3. [CSRF verification](https://developer.wordpress.org/plugins/security/nonces/)

5. Validation

7. Sanitization
Loading