Custom Fields
Re-Envisioned
Support General Add/update post meta with raw html to WYSIWYG Field

  • Creator
    Topic
  • #20558
    Bernhard Niemann
    Participant

    Hello,

    i want to add/update content to a wysiwyg field created with metabox. When Im doing this it saves the html content as normal text, not as html. Watch the following screen to understand:

    https://ibb.co/T46kBkb

    Is it possible to save this as raw html?

    Thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #20560
    Long Nguyen
    Moderator

    Hi Bernhard,

    If you want to insert some HTML code to the field WYSIWYG, please switch to the tab Text then type the HTML code. See my screenshot https://share.getcloudapp.com/P8ueQpLJ.

    #20568
    Bernhard Niemann
    Participant

    Yes I know how to do this, but if I add raw html code by php like this:

    add_post_meta($post_id, 'kie_arbeitsort_front', $arbeitsort, false);

    it is not possible to add raw html content. It always save the html content as normal text, which leads to the content to be saved as something like this:

    https://ibb.co/HH6w8s8

    #20569
    Long Nguyen
    Moderator

    Hi,

    After creating a field WYSIWYG, you should use the function update_post_meta(). It updates the HTML content in the tab Text as well. See my screen record https://www.loom.com/share/d1f5a83412454a999316b6e4f4bef292.

    Could you please share the code which creates the field and HTML content updated if the problem still happens?

    #20594
    Bernhard Niemann
    Participant

    Creating the field:

    function kie_jobs_aufgaben_metabox( $meta_boxes ) {
    
        $prefix = 'kie_';
    
        $meta_boxes[] = array(
            'id' => 'aufgaben',
            'title' => esc_html__( 'Aufgaben', 'kie-stellenboerse' ),
          'post_types' => array( 'jobs' ),
            'context' => 'after_title',
          'priority' => 'high',
            'autosave' => true,
          'fields' => array(
            array(
                'name'    => 'Aufgaben',
                'id'      => $prefix . 'aufgaben',
                'type'    => 'wysiwyg',
                'add_to_wpseo_analysis' => true,
                'class' => 'kie_metabox_textarea',
                'raw'     => false,
                'options' => array(
                    'textarea_rows' => 4,
                    'teeny'         => true,
                ),
            ),
          ),
        );
    
        return $meta_boxes;
    }

    Updating Post Meta:
    update_post_meta($post_id, 'kie_aufgaben', wp_kses_post($aufgaben), false);

    #20596
    Long Nguyen
    Moderator

    Hi,

    Your code works as well, see the screen record again https://www.loom.com/share/1b58e921914348eeac49aefe4fb5610e.

    add_filter( 'rwmb_meta_boxes', 'kie_jobs_aufgaben_metabox' );
    function kie_jobs_aufgaben_metabox( $meta_boxes ) {
    
        $prefix = 'kie_';
    
        $meta_boxes[] = array(
            'id' => 'aufgaben',
            'title' => esc_html__( 'Aufgaben', 'kie-stellenboerse' ),
            'post_types' => array( 'post' ),
            'context' => 'after_title',
            'priority' => 'high',
            'autosave' => true,
            'fields' => array(
                array(
                    'name'    => 'Aufgaben',
                    'id'      => $prefix . 'aufgaben',
                    'type'    => 'wysiwyg',
                    'add_to_wpseo_analysis' => true,
                    'class' => 'kie_metabox_textarea',
                    'raw'     => false,
                    'options' => array(
                        'textarea_rows' => 4,
                        'teeny'         => true,
                    ),
                ),
            ),
        );
    
        return $meta_boxes;
    }
    
    $meta_value = '<h2 style="color: green">Test HTML tag</h2>';
    update_post_meta( 4, 'kie_aufgaben', wp_kses_post( $meta_value ) );
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
grid gear info copy caret-up caret-down trash