Use -d memory_limit=512M
to override the memory limit or other PHP parameters on the command line without modifying php.ini
:
php -d memory_limit=512M script.php
Use -d memory_limit=512M
to override the memory limit or other PHP parameters on the command line without modifying php.ini
:
php -d memory_limit=512M script.php
While trying to update Nextcloud using the command line (e.g. SSH) using a command like
php updater/updater.phar
you see an error message containing PHP Fatal error: Allowed memory size of ... bytes exhausted
such as this one:
[✔] Check for expected files [✔] Check for write permissions [✔] Create backup [✔] Downloading [ ] Verify integrity ...PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 155061456 bytes) in phar:///owncloud.mydomain.com/updater/updater.phar/lib/Updater.php on line 637
First, try to adjust the memory limit in your webhosting panel or php.ini
. If this is not possible – such as for my hoster, which has different settings for the FastCGI PHP as opposed to the command line (CLI) PHP, you can manually set the memory limit using
php -d memory_limit=512M updater/updater.phar
When trying to update your Matomo (previously called Piwik) installation, you see an error message like
Fatal error: Access level to Piwik\Plugins\ExampleTracker\Columns\ExampleActionDimension::configureSegments() must be public (as in class Piwik\Columns\Dimension) in /usr/www/users/user/piwik/plugins/ExampleTracker/Columns/ExampleActionDimension.php on line 57
Delete the plugins/ExampleTracker
folder in the Matomo installation folder. It contains an example plugin for an old version of Piwik that is not needed any more.
Original source for the fix: Matomo forums
This grep statement filters out the DB_NAME
, DB_USER
, DB_PASSWORD
and DB_HOST
contents from a wp-config.php
:
grep -oP "define\(['\"]DB_NAME['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" wp-config.php
For example, from the wp-config.php
line
define('DB_NAME', 'techoverflow');
it will extract techoverflow
.
Here are the statements for DB_NAME
, DB_USER
, DB_PASSWORD
and DB_HOST
:
DB_NAME=$(grep -oP "define\(['\"]DB_NAME['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" wp-config.php) DB_USER=$(grep -oP "define\(['\"]DB_USER['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" wp-config.php) DB_PASSWORD=$(grep -oP "define\(['\"]DB_PASSWORD['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" wp-config.php) DB_HOST=$(grep -oP "define\(['\"]DB_HOST['\"],\s*['\"]\K[^'\"]+(?=[\'\"]\s*\)\s*;)" wp-config.php)
You can use this, for example, in automatic backup scripts without manually copying the password to the backup script
Your WordPress installation running on Ubuntu 20.04 shows you this error message:
OTGS Installer, responsible for receiving automated updates for WPML and Toolset, requires the following PHP component(s) in order to function:cURL Learn more: Minimum WPML requirements
You need to install the curl
package for your PHP version.
Typically you can use
sudo apt -y install php7.4-curl
Doing this should immediately fix the issue (just reload your WordPress dashboard). In case you’re using PHP 7.2 you need to sudo apt -y install php7.2-curl
instead.
In case the issue is still not fixed after doing this, try restarting your webserver (e.g. sudo service apache restart
or sudo service nginx restart
), try restarting PHP-FPM by using sudo service php7.4-fpm restart
or sudo service php7.2-fpm restart
or try rebooting your server. In case that doesn’t help either, you need to manually check which PHP instance the wordpress site is using and install the curl package for that PHP instance.
Use this command to run php occ
inside a running docker Nextcloud container:
docker-compose exec -u www-data nextcloud php occ [command]
where:
nextcloud
is the name of your container in docker-compose-u www-data
tells docker-compose
to run the command as the www-data
user that owns all the Nextcloud files.[command]
by the php occ
command you want to run, for example versions:cleanup
. If you want to see a list of all available commands, just omit [command]
Note: You need to run this command in the directory where your nextcloud docker-compose.yml
is located.
Example:
docker-compose exec -u www-data nextcloud php occ versions:cleanup
or:
docker-compose exec -u www-data nextcloud php occ trash:cleanup --all-users
When trying to connect using the Nextcloud client, during the Flow v2 authorization step where you open a page in the browser to authorize the client, you see an error message in the JS console like
Refused to send form data to 'http://nextcloud.mydomain.com/login/v2/grant' because it violates the following Content Security Policy directive: "form-action 'self'".
Add
'overwriteprotocol' => 'https',
after this line:
'version' => '18.0.0.10',
in your nextcloud/config/config.php
.
WordPress uses dashicons as its primary icon font. However, many themes do not require dashicons on the frontend and hence it’s only used in the admin frontend, i.e. when a user is logged in.
I made a simple plugin to make WordPress load the dashicons CSS only if the user is logged in. For non-logged-in users, the dashicons CSS is removed from the frontend (hence improving the loading speed of your website).
<?php /* Plugin Name: TechOverflow remove dashicons */ /** * Remove dashicons CSS from the page, only load if user is logged in */ function dashicons_admin_only() { if(!is_user_logged_in()) { global $wp_styles; wp_dequeue_style('dashicons'); // wp_deregister_style('dashicons') causes internal PHP errors in WordPress ! $wp_styles->registered['dashicons']->src = ''; } } add_action( 'wp_print_styles', 'dashicons_admin_only' );
Just create an new folder called techoverflow-no-dashicons
inside your wp-content/plugins
directory, and save the source code listed above as wp-content/plugins/techoverflow-no-dashicons/functions.php
inside. After that, you can modify the code to fit your specific needs.
Note that using this plugin might make your site appear differently if you are logged in. Hence it’s important to check your site when not logged in, e.g. using incognito mode.
Many WordPress plugins provide you with an option to configure whether scripts are loaded in the header or the footer of the page.
If you want to move a script to the footer for performance reasons, and the plugin doesn’t support it, one option is to just edit the wp_enqueue_script()
call in the plugin’s source code and set $in_footer = true
.
However, these change won’t survive plugin updates and hence are not recommended for security reasons.
A better option is to write a custom plugin that removes the <script>
tag from the head and moves it to the footer. The following file is the functions.php of my custom plugin that moves the <script> tag from the cookie-law-info plugin to the bottom of the page.
<?php /* Plugin Name: TechOverflow Cookie Law to footer */ function postpone_script($name) { global $wp_scripts; // Get attributes from original script $thesrc = $wp_scripts->registered[$name]->src; $theversion = $wp_scripts->registered[$name]->ver; // Remove script from the header wp_dequeue_script($name); wp_deregister_script($name); // Add script to the bottom wp_enqueue_script($name, $thesrc, false /* no deps */, $theversion, true /* bottom */); } /** * Move cookie law javascript to the bottom */ function postpone_cookie_law() { postpone_script('cookie-law-info'); } add_action( 'wp_print_scripts', 'postpone_cookie_law' );
Just create an new folder called techoverflow-cookie-law-footer
inside your wp-content/plugins
directory, and save the source code listed above as wp-content/plugins/techoverflow-cookie-law-footer/functions.php
inside. After that, you can modify the code to fit your specific needs.
Remember to check your page for issues (javascript errors, delayed rendering of some elements) that appear when you move the javascript to the footer, since some plugins may not be compatible with the script being placed in the footer. Also, you might need to call postpone_script()
multiple scripts with different $name
argument. Check the original plugin’s wp_enqueue_script()
calls for the correct $name
(first argument). In case it’s not obvious which wp_enqueue_script()
call relates to a specific script, you can also check the version in the src
of the original <script>
tag (e.g. 1.8.2
in ...?ver=1.8.2
) and search for that version in the plugin’s folder to find the matching wp_enqueue_script()
call.
Use this snippet in your wordpress plugin to load a script only if a user is logged in:
function enqueue_my_script() { if(is_user_logged_in()) { wp_enqueue_script( 'my-script', plugins_url('/my-script.js', __FILE__), false, '1.0', 'all'); } } add_action( 'wp_enqueue_scripts', 'enqueue_my_script');
It does not matter how exactly you use wp_enqueue_script()
, just wrap it in a
if(is_user_logged_in()) { // Call wp_enqueue_script() here ! }
Also see:
Minimal WordPress CSS plugin example
Minimal WordPress Shortcode plugin example
This plugin adds a static Javascript .js
file to WordPress which is loaded in the client.
First, create a directory for your plugin in wp-content/plugins/
, e.g. wp-content/plugins/my-js-plugin
Save the following code as functions.php
in the plugin folder, e.g. wp-content/plugins/my-js-plugin/functions.php
<?php /* Plugin Name: My JS plugin */ function my_plugin_enqueue_js(){ wp_enqueue_script('my-plugin-js', plugins_url('/script.js', __FILE__), false, '1.0.0', true /* in footer */); } add_action('wp_enqueue_scripts', "my_plugin_enqueue_js");
Next, save your desired JS file in script.js
in the plugin folder, e.g. wp-content/plugins/my-js-plugin/script.js
. Example script:
jQuery(document).ready(function() { console.info("Your JS plugin works!"); });
Now activate your plugin your WordPress admin area.
Your Javascript will be loaded on each WordPress page until you deactivate the plugin.
Note that if you are using a JS-optimizing plugin like Autoptimize, you might not actually see your JS file as separately loaded script file as it is compiled into the single Autoptimize JS. You javascript will still be loaded on the client!
Also see:
Minimal WordPress JS plugin example
Minimal WordPress Shortcode plugin example
This plugin adds a static CSS file to WordPress.
First, create a directory for your plugin in wp-content/plugins/
, e.g. wp-content/plugins/my-css-plugin
Save the following code as functions.php
in the plugin folder, e.g. wp-content/plugins/my-css-plugin/functions.php
<?php /* Plugin Name: My CSS plugin */ function my_plugin_enqueue_css(){ wp_enqueue_style('my-plugin-stylesheet', plugins_url('/style.css', __FILE__), false, '1.0.0', 'all'); } add_action('wp_enqueue_scripts', "my_plugin_enqueue_css");
Next, save your desired CSS file in style.css
in the plugin folder, e.g. wp-content/plugins/my-css-plugin/style.css
. Example for a stylesheet:
/* This is just an example CSS and does not have any specific meaning! */ .my-plugin-class { font-weight: bold; }
Now activate your plugin your WordPress admin area.
Your CSS will be loaded for each WordPress page until you deactivate the plugin.
Note that if you are using a CSS-optimizing plugin like Autoptimize, you might not actually see your CSS file as separately loaded stylesheet as it is compiled into the single Autoptimize CSS. You style will still be loaded!
Also see:
Minimal WordPress JS plugin example
Minimal WordPress CSS plugin example
This plugin creates a simple (static – no parameters) shortcode in WordPress
First, create a directory for your plugin in wp-content/plugins/
, e.g. wp-content/plugins/my-shortcode-plugin
Save the following code as functions.php
in the plugin folder, e.g. wp-content/plugins/my-shortcode-plugin/functions.php
<?php /* Plugin Name: My shortcode plugin */ function my_shortcode( $atts , $content = null ) { return '<h2>Shortcode works</h2>'; } add_shortcode( 'my-shortcode', 'my_shortcode' );
Now activate your plugin your WordPress admin area.
You can now create a post or page containing this code:
[my-shortcode][/my-shortcode]
which will be rendered like this:
Also see:
Minimal WordPress JS plugin example
Minimal WordPress CSS plugin example
Minimal WordPress Shortcode plugin example
This is the minimal wordpress plugin – it does not do anything at all, but you can activate it and use it as a basis for your plugins.
First, create a directory for your plugin in wp-content/plugins/
, e.g. wp-content/plugins/my-plugin
Save the following code as functions.php
in the plugin folder, e.g. wp-content/plugins/my-plugin/functions.php
<?php /* Plugin Name: My plugin */
Now you can activate your plugin your WordPress admin area:
The WordPress function the_date()
and get_the_date()
always return the date / time in the locale format defined by the language setting of the current WordPress installation.
What if you need to get the date in a specific locale, e.g. english?
Setting the wordpress language to the target locale will successfully achieve this, but will also change the language of other parts of WordPress and is therefore often not an option.
In case you can’t do that and you have to find a programmatic solution, this is my way to force
<?php the_date('r', '', '', TRUE); ?>
to a specific locale ("C"
i.e. plain english in this case)
<?php setlocale(LC_TIME, "C"); // Set to target locale (in which you want the date to be formatted echo strftime("%a, %d %b %Y %H:%M:%S %z", get_post_time('U', TRUE)); // Parse wordpress time and format it again setlocale(LC_TIME, "de_DE"); // Set back to the original locale! ?>
Since the_date()
ignores setlocale()
calls, we use PHP’s strftime()
to work around this.
First, we set the target tocale (the locale for the date to be formatted in) using setlocale(LC_TIME, "C");
Replace "C"
by your target locale! "C"
is a good choice if you want plain english.
Then, we get the post date & time (the same date & time that is used / returned by the_date()
& get_the_date()
) using get_post_time('U', TRUE);
. "U"
means return as Unix timestamp. TRUE
is very important here since it tells get_post_time()
to return the timestamp as UTC. If you don’t use TRUE
here, your dates will be offset by several hours (depending on your timezone) in case they are not UTC already.
After that, we run strftime()
to format the timestamp. You need to insert your desired format string here. In my case, the format string is "%a, %d %b %Y %H:%M:%S %z"
which is a RFC2822 date. Note that using this method, the timezone (%z
) will always be +0000
since it’s formatted as a UTC date. However, the timezone offset will be correctly accounted for.
As a last step, we re-set the original locale using setlocale(LC_TIME, "de_DE");
. This avoids affecting other function, e.g. in other plugins. You need to insert the correct (original) locale here. In my case, I know the correct locale is "de_DE"
, but in your case this may differ.
Recently my Nextcloud 16 instance (running via PHP 7.2 FPM on Nginx) return HTTP status 500 (internal server error) when trying to access it.
Analyzin the webserver log revealed this error message:
FastCGI sent in stderr: "PHP message: PHP Fatal error: Class OC\Authentication\Token\PublicKeyToken contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (OC\Authentication\Token\IToken::setExpires) in /var/sites/nextcloud.mydomain.com/lib/private/Authentication/Token/PublicKeyToken.php on line 47" while reading response header from upstream, client: 2003:ea:b747:ee00:7491:f492:480:57a9, server: nextcloud.mydomain.com, request: "PROPFIND /remote.php/dav/files/admin/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "nextcloud.mydomain.com"
After some research, I found out that – at least in my case – the issue could be fixed by flushing the PHP opcache:
sudo service php7.2-fpm reload
After that, nextcloud started to work properly again.
You are writing a wordpress plugin in which you need the path to the wp-content directory on the filesystem.
Use the WP_CONTENT_DIR
constant.
$path_to_wp_content = WP_CONTENT_DIR; // e.g. "/var/sites/techoverflow.net/wp-content"
Note that WP_CONTENT_DIR has no trailing slash.
Use WP_CONTENT_DIR . "/"
like this
$path_to_wp_content = WP_CONTENT_DIR . "/"; // e.g. "/var/sites/techoverflow.net/wp-content/"
to get the path to wp_content
including a trailing slash.
In your custom WordPress plugin or theme you have code like
$query = new WP_Query( ); while ( $query->have_posts() ) { $query->the_post(); // ... your code to process the post ... }
But $query->have_posts()
is always false and the loop is never executed.
WP_Query
works only if you use an appropriate query. Using no query at all is not equivalent to list all posts!
This is likely what you want to do:
$query = new WP_Query(array('post_type' => 'post'));
The following script shows you the absolute path on the webserver which often can’t be found using FTP alone.
<?php /* path.php */ list($scriptPath) = get_included_files(); echo $scriptPath; ?>
Upload this script to your webspace using FTP and then access it using the browser. It will show you a path like
/var/www/httpdocs/webmail.techoverflow.net/path.php
There are well-documented solutions to recover the wordpress admin access if you have MySQL, phpMyAdmin or shell access.
This solution shows you how to create a new admin user if you don’t know the admin username or password and you don’t have any form of MySQL or shell access (only FTP access is required).
This can be done by looking at the source code of your homepage (go to your domain, then Ctrl+U to show the source) and then Ctrl+F-search for wp-content/themes
. It will show hits like https://mydomain.de/wp-content/themes/twentyfifteen/style.css
. This means the currently active theme is twentyfifteen
.
functions.php
Now open your FTP software (I recommend FileZilla) and find your wp-content folder. Inside wp-content, go to themes and then open the folder of your currently active theme.
If the currently active theme is not listed in your wp-content/themes folder, you might have the wrong wp-content folder. Check if there are any other folders around.
When you have found your theme folder, edit functions.php and, just after the first <?php
, add this block of code:
function wpb_admin_account(){ $user = 'newadmin'; $pass = 'saiquae9shahZus6eeri3feNae8gie'; $email = '[email protected]'; if ( !username_exists( $user ) && !email_exists( $email ) ) { $user_id = wp_create_user( $user, $pass, $email ); $user = new WP_User( $user_id ); $user->set_role( 'administrator' ); } } add_action('init','wpb_admin_account');
Be sure to replace the username, password and email! I recommend to use a new random password and a non-generic username! The username you enter here must.
Save the file and upload it to the server.
After that, goto your homepage and reload once (this will create the new user).
Then, try to login using your newly created user (go to e.g. https://my-domain.com/wp-admin
to do so!). If it doesn’t work, check if you edited the functions.php
for the correct theme and try to use a different username!
If you just leave in the code, this will create a potential security risk. So I recommend deleting it right away!
Also, be sure to delete any admin users you don’t need afterwards.