Yes.
Took 2 years and no support from staff to get an answer to this. More than happy to necro the thread w/ all possible inherent disdain insinuated.
I'll hold my tongue... as best I can.
Tools & Settings -> Event Manager -> Add Event Handler -> Event = "Wordpress Installed".
Then after googling you can arrive to
some documentation on the event we want.
So you can run your command right there. Or follow their
guide on proper scripts.
In my case, I am hosted on windows, but you could probably use
sed
on linux to regex replace.
powershell -file C:\inetpub\AdminScripts\plesk\force_ssl_if_proxied.ps1 "<NEW_INSTALLATION_PATH>"
And a sample script utilizing the passed install path:
force_ssl_if_proxied.ps1
Code:
param(
[string]$Path
)
$CRLF = "`n";
$target = $Path+'\wp-config.php';
$find="Happy blogging. */"; # text present in all wp-config
$add_code="if (!empty(`$_SERVER['HTTP_X_FORWARDED_PROTO']) && `$_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
`$_SERVER['HTTPS'] = 'on';
}";
$str = [System.IO.File]::ReadAllText($target);
$str = $str.Replace($find, $find + $CRLF + $CRLF + $add_code + $CRLF);
[System.IO.File]::WriteAllText($target, $str);
However sadly that
fails, because
apparently the wp toolkit event only passes the relative path, which excludes the full path, and also excludes parent folder if it's a subdomain. Not sure how that could ever be useful to anyone. Cool...
So we must switch to some proper php code to avoid messy shell scripts and give ourselves some better intel:
- as per event handler class guide - drop the below php into a file in your install folder as they described
- and as per some discovery code I wrote for potential events in wordpress toolkit which is ENTIRELY undocumented:
- ext_wp-toolkit_uninstall
- ext_wp-toolkit_install
- And per lot's of online samples from github scripts for plesk, the following is born...
PHP:
<?php
use PleskExt\DomainConnect\DomainConnect;
class Modules_WordpressToolkit_EventListener implements EventListener {
public function filterActions() {
return [
'ext_wp-toolkit_install',
];
}
public function handleEvent($objectType, $objectId, $action, $oldValues, $newValues) {
if ($action == 'ext_wp-toolkit_install') {
$domain = \pm_Domain::getByGuid($newValues["Domain GUID"]);//getByDomainId($objectId);
if ($domain!=null && $domain->hasHosting()) {
$doc_root = $domain->getDocumentRoot();
$target = $doc_root."\\wp-config.php";
$find = "Happy blogging. */";
$add_code = "if (!empty(\$_SERVER['HTTP_X_FORWARDED_PROTO']) && \$_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {\r\n\t\$_SERVER['HTTPS'] = 'on';\r\n}";
$wpc = file_get_contents($target);
$wpc = str_replace($find, $find."\r\n".$add_code."\r\n", $wpc);
file_put_contents($target, $wpc);
}
}
}
}
return new Modules_WordpressToolkit_EventListener();
?>
However since the php EventListener code runs as [psaadm] , it does not have rights to write to the file, and
fails again.
Since the file can be owned & granted to the domain/customer/subscription.
For me the solution was the decision to apply inherited permissions at the VHost folder level, which already had [psaadm] on it.
Just switched from "This Folder Only"to "This folder, subfolders, and files". and changed from Deny to Allow.
I'm sure that's a
security risk for some reason (plesk , do you care to follow up on that question?).
But I see no alternative, other than maybe chaining executions of the above two scripts (either via php exec & runas, or plesk api to execute a custom event), to run the powershell script elevated.
And that folks is how you perform modifications to wordpress using the ever-lovely Wordpress Toolkit Extension.
Hope this helps someone save 3 hours of their life.
p.s. .. perhaps an alternative approach would be to simply modify plesk's temporary zip file of Wordpress which it downloads for future installs. And hope that plesk doesn't have an sha checksum running against it.