如何在WordPress的wpdatatables中为嵌套Json创建钩子

问题描述 投票:0回答:1

我在我的 WordPress 网站上安装了 wpdatatables。我能够将表连接到嵌套 json 链接

https://wp.kvvzwaluwen.be/u15a-wedstrijden/ table image linked to nested json 我有 home id,但我想更深入地了解 homeClub -Name(这是在子数组中) enter image description here

但是为了更深入地了解,我需要调用钩子,其中值分隔符是{

https://wpdatatables.com/documentation/creating-wpdatatables/creating-wordpress-tables-from-nested-json-data-with-json-authentication/

Use hooks to parse one more level deep in root path
If one of chosen root path objects contains keys that have an array or objects as values and you need to include them in a table as cell values, then you can choose these two hooks:

wpdatatables_get_one_level_deep_json_data_from_array_as_string and
wpdatatables_set_one_level_deep_json_data_separator
For the first hook that accepts 3 params

$deepParse – boolean – false by default,
$jsonURL – string – nested JSON URL ,
$tableID – int – id of the table
you only need to provide a boolean value true, like in the example below:

/**
* Parse one level deep in nested JSON structure
* @param $deepParse
* @param $jsonURL
* @param $tableID
* @return bool|mixed
*/
function parseNestedJSON($deepParse, $jsonURL, $tableID){
   
   //use only for tables that have specific json url
   if($jsonURL == 'https://swapi.dev/api/people'){
      return true;
   }
 
   return $deepParse;
}
 
add_action('wpdatatables_get_one_level_deep_json_data_from_array_as_string', 'parseNestedJSON',10,3);
If you use this hook then it will be used arrayValuesSeparator which is break tag <br> and all array values will be parsed in cell one under another.

If you need another separator then you can use a second hook that accepts 3 params as well

$arrayValuesSeparator – string – tag by default,
$jsonURL – string – nested JSON URL ,
$tableID – int – id of the table
where, as a separator, a comma is set, like in the example below:

/**
* Change array elements separator
* @param $arrayValuesSeparator
* @param $jsonURL
* @param $tableID
* @return bool|mixed
*/
function changeArraySeparator($arrayValuesSeparator, $jsonURL, $tableID){
   //use only for tables that have specific json url
   if($jsonURL == 'https://swapi.dev/api/people'){
      return ', ';
   }
 
   return $arrayValuesSeparator;
}
 
add_action('wpdatatables_set_one_level_deep_json_data_separator', 'changeArraySeparator',10,3);

我在我的functions.php中添加了第二部分,但是如何让这些值可见

我在functions.php中添加了代码,但不知道如何获取数据 我在哪里调用行动

/**
* Change array elements separator
* @param $arrayValuesSeparator
* @param $jsonURL
* @param $tableID
* @return bool|mixed
*/
function changeArraySeparator($arrayValuesSeparator, $jsonURL, $tableID){
   //use only for tables that have specific json url
   if($jsonURL == 'https://clubapi.prosoccerdata.com/games/team/15'){
      return '{ ';
   }
 
   return $arrayValuesSeparator;
}
 
add_action('wpdatatables_set_one_level_deep_json_data_separator', 'changeArraySeparator',10,7);
php wordpress hook
1个回答
0
投票

一种选择是使用

content
作为
rootPath
,然后使用钩子
wpdatatables_filter_nested_json_array
wpdatatables_filter_nested_json_rows_by_root_path
将子对象中的值移动到主对象中。

可能还有其他方法可以做到这一点。我正在使用类似的技术从其他字段创建“计算”字段。

function filter_list_row(&$row)
{
    // do specific things to the rows here
    
    // move homeClub.name to homeClubName
    if (isset($row['homeClub']['name'])) {
        // Create a new key called homeClubName
        $row['homeClubName'] = $row['homeClub']['name'];
    }
}

// This function runs when fetching data for the table
function filter_nested_json_array($data, $table_id, $table_settings)
{
    global $my_table_id;
    if ($table_id != $my_table_id) {
        return $data;
    }
    foreach ($data as &$row) {
        filter_list_row($row);
    }
    return $data;
}
add_filter('wpdatatables_filter_nested_json_array', 'filter_nested_json_array', 100, 3);

// This function runs when inspecting the data for table configuration
function filter_nested_json_rows_by_root_path($rows, $root, $response)
{
    // Should check if $root is 'content', I am not sure how to do this

    foreach ($rows as &$row) {
        $this->filter_list_row($row);
    }
    return $rows;
}
add_filter('wpdatatables_filter_nested_json_rows_by_root_path', 'filter_nested_json_rows_by_root_path', 100, 3);
© www.soinside.com 2019 - 2024. All rights reserved.