PHP

How to fix Ubuntu PHP Call to undefined function mb_internal_encoding()

Problem:

When you see the following error message in your PHP log:

2022/12/22 12:57:20 [error] 1908680#1908680: *9092980 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to undefined function mb_internal_encoding() in /var/www/snappymail/v/0.0.0/include.php:90
Stack trace:
#0 /var/www/index.php(11): include()
#1 {main}
  thrown in /var/www/snappymail/v/0.0.0/include.php on line 90" while reading response header from upstream, client: ::ffff:77.7.108.195, server: mydomain.com, request: "GET /?admin HTTP/2.0", upstream: "fastcgi://unix:/var/run/php/php8.2-fpm.sock:", host: "mydomain.com"

Solution:

The missing function mb_internal_encoding() is from the PHP mbstring module.

In order to install it, first you need to identify the PHP version running the given script. This can be done either by looking at the webserver config files for the given domain, or by looking in the error log. In this case, we can identify that the PHP version is 8.2 from upstream: "fastcgi://unix:/var/run/php/php8.2-fpm.sock:"

In case you can’t identify the correct version, I recommend installing mbstring for every installed PHP version, which you can see by using sudo dpkg --get-selections | grep php.

Given the version number, we can install the mbstring extension from the Ubuntu/Debian package sources:

sudo apt -y install php8.2-mbstring

Depending on your configuration you might also need to restart the webserver and/or PHP-FPM service. I recommend to try it out without restart, then restart the webserver and (if you are using PHP-FPM) restart the PHP-FPM for your PHP version. In case that doesn’t help, you can always reboot.

After that, the error message should have disappeared – sometimes you need to install additional PHP modules and it’s best to just watch the error log for more missing functions or other errors related to missing extension.

Posted by Uli Köhler in PHP

How to override PHP memory limit on the command line

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

 

Posted by Uli Köhler in PHP

How to fix Nextcloud updater PHP Fatal error:  Allowed memory size of … bytes exhausted

Problem:

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

Solution:

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

 

Posted by Uli Köhler in Networking, Nextcloud, PHP

How to fix Matomo/Piwik update configureSegments() must be public

Problem:

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

Solution:

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

Posted by Uli Köhler in PHP

How to grep for WordPress DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php

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

Posted by Uli Köhler in PHP, Wordpress

How to fix “OTGS Installer, responsible for receiving automated updates for WPML and Toolset, requires the following PHP component(s) in order to function:cURL” on Ubuntu 20.04

 Problem:

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

Solution:

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.

 

Posted by Uli Köhler in PHP, Wordpress

How to run Nextcloud “php occ” in a docker-compose configuration

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.
  • Replace [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

 

Posted by Uli Köhler in PHP

How to fix Nextcloud Refused to send form data to /login/v2/grant because it violates the following Content Security Policy directive: form-action ‘self’

Problem:

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'".

Solution:

Add

'overwriteprotocol' => 'https',

after this line:

'version' => '18.0.0.10',

in your nextcloud/config/config.php.

Posted by Uli Köhler in Nextcloud, PHP

How to remove dashicons CSS from WordPress

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.

Posted by Uli Köhler in PHP, Wordpress

WordPress: How to move script to footer if plugin doesn’t support it

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.

Posted by Uli Köhler in PHP, Wordpress

WordPress: Enqueue script only if user is logged in

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 !
}

 

Posted by Uli Köhler in PHP, Wordpress

Minimal WordPress JS plugin example

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!

Posted by Uli Köhler in PHP, Wordpress

Minimal WordPress CSS plugin example

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!

Posted by Uli Köhler in PHP, Wordpress

Minimal WordPress Shortcode plugin example

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:

Shortcode works

Posted by Uli Köhler in PHP, Wordpress

Minimal wordpress plugin example

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:

Posted by Uli Köhler in PHP, Wordpress

How to force the_date() / get_the_date() to a specific locale

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.

Posted by Uli Köhler in PHP, Wordpress

How I fixed Nextcloud PHP Fatal error: Class contains 1 abstract method and must therefore be declared abstract

 

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.

Posted by Uli Köhler in Nextcloud, PHP

How to get path to the wp_content directory in WordPress plugin

Problem:

You are writing a wordpress plugin in which you need the path to the wp-content directory on the filesystem.

Solution:

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.

Posted by Uli Köhler in PHP, Wordpress

How to fix WP_Query not listing any posts

Problem:

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.

Solution:

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'));
Posted by Uli Köhler in PHP, Wordpress

How to find absolute path on webserver using PHP

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
Posted by Uli Köhler in PHP