1

Is it possible to save the content (inside the WP WYSIWYG (the_content)) into a custom field?

This is what I have so far:

add_action('edit_post', 'save_content_to_field');
function save_content_to_field($post_ID) {
    global $wpdb;

    if(!wp_is_post_revision($post_ID)) {
        $content = get_the_content($post_ID);
        add_post_meta($post_ID, 'desc', $content, true);
    }
}
1
  • Do you already have your custom field working? If so, share what code you're using so far and I'm sure we can help with the saving issue.
    – Myles
    Commented Apr 10, 2018 at 2:47

2 Answers 2

1

You can get the content with $_REQUEST, Try the below code:

add_action('publish_post', 'save_content_to_field');
function save_content_to_field($post_ID) {
    if(isset($_REQUEST['content'])) {
        update_post_meta($post_ID, 'desc', $_REQUEST['content']);
    }
}

UPDATED With the below code if you want to sore with shortcode support:

add_action('publish_post', 'save_content_to_field');
function save_content_to_field($post_ID) {
    $post = get_post($post_ID);
    $result = apply_filters('the_content',$post->post_content);

    if($result) {
        update_post_meta($post_ID, 'desc', $result);
    }
}
2
  • working, but... this is getting the content as HTML and I would like to save it like it's inside the WP WYSIWYG. Just imagine, If I have a shortcode inside there, I would like to save it like I see it on the editor.
    – bpy
    Commented Apr 10, 2018 at 12:36
  • 1
    The post updated. Commented Apr 10, 2018 at 16:34
0

OK!

I found this way and this might be useful to someone:

add_action('edit_post', 'save_content_to_field');
function save_content_to_field($post_ID) {
    global $wpdb;

    if(!wp_is_post_revision($post_ID)) {
        //$content = get_the_content($post_ID);
        global $post;
        $content = apply_filters('the_content', $post->post_content);

        add_post_meta($post_ID, 'desc', $content, true);
       }
     }
5
  • I would just apply the filters on output the way WordPress does with the_content? I don't see what you gain doing this, other than potentially breaking something, since your code lacks the context of a front-end request.
    – Milo
    Commented Apr 10, 2018 at 16:00
  • @Milo can you please elaborate (if possible)? I don't understand much of programming, but I would like to learn. Thanks
    – bpy
    Commented Apr 10, 2018 at 16:20
  • Your code applies the_content filters, which is what WordPress does only on display, it doesn't save the results. You're also applying them outside of the loop and not in the context of a page view. Some shortcodes enqueue scripts/styles, and rely on checking the context of display (is_single, is_archive, etc.), they may be broken when you try to display that pre-rendered content.
    – Milo
    Commented Apr 10, 2018 at 17:37
  • @milo so I need to remove the apply_filters?
    – bpy
    Commented Apr 10, 2018 at 22:01
  • 1
    I would move it from the save function to wherever you're displaying the content.
    – Milo
    Commented Apr 11, 2018 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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