Make WordPress Core

Opened 10 months ago

Closed 8 days ago

#62086 closed enhancement (fixed)

Add Filter for Custom Form Attributes in New Site Creation (/wp-admin/network/site-new.php)

Reported by: sakibmoon's profile sakibmoon Owned by: realloc's profile realloc
Milestone: 6.9 Priority: normal
Severity: normal Version: 3.0
Component: Networks and Sites Keywords: has-patch has-test-info
Focuses: administration, multisite Cc:

Description

When adding a new site in a WordPress Multisite Network (via /wp-admin/network/site-new.php), the form does not provide a filter to allow the addition of custom attributes, such as enctype="multipart/form-data", which is necessary for handling file uploads during new site creation.

This issue arises when developers need to extend the new site creation form to allow file uploads (e.g., custom site logos or configuration files). However, since there is no hook or filter to modify the form attributes, it is currently impossible to add the enctype="multipart/form-data" attribute to the form without overriding core files.

Change History (14)

This ticket was mentioned in PR #7402 on WordPress/wordpress-develop by @sakibmoon.


10 months ago
#1

This change introduces the network_site_new_form_attributes filter to the site creation form (/wp-admin/network/site-new.php) in WordPress Multisite.

The filter allows developers to modify form attributes such as enctype="multipart/form-data", enabling file uploads or other custom behaviors when creating a new site.

This enhancement provides more flexibility for developers extending the multisite functionality without requiring modifications to core files.

Trac ticket: https://core.trac.wordpress.org/ticket/62086

This ticket was mentioned in PR #7403 on WordPress/wordpress-develop by @sakibmoon.


10 months ago
#2

This change introduces the network_site_new_form_attributes filter to the site creation form (/wp-admin/network/site-new.php) in WordPress Multisite.

The filter allows developers to modify form attributes such as enctype="multipart/form-data", enabling file uploads or other custom behaviors when creating a new site.

This enhancement provides more flexibility for developers extending the multisite functionality without requiring modifications to core files.

Trac ticket: https://core.trac.wordpress.org/ticket/62086

#3 @sabernhardt
9 months ago

  • Component changed from General to Networks and Sites
  • Version 6.6.2 deleted

@realloc commented on PR #7403:


4 months ago
#4

@sakibmoon There might be some room to improve this PR.

  1. If the filter returns something like autocomplete="off", it will be appended directly after the class attribute, resulting in:
<form method="post" action="site-new.php" class="wp-validate"autocomplete="off">

Which is invalid HTML due to the missing space. You could add a check to prepend a space if needed.

  1. It might also be worth considering some sanitization or validation of the filtered output — perhaps passing it through something like wp_kses_attr_check() or at least documenting that the output should be properly escaped HTML attributes.

Thanks for your work on this!

This ticket was mentioned in Slack in #core-multisite by realloc. View the logs.


3 months ago

#6 @spacedmonkey
3 months ago

  • Milestone changed from Awaiting Review to 6.9
  • Version set to 3.0

#7 follow-up: @johnjamesjacoby
3 months ago

What if we just add enctype="multipart/form-data" to the form? 🧐

What I don't like about putting a filter in there is that it would be unique. No other form attributes have filters like that, and introducing a new pattern (that could be used on many forms in WordPress Admin) would require a bunch of scrutiny.

What I like about adding it directly, is it:

  • doesn't change any behavior if there are no attachments
  • allows for plugins to do what is being proposed
  • adds a commonly used attribute that nav-menus already appears to have without handling attachments

What I don't like about adding it directly, is:

  • it's potentially confusing that the core form doesn't actually need it (maybe we add an inline doc?)
Last edited 3 months ago by johnjamesjacoby (previous) (diff)

#8 in reply to: ↑ 7 @realloc
2 months ago

Replying to johnjamesjacoby:

What if we just add enctype="multipart/form-data" to the form? 🧐

IMHO, that would be a much better solution than relying on the filter.

#9 @audrasjb
5 weeks ago

  • Keywords changes-requested added

This ticket was mentioned in PR #9084 on WordPress/wordpress-develop by @realloc.


12 days ago
#10

This PR adds the enctype="multipart/form-data" attribute to the site creation form on /wp-admin/network/site-new.php.

Related ticket: https://core.trac.wordpress.org/ticket/62086

#11 @realloc
12 days ago

  • Keywords needs-testing added; changes-requested removed
  • Owner set to realloc
  • Status changed from new to assigned

#12 @SirLouen
12 days ago

  • Keywords has-test-info added; needs-testing removed

Test Report

Description

✅ This report validates that the indicated patch works as expected.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/9084.diff

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.28
  • Server: nginx/1.27.5
  • Database: mysqli (Server: 8.4.5 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 137.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-One 2.5
  • MU Plugins: None activated
  • Plugins:
    • Network Site Logo 1.0.0
    • Test Reports 1.2.0

Patch Testing Instructions

  1. Create a Multisite
  2. Add the code provided in supplemental artifacts for testing. Basically, I'm adding as OP suggested, a field to add a logo. You can modify the code and add whatever field you like for the test
  3. Go to /wp-admin/network/site-new.php
  4. Add the file or whatever content you have added in the form
  5. Check in the DB, in the new options table for the newly created multisite, example wp_2_options the site_logo_url field has been introduced with the URL of the logo.

Actual Results

  1. ✅ Issue resolved with patch.

Supplemental Artifacts

Testing Code:

function add_ms_logo_field() {
	?>
	<tr class="form-field">
		<th scope="row"><label for="site-logo"><?php echo 'Site Logo'; ?></label></th>
		<td><input name="site_logo" type="file" id="site-logo" /></td>
	</tr>
	<?php
}
add_action( 'network_site_new_form', 'add_ms_logo_field' );

function save_ms_logo( $new_site ) {
	require_once ABSPATH . 'wp-admin/includes/file.php';
	switch_to_blog( $new_site->blog_id );
	$upload_overrides = array( 'test_form' => false );
	$logofile         = wp_handle_upload( $_FILES['site_logo'], $upload_overrides );
        update_option( 'site_logo_url', $logofile['url'] );
	restore_current_blog(); 
}
add_action( 'wp_initialize_site', 'save_ms_logo', 10, 1 ); 
Last edited 12 days ago by SirLouen (previous) (diff)

#13 @realloc
11 days ago

Thanks @SirLouen!

@johnjamesjacoby, with the latest changes, do you think this is ready to move forward?

#14 @johnjamesjacoby
8 days ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 60398:

Networks and Sites: Add support for file/upload type inputs on the "New Site" screen.

This change allows plugins to add files when creating sites via the network-admin "New Site" form.

  • Adds enctype="multipart/form-data" to the form in wp-admin/network/site-new.php.
  • Supports input type="file" fields in this form (via the network_site_new_form action).

Props johnjamesjacoby, sakibmoon, sirlouen, realloc.

Fixes #62086.

Note: See TracTickets for help on using tickets.