At this time When I am writing this post, we have PHP 8 as the latest version of PHP and WordPress 5.8 as the latest version of WordPress.
After these releases of PHP 8 and WordPress 5.8, many WordPress users approached us mentioning a new WordPress error that is
“Deprecated: required parameter $variable follows optional parameter $variable in any File on any line”.
Just by reading the error, we were sure that this was about PHP and finally, we confirmed that if they had updated PHP Version. The answer was Yes, they all updated to PHP 8.
In this blog, we will tell you how you can fix this deprecated error and also how you can avoid deprecated errors in the future. We will try to keep it as simple as possible so that users from a non-developer background can also fix their websites and scripts.
Reason for error that is Deprecated: required parameter $variable follows optional parameter $variable
The first thing we found was that all WordPress users had updated to PHP 8.
Themes developers didn’t release the update for themes to be compatible with PHP 8 and users were too excited to move on to PHP 8.
So users updated to PHP 8 without checking the compatibility of their WordPress themes with PHP 8.
Users updated to PHP 8 but the theme code wasn’t updated in accordance with PHP 8 which triggered this error. So this was the reason for this WordPress error in simple terms.
In technical terms, we have an update in PHP 8 that the required parameter should be before the optional parameter. This wasn’t an issue in earlier versions of PHP.
This declaration can be anywhere in PHP Scripts, in WordPress Plugins, or in any WordPress Theme File. So if you have any function where Required Parameter is declared after Optional Parameter then you will see this deprecated error but only if you have PHP 8, earlier versions of PHP don’t trigger ṭhis error.
The solution to “Deprecated: required $parameter follows optional $parameter”
For those WordPress users who don’t know PHP and are not developers switch back to PHP 7.4 / Change PHP Version using cPanel or control panel whatsoever you have. Moving back from PHP 8 would fix this issue.
Those who are from the developer background and practice in PHP can watch this video.
If you are seeing this error that clearly means you have any or some functions in which you have declared an optional parameter before the required one.
Example The From Video:- We are seeing an Error stating “Deprecated: required parameter $width follows optional parameter $attach_id in …….path of the file….. line …”.
From the statement itself, You can see the required parameter is $width and the optional parameter is $attach_id.
We log in to the cPanel to access the file in accordance with the path specified in the error we see on the screen. Checked the line specified in the error and confirmed the same in that function.
Now we had 2 choices either we switch back to PHP 7.4 which was on the safer side and another was to make corrections in the function/declaration itself.
So here is the first code which triggered the error:-
function ct_resize($attach_id = null, $width, $height, $img_url = null, $crop = false )
Here is what we updated
function ct_resize($width, $height, $attach_id = null, $img_url = null, $crop = false )
In the 1st code above, you can clearly see we have $Null declared before Variables $width and $height.
In the updated code, we have replaced $attach_null, by placing it after $width and $height.
This fixed the error. Then we checked the website completely to see if any other warning or error is triggered with the change.
This is really important that if you make changes in PHP Scripts then you should always check out that everything is working.
Fortunately, we were able to fix this by updating the code. This error was not a major one and also we were aware of the update.
One more point is that it was only in one file. But if you see it on multiple files in multiple lines then I would recommend you not to change the code. Switching the PHP version to before PHP 8 would be better in that case.
The moral of the blog is that never change the PHP version unnecessarily without checking the compatibility of the theme and plug-ins. In WordPress compatibility is one of the factors which you have to consider before updating anything. If you miss out on anything related to compatibility then it can be an issue.
We hope this article will help you!