Add/update post meta with raw html to WYSIWYG Field
- This topic has 11 replies, 2 voices, and was last updated 4 years, 9 months ago by
Long Nguyen.
-
AuthorPosts
-
June 30, 2020 at 5:35 PM #20558
Bernhard Niemann
ParticipantHello,
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:
Is it possible to save this as raw html?
Thanks in advance!
June 30, 2020 at 10:07 PM #20560Long Nguyen
ModeratorHi 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.July 1, 2020 at 1:58 PM #20568Bernhard Niemann
ParticipantYes 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:
July 1, 2020 at 3:02 PM #20569Long Nguyen
ModeratorHi,
After creating a field
WYSIWYG
, you should use the functionupdate_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?
July 2, 2020 at 2:14 PM #20594Bernhard Niemann
ParticipantCreating 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);
July 2, 2020 at 3:19 PM #20596Long Nguyen
ModeratorHi,
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 ) );
August 6, 2020 at 5:32 PM #21145Bernhard Niemann
ParticipantHello,
sorry for my late response. Please see the video. There you can see, that it isnt working, maybe because of tinymce.
https://streamable.com/extxx2August 7, 2020 at 10:10 AM #21163Long Nguyen
ModeratorHi,
The function update_post_meta() updates the meta value in the backend as well. The issue is it does not display the HTML tag in the frontend. Could you please check the code which shows the field value in the frontend? Is it rwmb_meta()?
August 7, 2020 at 4:42 PM #21166Bernhard Niemann
ParticipantYes, im using rwmb_meta().
August 7, 2020 at 10:07 PM #21169Long Nguyen
ModeratorHi,
This problem is so weird, please follow this guide to know how to create the staging site https://www.wpbeginner.com/wp-tutorials/how-to-create-staging-environment-for-a-wordpress-site/.
Then share the credentials (Admin site and FTP account) via this form https://metabox.io/contact/. I will check it out.
August 11, 2020 at 4:41 PM #21207Bernhard Niemann
ParticipantYeah i will do this today or tomorrow, thank you for the help.
August 13, 2020 at 3:02 PM #21248Long Nguyen
ModeratorHi,
The problem happens because the code used the function
esc_attr()
, it escapes HTML code and returns only a string that's why we only see the<ul><li>
tag.I've just removed it then we can see the list as well.
$benefits = $_POST['benefits']; $aufgaben = $_POST['aufgaben']; $fachwissen = $_POST['fachwissen'];
Or you can keep the function
esc_attr()
then use the functionhtmlspecialchars_decode()
to convert special HTML entities back to characters./* Aufgaben */ function kie_job_get_aufgaben(){ return htmlspecialchars_decode( strip_tags(rwmb_meta( 'kie_aufgaben', get_the_ID() ), '<ul><li></ul></li>') ); }
-
AuthorPosts
- You must be logged in to reply to this topic.