Toggle

This block type renders a toggle link that sends an AJAX request on every subsequent click, changing its icon and label depending on the current state.

Parameters

  • view - the link view, set to icon if labels are not required;

  • url - the URL for sending AJAX requests on click;

  • states - an array of the link state parameters;

  • attributes - the link HTML attributes;

  • active - true if the toggle link is currently active.

Example

The code below renders a toggle link that displays the "Follow" text with the user-plus Font Awesome icon when inactive and the "Unfollow" text with the user-minus icon when active. It sends an AJAX request to the https://example.com URL, changing the link icon and label on every subsequent click.

echo ( new HivePress\Blocks\Toggle(
	[
		'url'    => 'https://example.com',

		'states' => [
			[
				'icon'    => 'user-plus',
				'caption' => 'Follow',
			],
			[
				'icon'    => 'user-minus',
				'caption' => 'Unfollow',
			],
		],
	]
) )->render();

Last updated