icon-functions.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php declare( strict_types = 1 ); ?>
  2. <?php
  3. /**
  4. * SVG icons related functions
  5. *
  6. * @package WordPress
  7. * @subpackage Varia
  8. * @since 1.0.0
  9. */
  10. /**
  11. * Gets the SVG code for a given icon.
  12. */
  13. function varia_get_icon_svg( $icon, $size = 24 ) {
  14. return Varia_SVG_Icons::get_svg( 'ui', $icon, $size );
  15. }
  16. /**
  17. * Gets the SVG code for a given social icon.
  18. */
  19. function varia_get_social_icon_svg( $icon, $size = 24 ) {
  20. return Varia_SVG_Icons::get_svg( 'social', $icon, $size );
  21. }
  22. /**
  23. * Detects the social network from a URL and returns the SVG code for its icon.
  24. */
  25. function varia_get_social_link_svg( $uri, $size = 24 ) {
  26. return Varia_SVG_Icons::get_social_link_svg( $uri, $size );
  27. }
  28. /**
  29. * Display SVG icons in social links menu.
  30. *
  31. * @param string $item_output The menu item output.
  32. * @param WP_Post $item Menu item object.
  33. * @param int $depth Depth of the menu.
  34. * @param array $args wp_nav_menu() arguments.
  35. * @return string $item_output The menu item output with social icon.
  36. */
  37. function varia_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
  38. // Change SVG icon inside social links menu if there is supported URL.
  39. if ( 'social' === $args->theme_location ) {
  40. $svg = varia_get_social_link_svg( $item->url, 26 );
  41. if ( empty( $svg ) ) {
  42. $svg = varia_get_icon_svg( 'link' );
  43. }
  44. $item_output = str_replace( $args->link_after, '</span>' . $svg, $item_output );
  45. }
  46. return $item_output;
  47. }
  48. add_filter( 'walker_nav_menu_start_el', 'varia_nav_menu_social_icons', 10, 4 );