The Helper class

Kirki includes some helper methods that allow you to make a few tasks easier.

Get the ID of an image from its URL

To get the attachment ID of an image from its URL, you can use the following:

$image_id = Kirki\Util\Helper::get_image_id( $url );

Get image details from its URL

$image = Kirki\Util\Helper::get_image_from_url( $url );

The above will return an array formatted like this:

array(
	'url'       // string, the URL of the image
	'width'     // integer, the width of the image
	'height'    // integer, the height of the image
	'thumbnail' // string, URL of the image's thumbnail.
);

Get an array of posts

$posts = Kirki\Util\Helper::get_posts( $args );

as $args you will have to use an array formatted as documented in WordPress’s get_posts function.

The difference between WordPress’s get_posts function and the Kirki\Util\Helper::get_posts method is that the method included in Kirki will return an array of posts formatted in a way that can be easily used in a control in the choices argument.

Example:

new Kirki\Field\Select(
	[
		'settings'    => 'my_setting',
		'label'       => __( 'This is the label', 'kirki' ),
		'section'     => 'my_section',
		'default'     => '',
		'priority'    => 10,
		'multiple'    => 1,
		'placeholder' => __( 'Select an option', 'kirki' ),
		'choices'     => Kirki\Util\Helper::get_posts(
			array(
				'posts_per_page' => 10,
				'post_type'      => 'page'
			) ,
		),
	]
);

Get an array of available taxonomies

$taxonomies = Kirki\Util\Helper::get_taxonomies();

This will return an array of all the available taxonomies so you can use it directly in a control’s choices argument.

Example:

new Kirki\Field\Select(
	[
		'settings'    => 'my_setting',
		'label'       => __( 'This is the label', 'kirki' ),
		'section'     => 'my_section',
		'default'     => '',
		'priority'    => 10,
		'multiple'    => 1,
		'placeholder' => __( 'Select an option', 'kirki' ),
		'choices'     => Kirki\Util\Helper::get_taxonomies(),
	]
);

Get an array of available post-types

$post_types = Kirki\Util\Helper::get_post_types();

This will return an array of all the publicly-available post-types so you can use it directly in a control’s choices argument.

Example:

new Kirki\Field\Select(
	[
		'settings'    => 'my_setting',
		'label'       => __( 'This is the label', 'kirki' ),
		'section'     => 'my_section',
		'default'     => '',
		'priority'    => 10,
		'multiple'    => 1,
		'placeholder' => __( 'Select an option', 'kirki' ),
		'choices'     => Kirki\Util\Helper::get_post_types(),
	]
);

Get an array of terms

$terms = Kirki\Util\Helper::get_terms( $taxonomies );

as $taxonomies you will have to use an array formatted as documented in WordPress’s get_terms function.

The difference between WordPress’s get_terms function and the Kirki\Util\Helper::get_terms method is that the method included in Kirki will return an array of posts formatted in a way that can be easily used in a control in the choices argument.

Example:

new Kirki\Field\Select(
	[
		'settings'    => 'my_setting',
		'label'       => __( 'This is the label', 'kirki' ),
		'section'     => 'my_section',
		'default'     => '',
		'priority'    => 10,
		'multiple'    => 1,
		'placeholder' => __( 'Select an option', 'kirki' ),
		'choices'     => Kirki\Util\Helper::get_terms( array('taxonomy' => 'category') ),
	]
);

For full list of choices https://developer.wordpress.org/reference/functions/get_terms/#source

Custom Post Types and Custom Taxonomy

Please note that to make the custom post types and taxonomies work you must add the fields and sections in init action.

Example:

add_action( 'init', function() {
	new Kirki\Field\Select(
		[
			'settings'    => 'my_setting',
			'label'       => __( 'This is the label', 'kirki' ),
			'section'     => 'title_tagline',
			'default'     => '',
			'priority'    => 10,
			'choices'     => Kirki\Util\Helper::get_terms( 'product_cat' ),
		]
	);
} );

Was this helpful?