Fetch PHP memory limit

WordPress’ memory limit can be independently set to anything however will not go over PHP’s own limit. This will retrieve PHP’s memory limit. Will attempt to retrieve from default location for Kinsta sites then fallback to what PHP’s loaded ini file. Please note that sometimes there is a difference between CLI’s memory limits and web request limits.

php_version=$( php -v | head -n1 | cut -d ' ' -f 2 | cut -d '.' -f 1,2 )
if [ -f "/etc/php/$php_version/fpm/php.ini" ]; then
    php_memory=$( cat "/etc/php/$php_version/fpm/php.ini" | grep "memory_limit" )
fi
if [[ "$php_memory" == "" ]]; then
    php_ini=$( wp eval 'echo php_ini_loaded_file();' )
    php_memory=$( cat "$php_ini" | grep "memory_limit" )
fi

read -r -d '' php_code << heredoc
\$line = '$php_memory';
preg_match( '/memory_limit = (\d\w+)/', '$php_memory', \$matches );
echo \$matches[1];
heredoc

php -r "$php_code"