我在这里四处寻找答案,但我没有找到确切的答案,所以我尝试一下:
我正在创建一个插件
我创建了一个自定义帖子类型,我创建了一个与此CPT关联的元框,在这个元框中,我有很多表单元素(From these values in the metabox I want to create new content for the current post(这里有个问题))/ul>
我正在使用:(这是一个基于类的插件)
//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;
}
最合适的回答,由SO网友:KAGG Design 整理而成
第二次尝试更接近结果,因为它是文件管理器,而不是操作,您可以返回正确的数据。但代码有几个错误,我已经修复了它们。
过滤器的适当参数数为3。第一个参数是$data
应修改并返回。这是WordPress中任何过滤器的标准行为。
优先级必须为整数,无需设置为99。我使用标准10。
以下是经过测试的工作代码:
/**
* Filters slashed post data just before it is inserted into the database.
*
* @param array $data An array of slashed, sanitized, and processed post data.
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as
* originally passed to wp_insert_post().
*
* @return array
*/
public function save_csvtohtml_settings( $data, $postarr, $unsanitized_postarr ) {
// Generate shortcode from settings form (in metabox).
$shortcode = \'[csvtohtml_create \';
$shortcode_attributes = [];
foreach ( $_POST as $p_key => $p_item ) {
if ( ( strpos( $p_key, \'frm_\' ) === 0 ) && \'\' !== $p_item ) {
$attribute = substr( $p_key, 4 );
$shortcode_attributes[] = substr( $p_key, 4 ) . \'="\' . $p_item . \'"\';
}
}
$shortcode .= implode( \' \', $shortcode_attributes );
$shortcode .= \']\';
$data[\'post_content\'] = $shortcode;
return $data;
}
add_filter( \'wp_insert_post_data\', array( $this, \'save_csvtohtml_settings\'), 10, 3 );