Skip to content

Commit 285ac85

Browse files
committed
Hacky fix for replacing template content properly
WordPress seems to offer no proper info about which editor field is being manipulated when using the `the_editor_content` filter. A hacky workaround is to see how many times a replacement has been made in a filter callback so we replace field contents only for the very first editor field on each page. Maybe find some other solution for this, not sure if anyone is going to insert editor fields above the stock field somewhere.
1 parent f882dd3 commit 285ac85

1 file changed

Lines changed: 33 additions & 22 deletions

File tree

classes/class-cpt-wplf-form.php

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -511,41 +511,52 @@ function maybe_load_imported_template( $post_type, $post ) {
511511
protected function override_form_template( $template_content, $form_id ) {
512512
$this->maybe_persist_override_template( $template_content, $form_id );
513513

514+
static $times_content_replaced = 0;
515+
514516
// Make the editor textarea uneditable.
515517
add_filter( 'the_editor', function ( $editor ) {
516-
if ( ! preg_match( '%id="wp-content-editor-container"%', $editor ) ) {
517-
return $editor;
518-
}
518+
if ( ! preg_match( '%id="wp-content-editor-container"%', $editor ) ) {
519+
return $editor;
520+
}
519521

520-
$editor = preg_replace( '%\<textarea %', '<textarea readonly="readonly" ', $editor );
522+
$editor = preg_replace( '%\<textarea %', '<textarea readonly="readonly" ', $editor );
521523

522-
$notice = _x(
523-
'This form template is being overridden by code, you must edit it in your project code',
524-
'Template override notice in form edit admin view',
525-
'wp-libre-form'
526-
);
524+
$notice = _x(
525+
'This form template is being overridden by code, you must edit it in your project code',
526+
'Template override notice in form edit admin view',
527+
'wp-libre-form'
528+
);
527529

528-
$notice = sprintf( '<div class="wplf-template-override-notice">%s</div>', $notice );
530+
$notice = sprintf( '<div class="wplf-template-override-notice">%s</div>', $notice );
529531

530-
return $notice . $editor;
532+
return $notice . $editor;
531533
} );
532534

533535
// Custom settings for the form editor.
534-
add_filter( 'wp_editor_settings', function ( $settings, $editor_id ) {
535-
if ( $editor_id !== 'content' ) {
536-
return $settings;
537-
}
536+
add_filter( 'wp_editor_settings', function ( $settings, $editor_id ) {
537+
if ( $editor_id !== 'content' ) {
538+
return $settings;
539+
}
538540

539-
$settings['tinymce'] = false;
540-
$settings['quicktags'] = false;
541-
$settings['media_buttons'] = false;
541+
$settings['tinymce'] = false;
542+
$settings['quicktags'] = false;
543+
$settings['media_buttons'] = false;
542544

543-
return $settings;
544-
}, 10, 2 );
545+
return $settings;
546+
}, 10, 2 );
545547

546548
// Replace all editor content with template content.
547-
add_filter( 'the_editor_content', function ( $content ) use ( $template_content ) {
548-
return $template_content;
549+
add_filter( 'the_editor_content', function ( $content ) use ( $template_content, &$times_content_replaced ) {
550+
// This is hacky, yes. We only want to override the content for the first
551+
// editor field we come by, meaning 99% of the time we hit the wanted form
552+
// template editor field at the top of the edit view page.
553+
if ( $times_content_replaced > 0 ) {
554+
return $content;
555+
}
556+
557+
$times_content_replaced++;
558+
559+
return $template_content;
549560
} );
550561
}
551562

0 commit comments

Comments
 (0)