Links

Custom Data (Pro)

The page below describes the Custom Data feature for View. You can read about the Custom Data feature for Card here.

About the feature

Custom variables for extra data

Advanced Views automatically loads data from the selected fields into the View template. In most general cases, this will be sufficient to generate the desired output. However, there may be specific cases where you need to include additional data that is not available in the plugin's list of fields.
To address this need, you can take advantage of the Custom Data feature, which is available in the Pro version of the Advanced Views plugin. This feature allows you to retrieve any necessary data using a PHP code snippet and then use these extra variables in your template.
You can find the 'Custom Data' field under the 'Template' tab in the View settings. In this field, you can insert any PHP code and use WordPress functions as needed.

Custom Data snippet

The field with the snippet can be found in the Template tab of your View.
The snippet is a PHP code, which must return an instance of the Custom_View_Data class.
  • using the get_variables() method you can pass custom variables to the Card template
  • using the get_object_id() method you can get the id of the displaying object
  • using the get_custom_arguments() method you can access the custom arguments passed to the shortcode. The field is available in the both methods above.
  • using the get_default_variables() method you can access to the default twig variables (that filled out automatically by the plugin)
<?php
declare(strict_types=1);
use Org\Wplake\Advanced_Views\Pro\Bridge\Views\Custom_View_Data;
return new class extends Custom_View_Data {
/**
* @return array<string,mixed>
*/
public function get_variables(): array
{
return [
// "custom_variable" => get_post_meta($this->get_object_id(), "your_field", true),
// "another_var" => $this->get_custom_arguments()["another"] ?? "",
];
}
/**
* @return array<string,mixed>
*/
public function get_variables_for_validation(): array
{
// it's better to return dummy data here [ "another_var" => "dummy string", ]
return $this->get_variables();
}
};
Tip: if you pass custom variables using the get_variables() method, we recommend to change the get_variables_for_validation() method and return dummy data there.
Advanced Views has the built-in automatic template validation, which called on the Save action. There is no sense to return real data in this case, and it's better to return dummy data, to avoid potential issues with missing functions.
For example, if you use WP functions inside, they may be front-end only, and aren't available in the wp-admin area, which will lead to the failed validation.

Step by step

Go to Edit your View.
Switch to the Template tab.
Scroll down until you see the Custom Template Variables field.
Add your PHP code.
Click on the Update button to save your View.