0

Is it possible to override a function being called by a Wordpress plugin? Working on customizing a Wordpress plugin, I need to make some changes in some code blocks. Among the most critical blocks that I am going to modify are the functions that deal with saving and displaying dates. As our local date is different from the Gregorian date, the date format that the plugin is based upon. Hence, I need to override some parts of the plugins to meet the local needs. I have changed the codes inside the main plugin itself, yet as you all know, its not a good practice to change the codes inside the original plugin since the moment the plugin is updated, the modified files get overwritten by the original ones, and hence, all the modified code gets deleted. So, I am going to write a new plugin to do this process all automatically, and there is no worries for the times that the main plugin gets updated.

To recap, inside the new plugin I am going to tell Wordpress that whenever the main plugin calls up the following function, just take its parameter and then run it though another function, for instance, displayDateLocal($id). Just take a brief look at this code.

function displayDate ($id) {
$userActivityDate = Br::show_activity_date($id);
    $return = apply_filters('user_activity', $userActivityDate);}

As you can definitely guess in the second line of the function, it just calls another function, that is, 'show_activity_date'. This comes up with Gregorian date, which is not appropriate for our local purposes, and accordingly this line needs be modified if it going to be localized and useful in our case. Hence, I need to tell Wordpress that whenever displayDate($id) is called, just get the parameter and run it through another function, called, for example, displayLocalDate(). Inside this function, I have changed the date structure, and everything is fine if I am able to do this to override the code in the original function in the main plugin. Thanks in advance.

1
  • Is that the original code? The third line of the function calls the user_activity filter which is there for you to hook, although it gives you the $userActivityDate to work with not $id if that's what you need. – Rup Nov 22 '20 at 15:31
0

If you must use the 3rd party function - why not just reformat the returned value?

Date ($id) {
   $userActivityDate = Br::show_activity_date($id);
   date_timezone_set($userActivityDate, timezone_open('timezone You want it to be in'));
   date_format($userActivityDate, 'Y-m-d H:i:sP');
   $return = apply_filters('user_activity', $userActivityDate);
}

If the return value of the Br::show_activity_date is not directly useful in this manner, then I suggest you update your question to include what it returns, in order to provide a more complete answer.

https://www.php.net/manual/en/datetime.settimezone.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.