Skip to content

Comment

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

Intro

Ultimate Fields allows you to add additional fields to your comments.

Additional fields may only be displayed in the dashboard. Your front-end comment forms will not be modified.

Usage through the interface

To add fields to comments 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 "Comment" from the bottom row.
  4. Adjust when and where to show fields

Usage through PHP

The comment location in Ultimate Fields is handled by the Ultimate_Fields\Location\Comment 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( $args = array() ) {

$args is an array, which allows you to set arguments without having to explicitly call the particular setter. For example, you can pass 'stati' => 'approved' instead of calling ->set_stati( 'approved' ).

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\Comment;

$container = Container::create( 'comment-settings' );

// Create a new location and add definitions to it
$location = new Comment();
$location->set_stati( 'approved' );

// 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 "comment" string as the first parameter to add_location(). The rest of the parameters are the same as for the constructor of the location class.

use Ultimate_Fields\Container;

Container::create( 'comment-settings' )
	->add_location( 'comment', array(
		'stati' => 'approved'
	));

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 Comment location, $type parameter of all *_value functions should have the "comment_XX" format with XX representing the ID of the comment.

Examples:

<?php
$style = '';

if( get_value( 'featured_comment', 'comment_5' ) ) {
  $style = "font-size: 2em";
}
?>

<div style="<?php echo $style ?>">
	<!-- normal comment content -->
</div>

Options

There are a couple of options for the Comment location and all of them are listed below.

Stati

You can check which stati you want to associate a comment with. This settings allows you to show fields either on all comments or when a comment is marked as:

  1. Approved: keyword approved
  2. Pending: keyword pending
  3. Spam: keyword spam

Usage in PHP:

$container->add_location( 'comment', array(
	'stati' => 'pending'
));

Priority

You can select whether the additional fields should appear with a higher priority than other meta boxes on the comment screen.

Usage in PHP:

$container->add_location( 'comment', array(
	'priority' => 'high'
));

Usage with the REST API

In the UI

you need to go the "REST API" tab and select which fields you want to include. You can also select if those fields are editable or not.

In PHP

Please read the REST API section section of the Container Settings article, as the REST functionality is directly controlled in containers.

Clone this wiki locally