1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
<?php /** * Server-side rendering of the `core/search` block. * * @package WordPress */
/** * Dynamically renders the `core/search` block. * * @param array $attributes The block attributes. * * @return string The search block markup. */ function render_block_core_search( $attributes ) { static $instance_id = 0;
$input_id = 'wp-block-search__input-' . ++$instance_id;
if ( ! empty( $attributes['label'] ) ) { $label_markup = sprintf( '<label for="%s" class="wp-block-search__label">%s</label>', $input_id, $attributes['label'] ); }
$input_markup = sprintf( '<input type="search" id="%s" class="wp-block-search__input" name="s" value="%s" placeholder="%s" />', $input_id, esc_attr( get_search_query() ), esc_attr( $attributes['placeholder'] ) );
if ( ! empty( $attributes['buttonText'] ) ) { $button_markup = sprintf( '<button type="submit" class="wp-block-search__button">%s</button>', $attributes['buttonText'] ); }
$class = 'wp-block-search'; if ( isset( $attributes['className'] ) ) { $class .= ' ' . $attributes['className']; }
return sprintf( '<form class="%s" role="search" method="get" action="%s">%s</form>', $class, esc_url( home_url( '/' ) ), $label_markup . $input_markup . $button_markup ); }
/** * Registers the `core/search` block on the server. */ function register_block_core_search() { register_block_type( 'core/search', array( 'attributes' => array( 'label' => array( 'type' => 'string', 'default' => __( 'Search' ), ), 'placeholder' => array( 'type' => 'string', 'default' => '', ), 'buttonText' => array( 'type' => 'string', 'default' => __( 'Search' ), ), ),
'render_callback' => 'render_block_core_search', ) ); }
add_action( 'init', 'register_block_core_search' );
|