最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Overriding a function in a Wordpress plugin

programmeradmin2浏览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.

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.

Share Improve this question asked Nov 22, 2020 at 15:04 H. M..H. M.. 1135 bronze badges 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 Commented Nov 22, 2020 at 15:31
Add a comment  | 

1 Answer 1

Reset to default 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/manual/en/datetime.settimezone.php

发布评论

评论列表(0)

  1. 暂无评论