Skip to content
Radoslav Georgiev edited this page Oct 21, 2018 · 4 revisions

Intro

Widgets in WordPress allow you to populate sidebars in order to display similarly structured data in multiple places on your website.

The Widget location of Ultimate Fields associates a container with one or more widgets.

Ultimate Fields does not create new widgets automatically

This location only works with existing widgets. If you are looking to create a new widget, please follow the simple steps from the Creating a custom widget tutorial.

Usage through the interface

To add fields to users with the the Administration Interface, please follow these steps:

  1. If you are not on the "Add Container" screen already, locate the "Ultimate Fields" section in the administration area and click the "Add New" button on the top (next to the "Containers" title).
  2. Locate the "Locations" box.
  3. Click "Widget" from the bottom row.
  4. Select if you want to add fields to every widget or only a specific one.

Usage through PHP

The user location in Ultimate Fields is handled by the Ultimate_Fields\Location\Widget class. In order to use it, you can either create the new location object manually or let the container do it for you.

The constructor of the class looks like this:

public function __construct( $widgets = array(), $args = array() ) {
  1. $widgets should either be the PHP class of a single widget or an array of widgets.
  2. $args is an array, which allows you to set arguments without having to explicitly call the particular setter.

Manual creation

Create a new location by using the new keyword and assign it to the container through the add_location() method.

use Ultimate_Fields\Container;
use Ultimate_Fields\Location\Widget;

$container = Container::create( 'wysiwyg_widget' );

// Create a new location and add definitions to it
$location = new Widget( 'WP_Widget_Text' );

// Once the location has been fully set up, add it to the container
$container->add_location( $location );

Do not forget to use the correct namespace for the location class!

Automatic creation

You can also let the container create the location for you by providing the "widget" string as the first parameter to add_location().

use Ultimate_Fields\Container;

Container::create( 'wysiwyg_widget' )
	->add_location( 'widget', 'WP_Widget_Text' );

This method allows you to use method chaining and shortens the syntax, in order to make the code more readable.

Data retrival

To retrieve the values of fields, associated with the Widget location, $type parameter of all *_value functions should be "widget". This will automatically link the functions with the current widget.

Examples:

$title    = get_value( 'title', 'widget' );
$sections = get_value( 'sections', 'widget' );
Clone this wiki locally