[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> post-title.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/post-title` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/post-title` block on the server.
  10   *
  11   * @param array    $attributes Block attributes.
  12   * @param string   $content    Block default content.
  13   * @param WP_Block $block      Block instance.
  14   *
  15   * @return string Returns the filtered post title for the current post wrapped inside "h1" tags.
  16   */
  17  function render_block_core_post_title( $attributes, $content, $block ) {
  18      if ( ! isset( $block->context['postId'] ) ) {
  19          return '';
  20      }
  21  
  22      $post_ID = $block->context['postId'];
  23      $title   = get_the_title();
  24  
  25      if ( ! $title ) {
  26          return '';
  27      }
  28  
  29      $tag_name         = 'h2';
  30      $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
  31  
  32      if ( isset( $attributes['level'] ) ) {
  33          $tag_name = 0 === $attributes['level'] ? 'p' : 'h' . $attributes['level'];
  34      }
  35  
  36      if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
  37          $title = sprintf( '<a href="%1$s" target="%2$s" rel="%3$s">%4$s</a>', get_the_permalink( $post_ID ), esc_attr( $attributes['linkTarget'] ), esc_attr( $attributes['rel'] ), $title );
  38      }
  39      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
  40  
  41      return sprintf(
  42          '<%1$s %2$s>%3$s</%1$s>',
  43          $tag_name,
  44          $wrapper_attributes,
  45          $title
  46      );
  47  }
  48  
  49  /**
  50   * Registers the `core/post-title` block on the server.
  51   */
  52  function register_block_core_post_title() {
  53      register_block_type_from_metadata(
  54          __DIR__ . '/post-title',
  55          array(
  56              'render_callback' => 'render_block_core_post_title',
  57          )
  58      );
  59  }
  60  add_action( 'init', 'register_block_core_post_title' );


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1