我在这里四处寻找答案,但我没有找到确切的答案,所以我尝试一下:
我正在创建一个插件
我正在使用:(这是一个基于类的插件)
//I\'m using this hook because then I can retrieve the values of $_POST from the metabox
add_action( \'pre_post_update\', array( $this, \'save_csvtohtml_settings\'), 10, 3 );
public function save_csvtohtml_settings( $post_ID, $current_data )
{
//I want some values from $_POST to be used to create a new content
//in the actual post
//Example:
//$_POST[\'frm_source_files\'] = \'bundeslander_staple.csv\';
//$current_data[\'post_content\'] = \'bundeslander_staple.csv\';
$current_data[\'post_content\'] = $_POST[\'frm_source_files\'];
//Because this is a pre post update, I don\'t want to do
//a "save post" here (then I think it will mess up?)
return $current_data; //Wouldn\'t it be nice if this worked? :-)
}
我是否使用了正确的钩子?(pre\\u post\\u更新)如何change content value in db - to the generated value ($_POST[\'frm_source_files\']
) 从我的metabox中的表单我知道实际的钩子已经执行了(save_csvtohtml_settings()
) .是的,我知道我可以从表单中保存很多元数据值,但我只对实际生成的内容感兴趣。在我的场景中,没有必要将每个值都保存到数据库中。
<小时/>
I\'ve also tried this:
function ... {
...
....
add_filter( \'wp_insert_post_data\' , array( $this, \'save_csvtohtml_settings\')
, \'99\', 2 );
----
}
public function save_csvtohtml_settings( $post_ID, $current_data )
{
//Generate shortcode from settings form (in metabox)
$shortcode = \'[csvtohtml_create \';
$shortcode_attributes = [];
foreach( $_POST as $p_key => $p_item )
{
if (substr($p_key, 0, 4) == \'frm_\') {
if (mb_strlen($p_item) > 0)
{
$attribute = substr($p_key,4);
$shortcode_attributes[] = substr($p_key,4) . \'="\' . $p_item . \'"\';
}
}
}
$shortcode .= implode(\' \', $shortcode_attributes);
$shortcode .= \']\';
$current_data[\'post_content\'] = $shortcode;
// Change post content to the generated code (from current attributes)
//This changes in array $current_data but does not save the actual post
return $current_data;
}