One of the issues we need or at least should tackle before resubmitting CiviCRM 4.7.x to the WP plugin repo is address the issue of WordPress in it's own directory - https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory. While this is not the standard or common way of installing, it is used. It is more common for Agency installed or Enterprise sites than stand alone smaller organizations.
Currently CiviCRM does not handle this well. WordPress ends up with two urls - one for the home page and one fro the admin. So http://example.com is the public facing web site, and http://example.com/wp is the path to wp-admin, the content directory and CiviCRM.
The settings appear in the admin as follows:
Then when we install CiviCRM we get the Green Light:
And the directories are set as follows:
In civicrm.settings.php we get the following:
$civicrm_root = '/home/aaa/webdir/wp/wp-content/plugins/civicrm/civicrm/';
if (!defined('CIVICRM_TEMPLATE_COMPILEDIR')) {
define( 'CIVICRM_TEMPLATE_COMPILEDIR', '/home/aaa/webroot/wp/wp-content/uploads/civicrm/templates_c/');
}
if (!defined('CIVICRM_UF_BASEURL')) {
define( 'CIVICRM_UF_BASEURL' , 'https://example.com/wp/');
}
The CMS settings are now set to have the basepage set to: https://example.com/wp/civicrm
##Issues First issue is when we try and add a CiviCRM component via the shortcode button.
If we try and use a CiviCRM link such as https://example.com/wp/civicrm/?q=civicrm%2Fcontribute%2Ftransact&reset=1&id=1 WP sends us to https://example.com/civicrm/?q=civicrm%2Fcontribute%2Ftransact&reset=1&id=1 This results in a 404 error.
##Hack
If we go and make the base page 'civicrm' a child page (using a parent named 'wp') then this link https://example.com/wp/civicrm/?q=civicrm%2Fcontribute%2Ftransact&reset=1&id=1 becomes valid and works.
However, the shortcode button is still broken.
##Testing and Planed fixes:
In the file: civicrm/CRM/Utils/System/WordPress.php replace the function private function getBaseUrl($absolute, $frontend, $forceBackend) with the following:
private function getBaseUrl($absolute, $frontend, $forceBackend) {
$config = CRM_Core_Config::singleton();
if (!defined('CIVICRM_UF_ADMINURL')) {
define('CIVICRM_UF_ADMINURL', CIVICRM_UF_BASEURL . '/wp-admin/');
}
if (!defined('CIVICRM_UF_WP_BASEURL')) {
define('CIVICRM_UF_WP_BASEURL', CIVICRM_UF_BASEURL );
}
if ((is_admin() && !$frontend) || $forceBackend) {
$url = CIVICRM_UF_ADMINURL . 'admin.php';
return $url;
}
elseif (defined('CIVICRM_UF_WP_BASEPAGE')) {
$url = CIVICRM_UF_WP_BASEURL . CIVICRM_UF_WP_BASEPAGE ;
return $url;
}
elseif (isset($config->wpBasePage)) {
$url = CIVICRM_UF_WP_BASEURL . $config->wpBasePage;
return $url;
}
return $absolute ? $url : preg_replace(';https?://[^/]+/;', '/', $url);
}
With this change, the backend and full CiviCRM links on the front end work fine. Needs more testing on standard installs and subdirectory installs.
Corresponing updates to civicrm.settings.php can be made to set the above constants:
/**
* Site URLs:
*
* This section defines absolute and relative URLs to access the host CMS (Backdrop, Drupal, or Joomla) resources.
*
* IMPORTANT: Trailing slashes should be used on all URL settings.
*
*
* EXAMPLE - Drupal Installations:
* If your site's home url is http://www.example.com/drupal/
* these variables would be set as below. Modify as needed for your install.
*
* CIVICRM_UF_BASEURL - home URL for your site:
* define( 'CIVICRM_UF_BASEURL' , 'http://www.example.com/drupal/');
*
* EXAMPLE - Backdrop CMS Installations:
* If your site's home url is http://www.example.com/backdrop/
* these variables would be set as below. Modify as needed for your install.
*
* CIVICRM_UF_BASEURL - home URL for your site:
* define( 'CIVICRM_UF_BASEURL' , 'http://www.example.com/backdrop/');
*
* EXAMPLE - Joomla Installations:
* If your site's home url is http://www.example.com/joomla/
*
* CIVICRM_UF_BASEURL - home URL for your site:
* Administration site:
* define( 'CIVICRM_UF_BASEURL' , 'http://www.example.com/joomla/administrator/');
* Front-end site:
* define( 'CIVICRM_UF_BASEURL' , 'http://www.example.com/joomla/');
*
*/
if (!defined('CIVICRM_UF_WP_BASEURL')) {
define( 'CIVICRM_UF_WP_BASEURL' , 'http://wpsub.dev/');
}
if (!defined('CIVICRM_UF_BASEURL')) {
define( 'CIVICRM_UF_BASEURL' , 'http://wpsub.dev/wp');
}
if (!defined('CIVICRM_UF_ADMINURL')) {
define( 'CIVICRM_UF_ADMINURL' , 'http://wpsub.dev/wp/wp-admin/' );
}
The shortcode button is still an issue.
- In the file civicrm\includes\civicrm.shortcodes.modal.php replace existing function add_form_button() with the following:
public function add_form_button() {
// add button to WP selected post types, if allowed
if ( $this->post_type_has_button() ) {
if (!$this->civi->initialize()) {
return;
}
$config = CRM_Core_Config::singleton();
$imageBtnURL = $config->resourceBase . 'i/logo16px.png';
$url = admin_url( 'admin.php?page=CiviCRM&q=civicrm/shortcode&reset=1' );
echo '<a href= "' . $url . '" class="button crm-popup medium-popup crm-shortcode-button" data-popup-type="page" style="padding-left: 4px;" title="' . __( 'Add CiviCRM Public Pages', 'civicrm' ) . '"><img src="' . $imageBtnURL . '" height="15" width="15" alt="' . __( 'Add CiviCRM Public Pages', 'civicrm' ) . '" />'. __( 'CiviCRM', 'civicrm' ) .'</a>';
}
}
Modal had been called with a hardcoded link to wp-admin, so the modal could not display,
##Update June 8 2016:
Created the following PRs:
https://github.com/civicrm/civicrm-core/pull/8536
https://github.com/civicrm/civicrm-wordpress/pull/103
These PRs are the foundation to support WP In it's own Directory.
##TODOS:
Update Installer with settings to support
if (!defined('CIVICRM_UF_BASEURL')) {
define( 'CIVICRM_UF_BASEURL' , 'http://site.dev/wp/');
}
if (!defined('CIVICRM_UF_WP_BASEURL')) {
define( 'CIVICRM_UF_WP_BASEURL' , 'http://site.dev/');
}
if (!defined('CIVICRM_UF_ADMINURL')) {
define( 'CIVICRM_UF_ADMINURL' , 'http://site.dev/wp/wp-admin/' );
}
If wp-config.php has defined an alternate WP Content directory :
define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/apps' );
define( 'WP_CONTENT_URL', 'http://site.dev/apps' );
Test to see if the option 'home' matches the option 'siteurl'
//get home url
get_option('home');
//result - http://site.dev/wp
//get site url
get_option('siteurl');
//result - https://site.dev
If WP is in it's own directory these will not match
If either condition exists, then enable the below override
global $civicrm_setting;
// Override the resource url
$civicrm_setting['URL Preferences']['userFrameworkResourceURL'] = 'http://sites.dev/app/plugins/civicrm/civicrm/';
##Jefferson Sprint Sept 27 2016
Tested 4.7.12-rc against following WP alternate configurations:
A) WordPress wp-content directory moved
define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/alt-content' );
define( 'WP_CONTENT_URL', 'http://wpcrazy.dev/alt-content' );
Result – Success
B) WP plugins folder moved
define( 'WP_PLUGIN_DIR', dirname(__FILE__) . '/alt-plugins' );
define( 'WP_PLUGIN_URL', 'http://wpcrazy.dev/alt-plugins' );
Result - Success
C) WordPress wp-content and uploads directories moved:
define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/alt-content' );
define( 'WP_CONTENT_URL', 'http://uploads.dev/alt-content' );
define( 'WP_PLUGIN_DIR', dirname(__FILE__) . '/alt-plugins' );
define( 'WP_PLUGIN_URL', 'http://uploads.dev/alt-plugins' );
define( 'UPLOADS', '/alt-content/media' );
Result - Success
D) WordPress in it's own directory and moving wp-content, plugins and uploads directories
define( 'WP_SITEURL', 'http://wpcrazy.dev/wp' );
define( 'WP_HOME', 'http://wpcrazy.dev' );
define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/alt-content' );
define( 'WP_CONTENT_URL', 'http://uploads.dev/alt-content' );
define( 'WP_PLUGIN_DIR', dirname(__FILE__) . '/alt-plugins' );
define( 'WP_PLUGIN_URL', 'http://uploads.dev/alt-plugins' );
define( 'UPLOADS', '/alt-content/media' );
Result - Failure on install - Uploads directory cannot be moved along with WP in it's own directory. Or at least I have failed at this test.
E) WordPress in it's own directory and moving wp-content, plugins directories
define( 'WP_SITEURL', 'http://wpcrazy.dev/wp' );
define( 'WP_HOME', 'http://wpcrazy.dev' );
define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/alt-content' );
define( 'WP_CONTENT_URL', 'http://uploads.dev/alt-content' );
define( 'WP_PLUGIN_DIR', dirname(__FILE__) . '/alt-plugins' );
define( 'WP_PLUGIN_URL', 'http://uploads.dev/alt-plugins' );
Update civicrm.settings.php with the following code block, replacing installer's CIVICRM_UF_BASEURL section
if (!defined('CIVICRM_UF_WP_BASEURL')) {
define( 'CIVICRM_UF_WP_BASEURL' , 'http://wpcrazy.dev/');
}
if (!defined('CIVICRM_UF_BASEURL')) {
define( 'CIVICRM_UF_BASEURL' , 'http://wpcrazy.dev/wp');
}
if (!defined('CIVICRM_UF_ADMINURL')) {
define( 'CIVICRM_UF_ADMINURL' , 'http://wpcrazy.dev/wp/wp-admin/' );
}
Then add the following:
global $civicrm_setting;
$civicrm_setting['URL Preferences']['userFrameworkResourceURL'] = 'http://wpcrazy.dev/alt-plugins/civicrm/civicrm/';
This is because the [civicrm.root] variable returns - http://wpcrazy.dev/wp/srv/www/wpcrazy/alt-plugins/civicrm/civicrm/ instead of http://wpcrazy.dev/alt-plugins/civicrm/civicrm/
After above changes: Result - Success
##Next Steps
Update installer - test for site_url() home_url() and admin_url() and set as below - look at getCiviSourceStorage in \Civi\Core\Paths – determine if this can be corrected for alternate wp-content plus WP in subdirectory. If not set at install:
echo site_url();
http://wpcrazy.dev/wp
echo home_url();
http://wpcrazy.dev
echo admin_url();
http://wpcrazy.dev/wp/wp-admin/
echo plugin_dir_url(CIVICRM_PLUGIN_FILE);
http://wpcrazy.dev/alt-plugins/civicrm
CIVICRM_UF_WP_BASEURL = home_url();
CIVICRM_UF_BASEURL = site_url();
CIVICRM_UF_ADMINURL = admin_url()
userFrameworkResourceURL = plugin_dir_url(CIVICRM_PLUGIN_FILE) . 'civicrm';
##Update from Edale Sprint 2016:
##From haystack via Mattermost
I've just collared Tim and he's put a system of adding extra settings/constants into civicrm-settings.php (via an external file in the same directory) which means we can look at adding the useful constants via that mechanism. Here's the diff https://gist.github.com/totten/f080b6fa821d1b6c4d6711e750f04cd2
Therefore, so we only need to create the civicrm.settings.extra.php for WP. https://gist.github.com/totten/f080b6fa821d1b6c4d6711e750f04cd2
##23 April 2017:
PR: https://github.com/civicrm/civicrm-core/pull/10214 and PR: https://github.com/civicrm/civicrm-wordpress/pull/105 implements the above. Currently adds the civicrm.settings.extra.php file for WP only and adds the minimum number of defines to get all common install scenarios automated.
##17 October 2017
Changes Merged Release scheduled 4.7.27
##19 October 2017
Further testing due to CRM-21297 has uncovered that if wp is in its own directory AND the content directory has been moved there is an issue with ckeditor. https://github.com/civicrm/civicrm-core/blob/master/CRM/Core/Resources.php#L737 uses "[civicrm.root]" and does not find the new settings via civicrm.settings.php. https://github.com/civicrm/civicrm-core/blob/master/CRM/Admin/Page/CKEditorConfig.php#L273 uses "[civicrm.files]" and this also does not find the overridden settings.