Skip to content

Instantly share code, notes, and snippets.

@BhargavBhandari90
Created April 3, 2024 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BhargavBhandari90/b88db2d00b2d41d4901cd60236bbc976 to your computer and use it in GitHub Desktop.
Save BhargavBhandari90/b88db2d00b2d41d4901cd60236bbc976 to your computer and use it in GitHub Desktop.
Convert BB Discussion/Reply medium editor to WYSWING editor
<?php
// Enable WYSWING editor.
function bbp_enable_visual_editor( $r = array() ) {
$r['tinymce'] = true;
$r['media_buttons'] = true;
// Load WHYSWING.
wp_editor( 'Test Content', 'test', array(
'wpautop' => $r['wpautop'],
'media_buttons' => $r['media_buttons'],
'textarea_rows' => $r['textarea_rows'],
'tabindex' => $r['tabindex'],
'tabfocus_elements' => $r['tabfocus_elements'],
'editor_class' => $r['editor_class'],
'tinymce' => $r['tinymce'],
'teeny' => $r['teeny'],
'quicktags' => $r['quicktags'],
'name' => 'bbp_' . $r['context'] . '_content',
) );
return $r;
}
add_filter( 'bbp_after_get_the_content_parse_args', 'bbp_enable_visual_editor' );
// Disable BB medium Editor.
function disable_bb_medium_editor( $status ) {
return false;
}
add_filter( 'bbp_use_wp_editor', 'disable_bb_medium_editor', 999 );
// Set value from new editor.
function set_new_content( $content ) {
$content = $_POST['test'];
return $content;
}
add_filter( 'bbp_new_topic_pre_content', 'set_new_content' );
function ombed_callback( $content, $topic_id ) {
return convert_links_to_embeds( $content );
}
add_filter( 'bbp_get_topic_content', 'ombed_callback', 10, 2 );
// Function to convert links in forum content to embeds
function convert_links_to_embeds( $content ) {
// Regular expression to match anchor tags with href attributes
$pattern = '/<a\s+([^>]*?\s+)?href=([\'"])(.*?)\2([^>]*)>(.*?)<\/a>/i';
// Find all anchor tags in the content
preg_match_all( $pattern, $content, $matches, PREG_SET_ORDER );
// Iterate over each matched anchor tag and replace it with the embed code
foreach ( $matches as $match ) {
$url = $match[3];
$attributes = $match[1] . $match[4];
$embed_code = wp_oembed_get( $url );
$replacement = $embed_code;
$content = str_replace( $match[0], $replacement, $content );
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment