Integrating wordpress into Drupal 7 site

So I had one plugin for Wordpress which I really wanted to use on one of my Drupal website. Naive approach was:

define('WP_USE_THEMES', false);
include_once './wp/wp-blog-header.php';

Only to get:

Fatal error: Cannot redeclare timer_start() (previously declared in /home/ariel/wwwroot/drupal-mine/includes/bootstrap.inc:116) in /home/site/wwwroot/site/includes/bootstrap.inc on line 122

Turned out both Drupal and Wordpress define several function using same name (and neither uses namespaces, unfortunately.) After looking for other potential solutions, I decided to try to simply rename all offending functions inside Wordpress. Turned out it was just 4 of them:

find ./ -type f -exec sed -i 's/timer_start/wp_timer_start/g' {} \;
find ./ -type f -exec sed -i 's/timer_stop/wp_timer_stop/g' {} \;
find ./ -type f -exec sed -i 's/comment_form/wp_comment_form/g' {} \;
find ./ -type f -exec sed -i 's/image_resize/wp_image_resize/g' {} \;

After that I was able to get through "redeclare" error.

Next task was to clean up "unnecessaries" (extra meta/title tags, emoji-related code, etc. Below is final code I came up with. It is header and footer pieces, and your wordpress-code should go in the middle:

define('WP_USE_THEMES', false);
define('STYLESHEETPATH', '');
define('TEMPLATEPATH', '');
include_once './wp/wp-load.php';
 
global $wp_filter;
 
function disable_wp_emojicons() {
 
  // all actions related to emojis
  remove_action( 'admin_print_styles', 'print_emoji_styles' );
  remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
  remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  remove_action( 'wp_print_styles', 'print_emoji_styles' );
  remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
  remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
  remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
 
  // filter to remove TinyMCE emojis
  add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' );
}
 
disable_wp_emojicons();
remove_action('wp_head', 'dns-prefetch' );
remove_action('wp_head', 'wp_resource_hints', 2 );
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'rest_output_link_wp_head');
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('template_redirect', 'rest_output_link_header', 11, 0 );
 
ob_start();
wp_head();
 
# YOUR WP CODE GOES HERE (OPTIONAL)
 
wp_footer();
$wpcode = ob_get_contents();
ob_end_clean();
 
echo preg_replace('/^.+jquery\/jquery.+$/m', '', $wpcode);

This will ensure that all necessary css/js from wordpress plugin is loaded.

Unfortunately, since i replaced code inside wordpress code base, after each wordpress update, i have to re-run functions renaming code again.. I probably simply disable updates for time being, since that wordpress site is not exposed any way except via invoking plugin code.

Tags: