网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

使用第二个WordPress内容编辑器(wysiwyg)插入图像时,不会呈现图像[caption]元数据

GG网络技术分享 2025-03-18 16:12 6


问题描述:

I have deployed some code into functions.php that gives me a second CLASSIC WYSIWYG / text editor (not dealing with Gutenberg right now) so that I can populate content for each post from both editors. Whenever I am editing a post, I see two wizzy\'s (the second editor is displayed in the admin area using a meta_box).

Having a second editor allows me to insert other code (like ads and other things) in between where each editor outputs to the page.

All works great except that when I insert images into the second editor, the caption data is escaped rather than rendered as the friendly caption text.

Below is the code for the second editor in functions.php.

     function custom_editor2_meta_box() {    

add_meta_box (

\'custom-editor2\',

__(\'Second Content Area\', \'custom-editor2\') ,

\'custom_editor2\',

\'post\'

);

}

//Displaying the meta box

function custom_editor2($post) {

echo \"<h3>Second content area that appears after the inline advertisement.</h3>\";

$content = get_post_meta($post->ID, \'custom_editor2\', true);

//This function adds the WYSIWYG Editor

wp_editor (

$content ,

\'custom_editor2\',

array ( \"media_buttons\" => true )

);

}

//This function saves the data you put in the meta box

function custom_editor2_save_postdata($post_id) {

if( isset( $_POST[\'custom_editor_nonce\'] ) && isset( $_POST[\'second\'] ) ) {

//Not save if the user hasn\'t submitted changes

if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {

return;

}

// Verifying whether input is coming from the proper form

if ( ! wp_verify_nonce ( $_POST[\'custom_editor_nonce\'] ) ) {

return;

}

// Making sure the user has permission

if( \'post\' == $_POST[\'second\'] ) {

if( ! current_user_can( \'edit_post\', $post_id ) ) {

return;

}

}

}

if (!empty($_POST[\'custom_editor2\'])) {

$data = $_POST[\'custom_editor2\'];

if (strlen($data) > 0) { update_post_meta($post_id, \'custom_editor2\', $data); }

}

}

add_action(\'save_post\', \'custom_editor2_save_postdata\');

add_action(\'admin_init\', \'custom_editor2_meta_box\');

What I expect to see is the image rendered and then the friendly text below it. Instead I see the [caption] syntax itself before and after the properly rendered image, as if the syntax was completely ignored by WordPress:

[caption id=\"attachment_165\" align=\"alignnone\" width=\"900\"]

(Image is rendered properly because this is where the image tag is located within the caption syntax)

Friendly caption text is here[/caption]

网友观点:

It looks like you want the caption shortcode to be processed in the WYSIWYG editor? This is not the intended function inside the WYSIWYG, which is meant to show the raw shortcode. If it\'s not displaying correctly on the frontend (template) try wrapping your call to print the content of the editor in do_shortcode.

You could possibly try outputting your content into the editor using do_shortcode but that would only work once. I.e. once the shortcode is converted to HTML you\'ll have HTML floating around in your editor.

标签:

提交需求或反馈

Demand feedback