Toolbox Shortcodes

Estimated reading: 4 minutes

You can use shortcodes to do almost anything in WordPress. You can also use Toolbox shortcodes to get whatever field and apply whatever template you want inline.

Every setting that is available in the Modules, is also available in the shortcodes. So, if you’re extending to accept more parameters, you can also use those to alter the results of a toolbox shortcode.

There are three shortcodes that you can use:

  • toolbox
  • toolboxwp
  • toolboxtwig

toolbox

The toolbox shortcode is the main shortcode for this plugin. It is used to retrieve a field and render it in the browser.

You can use it like this:

[toolbox field="fieldname"]

But you can also provide your settings while requesting the field:

[toolbox field="company_logo" size="medium" tag="true"]

would get the company_logo field from this post. It retrieves the medium sized thumbnail and adds tags around the url, adds the title- and alt-tag if it’s stored as an Array or ID in the database. It also applies default alignment.

NOTE: How these settings are handled is manageable by the toolbox/helpers/get_acf_field/type={fieldtype} filters.

Applying Twig templates

You can also directly apply a Twig template to the field. This is useful when you want to return a repeater inline, inside another module for instance.

Simply create or copy a twig template to the Twig Templates CPT. Make sure to create a template of type inclusion.

Now add the shortcode:

[toolbox field="myrepeater" twigtemplate="mytemplate"]

The field ‘myrepeater’ will be passed through the ‘mytemplate’ template and rendered to the browser.

toolboxwp

The toolboxwp shortcode is used to get information from wp and render it to the browser, according to the filters set in the toolbox/helpers/get_wp_field/type={returntype} filter.

the toolboxwp shortcode can retrieve the following information:

  • title
  • featuredimage
  • media
  • content
  • excerpt
  • permalink
  • publish
  • modified
  • author
  • comments
  • avatar
  • previous_post
  • next_post

You can use toolboxwp like this:

[toolboxwp field="title"]

to pull the post title.

[toolboxwp field="featuredimage" size="medium" tag="true"]

to pull the featured image of the post, with size medium and a tag surrounding the retrieved url.

toolboxtwig

The toolboxtwig shortcode is useable when the Timber-library is enabled. The contents of the shortcode is the Twig template that will be processed by the Twig engine.

When calling the shortcode you can also pass in a fieldname and a twigtemplate:

[toolboxtwig twigtemplate="my_resolver"][/toolboxtwig]

toolboxtwig Passing in a Custom Field

When providing the field attribute it will return the variables __field__ and __field__object for convenience. Respectively they will be the returned value for ACF’s get_field() and get_field_object() functions.

[toolboxtwig field="my_custom_field" twigtemplate="my_resolver"]

This shortcode will fetch the custom field called ‘my_custom_field’ and apply the Twig template identified in the CPT with the ‘my_resolver’ slug.

In the template you can use the variables __field__ and __field__object to get the custom fields value or field object value.

toolboxtwig Additional attributes

As of Toolbox v1.1 all and any attributes for the [toolboxtwig] are passed into the template using the __atts__ variable. This means, that you can use the shortcode more dynamically.

This makes way for very dynamic shortcode creation.

[toolboxtwig my_name="Jason Bourne" twigtemplate="show_my_name"]
{# 
    show_my_name.twig template 
#}
<h3>Your name is {{ __atts__.my_name }}</h3>

This methods allows you to write your custom shortcodes on the fly and from within your WP Dashboard.

Share this Doc

Toolbox Shortcodes

Or copy link

CONTENTS