[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> search.php (source)

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


Generated: Sat Jul 11 01:00:03 2020 Cross-referenced by PHPXref 0.7.1