render_block_core_social_link()
Renders the core/social-link block on server.
No Hooks.
Returns
String. Rendered HTML of the referenced block.
Usage
render_block_core_social_link( $attributes, $content, $block );
- $attributes(Array) (required)
- The block attributes.
- $content(String) (required)
- InnerBlocks content of the Block.
- $block(WP_Block) (required)
- Block object.
Changelog
| Since 5.4.0 | Introduced. |
render_block_core_social_link() render block core social link code WP 6.9.1
function render_block_core_social_link( $attributes, $content, $block ) {
$open_in_new_tab = isset( $block->context['openInNewTab'] ) ? $block->context['openInNewTab'] : false;
$text = ! empty( $attributes['label'] ) ? trim( $attributes['label'] ) : '';
$service = isset( $attributes['service'] ) ? $attributes['service'] : 'Icon';
$url = isset( $attributes['url'] ) ? $attributes['url'] : false;
$text = $text ? $text : block_core_social_link_get_name( $service );
$rel = isset( $attributes['rel'] ) ? $attributes['rel'] : '';
$show_labels = array_key_exists( 'showLabels', $block->context ) ? $block->context['showLabels'] : false;
// Don't render a link if there is no URL set.
if ( ! $url ) {
return '';
}
/**
* Prepend emails with `mailto:` if not set.
* The `is_email` returns false for emails with schema.
*/
if ( is_email( $url ) ) {
$url = 'mailto:' . antispambot( $url );
}
/**
* Prepend URL with https:// if it doesn't appear to contain a scheme
* and it's not a relative link or a fragment.
*/
if ( ! parse_url( $url, PHP_URL_SCHEME ) && ! str_starts_with( $url, '//' ) && ! str_starts_with( $url, '#' ) ) {
$url = 'https://' . $url;
}
$icon = block_core_social_link_get_icon( $service );
$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => 'wp-social-link wp-social-link-' . $service . block_core_social_link_get_color_classes( $block->context ),
'style' => block_core_social_link_get_color_styles( $block->context ),
)
);
$link = '<li ' . $wrapper_attributes . '>';
$link .= '<a href="' . esc_url( $url ) . '" class="wp-block-social-link-anchor">';
$link .= $icon;
$link .= '<span class="wp-block-social-link-label' . ( $show_labels ? '' : ' screen-reader-text' ) . '">' . esc_html( $text ) . '</span>';
$link .= '</a></li>';
$processor = new WP_HTML_Tag_Processor( $link );
$processor->next_tag( 'a' );
if ( $open_in_new_tab ) {
$processor->set_attribute( 'rel', trim( $rel . ' noopener nofollow' ) );
$processor->set_attribute( 'target', '_blank' );
} elseif ( '' !== $rel ) {
$processor->set_attribute( 'rel', trim( $rel ) );
}
return $processor->get_updated_html();
}