• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress a2z

WordPress a2z

WordPress Dynamic API Reference

  • Home
  • Plugins
  • Themes
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / twentyseventeen_get_svg() – Return SVG markup.

You appear to be a bot. Output may be restricted

Description

Return SVG markup.

Usage

$string = twentyseventeen_get_svg( $args );

Parameters

$args
( array ) optional – { Parameters needed to display an SVG.
$icon
( string ) optional – Required SVG icon filename.
$title
( string ) optional – Optional SVG title.
$desc
( string ) optional – Optional SVG description. }

Returns

string SVG markup.

Source

File name: twentyseventeen/inc/icon-functions.php
Lines:

1 to 80 of 80
function twentyseventeen_get_svg( $args = array() ) {
  // Make sure $args are an array.
  if ( empty( $args ) ) {
    return __( 'Please define default parameters in the form of an array.', 'twentyseventeen' );
  }

  // Define an icon.
  if ( false === array_key_exists( 'icon', $args ) ) {
    return __( 'Please define an SVG icon filename.', 'twentyseventeen' );
  }

  // Set defaults.
  $defaults = array(
    'icon'     => '',
    'title'    => '',
    'desc'     => '',
    'fallback' => false,
  );

  // Parse args.
  $args = wp_parse_args( $args, $defaults );

  // Set aria hidden.
  $aria_hidden = ' aria-hidden="true"';

  // Set ARIA.
  $aria_labelledby = '';

  /*
	 * Twenty Seventeen doesn't use the SVG title or description attributes; non-decorative icons are described with .screen-reader-text.
	 *
	 * However, child themes can use the title and description to add information to non-decorative SVG icons to improve accessibility.
	 *
	 * Example 1 with title: <?php echo twentyseventeen_get_svg( array( 'icon' => 'arrow-right', 'title' => __( 'This is the title', 'textdomain' ) ) ); ?>
	 *
	 * Example 2 with title and description: <?php echo twentyseventeen_get_svg( array( 'icon' => 'arrow-right', 'title' => __( 'This is the title', 'textdomain' ), 'desc' => __( 'This is the description', 'textdomain' ) ) ); ?>
	 *
	 * See https://www.paciellogroup.com/blog/2013/12/using-aria-enhance-svg-accessibility/.
	 */
  if ( $args['title'] ) {
    $aria_hidden     = '';
    $unique_id       = twentyseventeen_unique_id();
    $aria_labelledby = ' aria-labelledby="title-' . $unique_id . '"';

    if ( $args['desc'] ) {
      $aria_labelledby = ' aria-labelledby="title-' . $unique_id . ' desc-' . $unique_id . '"';
    }
  }

  // Begin SVG markup.
  $svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . $aria_labelledby . ' role="img">';

  // Display the title.
  if ( $args['title'] ) {
    $svg .= '<title id="title-' . $unique_id . '">' . esc_html( $args['title'] ) . '</title>';

    // Display the desc only if the title is already set.
    if ( $args['desc'] ) {
      $svg .= '<desc id="desc-' . $unique_id . '">' . esc_html( $args['desc'] ) . '</desc>';
    }
  }

  /*
	 * Display the icon.
	 *
	 * The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
	 *
	 * See https://core.trac.wordpress.org/ticket/38387.
	 */
  $svg .= ' <use href="#icon-' . esc_html( $args['icon'] ) . '" xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> ';

  // Add some markup to use as a fallback for browsers that do not support SVGs.
  if ( $args['fallback'] ) {
    $svg .= '<span class="svg-fallback icon-' . esc_attr( $args['icon'] ) . '"></span>';
  }

  $svg .= '</svg>';

  return $svg;
}
 

 View on GitHub View on Trac

Published: 8th April 2020 | Last updated: 21st August 2020

Primary Sidebar

Information

Function name: twentyseventeen_get_svg
Plugin ref: Twenty Seventeen
Version: 3.1
Sourcefile: inc/icon-functions.php
File ref: inc/icon-functions.php
Deprecated?: No
API Letters: G,S,T

Footer

WordPress a2z
WordPress a2z
WordPress Dynamic API Reference
WordPress 6.1.1. PHP: 8.0.28
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  wp-a2z.org
© Copyright WordPress a2z 2014-2023. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites