In the last few days, 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”.
After reading the error, we were sure that this was about PHP and finally, we confirmed if users 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 Deprecated: required parameter $variable follows optional parameter $variable
We found 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.
So those who updated the PHP version to PHP 8 and their themes were not updated in accordance with PHP 8 were seeing 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 then it 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 option 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.
So never change the PHP version unnecessarily without checking the compatibility of themes 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!