Skip to main content

Available PHP Functions

Not a hook or a filter? Not a template tag used to output something directly in a template or template part? Then you've come to the right place! Our /inc/functions/ directory provides functions that act independently of the theme templates.

generate_block_id()

If you're building blocks with ACF, this function will help you replicate the ACF ID that ACF creates. The ACF ID is only added to block data on the page that loads said block but what if you need to access data from a block on another page/post? That's where this function shines. pass in the block attributes and the block's context and it will return a string that matches the ACF ID.

Usage: $block_id = generate_block_id( $attrs, $context );

Output: asdbjas3432bjdk23234323r23ddfwasc

get_acf_fields()

This function returns an array of ACF fields. Within the function, it checks to see if get_field() exists, and if so, it'll get all the field data that you pass into the function.

Usage:


$block_data = get_acf_fields(['hero_title', 'hero_image', 'hero_teaser'], $post_id );

Output:

array(
'hero_title' => 'Hero Title',
'hero_image' => 'https://example.com/image.jpg',
'hero_teaser' => 'Hero Teaser',
)

get_asset_relative_url()

Get the relative URL of an asset from it's full URL. This function is useful for getting the relative URL of an asset that is located in the theme's /build/ directory.

Usage $path = get_asset_relative_url( $asset_url );

Output /path/to/asset

get_attributes()

Get tag attributes

Usage:

$array_of_attributes = array(
'id' => 'about-section',
'class' => 'class-one class-two', // accepts array or string array('class-one', 'class-two')
'data-number' => 1,
);

$attrs = get_attributes( $array_of_attributes );

Output: String of attributes ready to be used in a tag

get_block_classes()

Returns an updated array of classes. This function is useful for adding classes to blocks that are built with ACF.

Usage: $block_classes - get_block_classes( $block )

Output: An array of classes.

get_block_parent()

Get the momma or poppa block. This function is useful for getting the parent block of a nested block. You need to pass in the post ID, the block's context. This function might not be neccessary in the future when using block.json context to pass data down to innerBlocks.

Usage: $parent_block = get_block_parent( $post_id, $block_context, $child_block_id )

Output String of parent block name

jaws_is_block_style()

Returns a boolean if the style is in the block classes. This function looks for a string in the array of classes generated in a gutenberg block. Pass in the style you're looking for and the array of classes.

Usage:


if ( get_block_style( 'rounded', $classes ) ) {
// do something
}

jaws_is_block_variation()

Returns a boolean if the variation is in the block classes. This function looks for a string in the array of classes generated in a gutenberg block. Pass in the style you're looking for and the array of classes.

Usage:


if ( get_block_variation( 'person-card', $classes ) ) {
// do something
}

jaws_maybe_get

Get the value if the key in the array exists.

Usage: $title = maybe_get( $array, 'title' );

Output: Title || null

get_field_from_block()

Get field data from a block used on another post/page.

Usage: $field_data = get_field_from_block( $selector, $post_id, $block_name );

Output: Field Data

get_formatted_args()

Merges the element defaults with the passed args. This is utilized in ACF blocks to merge the block's default args with the args passed in from the block's data.

Output: Array of merged args

get_formatted_atts()

Returns a string of formatted element attributes to be used in a tag. This is utilized in the ACF blocks.

get_sprite_svg()

Outputted by the print_svg function (inside inc/template-tags/print-svg.php) and accepts the same args that function.

Usage:

get_svg( array(
'icon' => 'facebook-square',
'title' => __( 'Facebook square icon', '_s' ),
'desc' => __( 'A rounded square version of the Facebook logo', '_s' ),
'color' => '#ffff00',
'stroke-width' => '0.5',
'height' => '48',
'width' => '48',
)
);

Output Stringified svg

<svg
height="48"
width="48"
color="#ffff00"
stroke-width="0.5"
class="icon facebook-square"
aria-labelledby="title-facebook-square-icon-67297 desc-facebook-square-icon-67297"
role="img"
>
<title id="title-facebook-square-icon-67297">Facebook square icon</title>
<desc id="desc-facebook-square-icon-67297">
A rounded square version of the Facebook logo
</desc>
<use xlink:href="#facebook-square"></use>
</svg>

get_trimmed_excerpt()

Limit the excerpt length

Usage:

get_trimmed_excerpt( array(
'length' => 20,
'more' => '...',
'post' => '',
)
);

Output: Returns the trimmed excerpt

get_trimmed_title()

Limit the title length

Usage:

get_trimmed_title( array(
'length' => 12,
'more' => '...',
)
);

Output: Returns the trimmed title

get_unique_id()

Generate a unique ID when one is needed but not meant to be used as selector.

Usage: $unique_id = get_unique_id( 'card--');

Output card--asdbjas3432bjdk23234323r23ddfwasc