wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_get_admin_notice › WordPress Function

Since6.4.0
Deprecatedn/a
wp_get_admin_notice ( $message, $args = array() )
Parameters: (2)
  • (string) $message The message.
    Required: Yes
  • (array) $args { Optional. An array of arguments for the admin notice. Default empty array. @type string $type Optional. The type of admin notice. For example, 'error', 'success', 'warning', 'info'. Default empty string. @type bool $dismissible Optional. Whether the admin notice is dismissible. Default false. @type string $id Optional. The value of the admin notice's ID attribute. Default empty string. @type string[] $additional_classes Optional. A string array of class names. Default empty array. @type string[] $attributes Optional. Additional attributes for the notice div. Default empty array. @type bool $paragraph_wrap Optional. Whether to wrap the message in paragraph tags. Default true. }
    Required: No
    Default: array()
Returns:
  • (string) The markup for an admin notice.
Defined at:
Codex:

Creates and returns the markup for an admin notice.



Source

function wp_get_admin_notice( $message, $args = array() ) {
	$defaults = array(
		'type'               => '',
		'dismissible'        => false,
		'id'                 => '',
		'additional_classes' => array(),
		'attributes'         => array(),
		'paragraph_wrap'     => true,
	);

	$args = wp_parse_args( $args, $defaults );

	/**
	 * Filters the arguments for an admin notice.
	 *
	 * @since 6.4.0
	 *
	 * @param array  $args    The arguments for the admin notice.
	 * @param string $message The message for the admin notice.
	 */
	$args = apply_filters( 'wp_admin_notice_args', $args, $message );

	$wrap_with_p  = false !== $args['paragraph_wrap'];
	$wrap_opener  = $wrap_with_p ? '<p>' : '';
	$wrap_closer  = $wrap_with_p ? '</p>' : '';
	$html_builder = new WP_HTML_Tag_Processor( "<div class=\"notice\">{$wrap_opener}" );
	$html_builder->next_token();

	if ( is_string( $args['id'] ) ) {
		$trimmed_id = trim( $args['id'] );

		if ( '' !== $trimmed_id ) {
			$html_builder->set_attribute( 'id', $trimmed_id );
		}
	}

	if ( is_string( $args['type'] ) ) {
		$type = trim( $args['type'] );

		if ( strlen( $type ) !== strcspn( $type, " \f\t\r\n" ) ) {
			_doing_it_wrong(
				__FUNCTION__,
				sprintf(
					/* translators: %s: The "type" key. */
					__( 'The %s key must be a string without spaces.' ),
					'<code>type</code>'
				),
				'6.4.0'
			);
		}

		if ( '' !== $type ) {
			$html_builder->add_class( "notice-{$type}" );
		}
	}

	if ( true === $args['dismissible'] ) {
		$html_builder->add_class( 'is-dismissible' );
	}

	if ( is_array( $args['additional_classes'] ) && ! empty( $args['additional_classes'] ) ) {
		foreach ( $args['additional_classes'] as $class_name ) {
			$html_builder->add_class( $class_name );
		}
	}

	if ( is_array( $args['attributes'] ) && ! empty( $args['attributes'] ) ) {
		foreach ( $args['attributes'] as $name => $value ) {
			if ( is_int( $name ) ) {
				/*
				 * Boolean attributes may have been appended as numeric list items,
				 * for example, with `$args['attributes'][] = 'disabled'`. They should
				 * be recorded with the value serving as their name.
				 */
				$html_builder->set_attribute( $value, true );
			} elseif ( true === $value ) {
				$html_builder->set_attribute( $name, true );
			} elseif ( false !== $value ) {
				$html_builder->set_attribute( $name, trim( (string) $value ) );
			}
		}
	}

	$markup  = $html_builder->get_updated_html();
	$markup .= $message;
	$markup .= "{$wrap_closer}</div>";

	/**
	 * Filters the markup for an admin notice.
	 *
	 * @since 6.4.0
	 *
	 * @param string $markup  The HTML markup for the admin notice.
	 * @param string $message The message for the admin notice.
	 * @param array  $args    The arguments for the admin notice.
	 */
	return apply_filters( 'wp_admin_notice_markup', $markup, $message, $args );
}