Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I create a meta custom input to make a page builder. So i can drop & drag element to build content for post.

And when i save post, i want it will save directly to post content.

So i think i will change the $_POST and hook it to save_post action.

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

I have try but it does not work. Any advises ? Code:

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();
share|improve this question
    
Why do you want to save meta box content into post content? –  Vinod Dalvi May 22 '13 at 9:29
    
I create a custom metabox content instead for post's editor. If i want to it present in front page, so i need to save it into post content. –  Quang Trung Nguyen May 22 '13 at 9:39
    
ok. I got it but the above code that you have added in question does not display any textbox or textarea to enter content in it. Did you miss to copy it here or you have not created it? –  Vinod Dalvi May 22 '13 at 9:55
    
in function stPageBuilderCallBack i will include a form to do it. I sure have a custom value, the only thing we need to save this value to post content –  Quang Trung Nguyen May 22 '13 at 10:03

2 Answers 2

up vote 1 down vote accepted

The answer appears to be clear if you look at the source code. The action you're hooking to occurs inside wp_insert_post, and is called just before it returns the $post_ID variable. This means that all the manipulation and data insertion has already taken place, so modifying the $_POST array will do nothing. You must look for things to do prior to database modification for your changes to have any effect. Luckily, WordPress has many options to choose from.

In short, here's what I would do:

//  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;
}

Let us know how it goes!

share|improve this answer
    
Yes, that is what i need. Thank you very much ! –  Quang Trung Nguyen May 22 '13 at 10:07
    
Glad it helped :) –  tbuteler May 22 '13 at 10:54

Did you try using wp_insert_post function from WordPress to insert post into post content

Example:

// 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 );
share|improve this answer
    
He's already hooking to an action inside wp_insert_post. Calling this function within his existing logic can cause an infinite loop. You should clarify what you intend for him to do. –  tbuteler May 22 '13 at 10:01
    
This will create another post, not update an existing one. You could use wp_update_post() on the save_post hook, but beware the infinite loop: codex.wordpress.org/Plugin_API/Action_Reference/… –  Stephen Harris May 22 '13 at 10:01
    
Plus what @StephenHarris said; always with the sharper eye :) –  tbuteler May 22 '13 at 10:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.