Skip to content
WordPress functions for developers to use instead of wp_get_attachment functions to inline SVGs.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md
attachment-functions.php

README.md

WordPress Attachment Functions

WordPress functions for developers to use instead of wp_get_attachment functions to inline SVGs.

What It Does

Using the new functions will run each image through a check to see if it's an SVG. It'll then output the SVG using the alt tag from the media library for the uploaded image, add role="img", and also sizes it. If it's not an SVG it runs wp_get_attachnment_image or equivalent WP function.

Instructions

You can either include this code directly in your functions.php file in your theme or add to a directory, such as library/attachment-functions.php then include it in your functions.php code:

/** Attachment Functions */ require_once( 'library/attachment-functions.php' );

WordPress Functions Reference

https://developer.wordpress.org/reference/functions/wp_get_attachment_image/ https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/ https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

The Functions

  • get_the_image($attachment_id,$size = 'thumbnail', $icon = false, $attr = '')

    • Returns the image or inline SVG code
  • the_image($attachment_id,$size = 'thumbnail', $icon = false, $attr = '')

    • Prints the image or inline SVG code
  • get_the_thumbnail_bg($post = null, $size = 'post-thumbnail')

    • Returns the thumbnail image or inline SVG code
  • the_thumbnail_bg($size = 'post-thumbnail')

    • Prints the thumbnail image or inline SVG code
  • get_the_image_bg($attachment_id,$size = 'thumbnail', $icon = false, $attr = '')

    • If an SVG returns the image as inline SVG CSS code otherwise it returns the image as a URL
  • the_image_bg($attachment_id,$size = 'thumbnail', $icon = false, $attr = '')

    • Prints the background image SVG inline or image url

A number of other functions that are used internally

  • is_svg($attachment_id)

    • Returns True/False
  • get_svg($attachment_id,$size = 'thumbnail', $icon = false)

    • Returns the SVG code
  • the_svg($attachment_id)

    • Prints the SVG code
  • generate_ratio($new_size = array(), $old_size = array())

    • Takes a value and returns an array of the width, height (Mainly used to assist get_the_image() when generating the SVG size)
You can’t perform that action at this time.