Skip to content

Instantly share code, notes, and snippets.

@codersantosh
Created September 20, 2024 18:48
Show Gist options
  • Select an option

  • Save codersantosh/3740ab3047906967d513d576cf81f309 to your computer and use it in GitHub Desktop.

Select an option

Save codersantosh/3740ab3047906967d513d576cf81f309 to your computer and use it in GitHub Desktop.
Archive title block bindings
/**
* Register block binding sources.
*/
if ( ! function_exists( 'prefix_register_block_bindings' ) ) :
/**
* Register the archive title binding source.
*
* @since Project Name 1.0.0
* @return void
*/
function prefix_register_block_bindings() {
register_block_bindings_source(
'prefix/archive-title',
array(
'label' => _x( 'Archive title', 'Label for the archive title placeholder in the editor', 'text-domain' ),
'get_value_callback' => 'prefix_archive_title_binding',
)
);
}
endif;
/**
* Register block binding callback function for the archive title.
*/
if ( ! function_exists( 'prefix_archive_title_binding' ) ) :
/**
* Callback function for the archive title block binding source.
*
* @since Project Name 1.0
* @param array $source_args Array containing source arguments used to look up the override value.
* @param WP_Block $block_instance The block instance.
* @return string archive title text.
*/
function prefix_archive_title_binding( array $source_args, $block_instance ) {
$binding_data = null;
if ( is_archive() ) {
$binding_data = get_the_archive_title();
} elseif ( is_search() ) {
$binding_data = sprintf(
/* translators: %s is the search term. */
__( 'Search results for: "%s"', 'text-domain' ),
get_search_query()
);
} elseif ( is_home() ) {
$binding_data = __( 'Blog', 'text-domain' );
}
return apply_filters( 'prefix_archive_title_binding', $binding_data, $source_args, $block_instance );
}
endif;
add_action( 'init', 'prefix_register_block_bindings' );
<?php
/**
* Title: Archive Title
* Slug: prefix/archive-title
* Categories: header
* Description: Displays the title for archive pages, including search, blog, and other archive pages.
*
* @package Project_Name
* @subpackage Project_Name/patterns
* @since 1.0.0
*/
?>
<!-- wp:heading {"metadata":{"bindings":{"content":{"source":"prefix/archive-title"}}},"level":1} -->
<h1 class="wp-block-heading"></h1>
<!-- /wp:heading -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment