Skip to content

Instantly share code, notes, and snippets.

@bacoords
Last active November 17, 2023 15:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bacoords/8c4379a0c535ecf12f2cabe41fa41d99 to your computer and use it in GitHub Desktop.
Save bacoords/8c4379a0c535ecf12f2cabe41fa41d99 to your computer and use it in GitHub Desktop.
Filter the social icons block.
<?php
/**
* Filter the social icons block.
*
* @param string $block_content The block content.
* @param array $block The block.
*
* @return string
*/
function filter_social_icons_block( $block_content, $block ) {
// Grab our options (note that this could also come from an ACF options field).
$settings = get_option( 'my_options' );
if ( 'twitter' === $block['attrs']['service'] && '' !== $settings['twitter'] ) {
$new_content = new WP_HTML_Tag_Processor( $block_content );
if ( $new_content->next_tag( 'a' ) ) {
$new_content->set_attribute( 'href', $settings['twitter'] );
}
$block_content = $new_content->get_updated_html();
}
if ( 'facebook' === $block['attrs']['service'] && '' !== $settings['facebook'] ) {
$new_content = new WP_HTML_Tag_Processor( $block_content );
if ( $new_content->next_tag( 'a' ) ) {
$new_content->set_attribute( 'href', $settings['facebook'] );
}
$block_content = $new_content->get_updated_html();
}
if ( 'instagram' === $block['attrs']['service'] && '' !== $settings['instagram'] ) {
$new_content = new WP_HTML_Tag_Processor( $block_content );
if ( $new_content->next_tag( 'a' ) ) {
$new_content->set_attribute( 'href', $settings['instagram'] );
}
$block_content = $new_content->get_updated_html();
}
return $block_content;
}
add_filter( 'render_block_core/social-link', 'filter_social_icons_block', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment