将自定义值保存到帖子的主要内容

时间:2013-05-22 作者:Chameron

我创建一个元自定义输入来创建一个页面生成器。这样我就可以放下&;拖动元素以生成帖子的内容。

当我保存帖子时,我希望它能直接保存到帖子内容。

因此,我想我将更改$\\u POST并将其挂钩以保存\\u POST操作。

$_POST[\'content\'] = $_POST[\'page-builder-content\'];

我试过了,但没用。有什么建议吗?代码:

function stPageBuilder() {
    $screens = array(\'post\', \'page\');

    foreach ($screens as $screen) 
    {
        add_meta_box(
            \'st-post-class\',            // Unique ID
            esc_html__( \'Page Builder\', page-builder\' ),        // Title
            \'stPageBuilderCallBack\',        // Callback function
            $screen,                    // Admin page (or post type)
            \'advanced\',                 // Context
            \'default\'                   // Priority
        );
    }
}

function stPageBuilderCallBack($post, $box) {
    wp_nonce_field( basename( __FILE__ ), \'st-page-builder-nonce\' );
    // here is html layout for drag & drop feature
}

function stPageBuilderSave($postID) 
{
    if (\'page\' == $POST[\'post_type\']) {
        if (!current_user_can(\'edit_page\', $postID)) return;
    } else {
        if (!current_user_can(\'edit_post\', $postID)) return;
    }

    if (!isset($_POST[\'st-page-builder-nonce\']) || !wp_verify_nonce($_POST[\'st-page-builder-nonce\'], basename(__FILE__))) return;

    $_POST[\'content\'] = $_POST[\'st-page-builder-content\'];

}

function staddPageBuilder() {
    add_action( \'add_meta_boxes\', \'stPageBuilder\' );    
    add_action( \'save_post\', \'stPageBuilderSave\' );
}

staddPageBuilder();

2 个回复
最合适的回答,由SO网友:Tomas Buteler 整理而成

如果查看源代码,答案似乎很清楚。您要挂接的操作发生在内部wp_insert_post, 并在返回$post_ID 变量这意味着所有操作和数据插入都已经发生,因此修改$_POST 数组将不起任何作用。您必须在修改数据库之前寻找要做的事情,以使您的更改产生任何效果。幸运的是,WordPress有许多选项可供选择。

简而言之,我会这样做:

//  Hook to a different action / filter
function staddPageBuilder() {
    // ...
    add_filter( \'wp_insert_post_data\', \'stPageBuilderSave\', 10, 2);
}

// Notice the new filter passes different arguments, so we need to adapt the function
function stPageBuilderSave($data, $postarr) 
{
    // Since we don\'t have the $postID variable, let\'s try to grab it from the array passed by the insert function
    // Note that passing the object ID to the current_user_can function is very unusual and you can do without it
    $postID = isset($postarr[\'ID\']) ? $postarr[\'ID\'] : 0;

    if (\'page\' == $POST[\'post_type\']) {
        if (!current_user_can(\'edit_page\', $postID)) return;
    } else {
        if (!current_user_can(\'edit_post\', $postID)) return;
    }

    if (!isset($_POST[\'st-page-builder-nonce\']) || !wp_verify_nonce($_POST[\'st-page-builder-nonce\'], basename(__FILE__))) return;

    // This bit changes, because the action is different and so are the function\'s arguments
    $data[\'post_content\'] = $_POST[\'st-page-builder-content\'];
    return $data;
}
让我们知道进展如何!

SO网友:Core

您是否尝试使用wp_insert_post WordPress提供的将帖子插入帖子内容的功能

示例:

// Create post object
$my_post = array(
  \'post_title\'    => \'My post\',
  \'post_content\'  => \'This is my post.\',
  \'post_status\'   => \'publish\',
  \'post_author\'   => 1,
  \'post_category\' => array(8,39)
);

// Insert the post into the database
wp_insert_post( $my_post );

结束

相关推荐