PHP

Import SVG inline

A nice easy way to grab icons anywhere in your site.

<?php include("icons/my-icon.svg"); ?>

// getting an icon from a parent directory
<?php include __DIR__ . '/../icons/facebook.svg'; ?>
Updated: 6 February 2022

Enqueue a script

A method to functionally add scripts to your WordPress projects footer.

wp_enqueue_script(
    'lazysizes',
    get_template_directory_uri() . '/js/lazysizes.min.js'
);
Updated: 6 February 2022

Get template part

Reuse some templated PHP content you have created

<?php get_template_part('template_parts/griditem'); ?>
Updated: 6 February 2022

Use variables in template parts

Sometimes you need to pass variables down into a template part, a similar thinking pattern to react and props.

// you can use any value including objects.
set_query_var( 'var_name_to_be_used_later', 'Value to be retrieved later' );

// in the template part
$passedVariable = get_query_var('var_name_to_be_used_later');
Updated: 6 February 2022

List all blocks on the page

If you have ever done any Gutenberg block development for WordPress this is an essential snippet.

// lets grab all of the blocks from the post content
$blocks = parse_blocks($post->post_content);
// and lets print it out
var_dump($blocks);
Updated: 6 February 2022