英文:
WordPress shortcode not updating dynamic data
问题
我正在构建一个与捐赠有关的插件,并添加了一个进度条,其值必须从数据库中读取。
例如,如果我在/sample-page/
上添加了我的短代码,并且数据库中的值在那一刻是1000
,即使我更新了数据库中的值,只要URL没有更改,例如/sample-page/
,显示的值不会改变,除非URL发生更改,例如/smaple-page/?something=s
,然后它会加载新内容,如果数据库中的值再次更改,我需要更改URL才能呈现最新内容。
请帮忙。
您可以在下面找到代码:
add_shortcode( $this->donationProgressShortCode, array($this,'progressBarShortCode'));
function progressBarShortCode($params){
if(!isset($params['id']) || !$params['id']){
return '未提供ID';
}
$model = new Donation();
$donation = $model->getDonation($params['id']);
if(!$donation){
return;
}
$target = $donation->target;
if($target <= 0){
return;
}
$total = 0;
$percentage = 0;
$transactions = $model->getTransactions($params['id']);
if($transactions){
foreach($transactions as $item){
$total+= $item->amount;
}
}
if($total > 0){
$percentage = ($total * 100) / $target;
}
wp_enqueue_style('tz_payment_gateway_style', $this->pluginUrl . 'assets/style/checkout-style.css');
$content = '<div style="width: 100%; padding: 2px">
<div style="display: flex; justify-content-between; font-weight: bold">
<div style="width: 100%">'.$total.'<small>'.$donation->currency.'</small></div>
<div style="width: 100%; text-align: right">'.$target.'<small>'.$donation->currency.'</small></div>
</div>
<div style="position: relative; background-color: #ccc9; border-radius: 5px; overflow: hidden; height: 18px">
<div style="transition: 11.5s all ease-in; height: 18px; background-color: '.$donation->background_color.'; position: absolute; width:'.($percentage> 100? 100: $percentage).'%; color: transparent"></div>
<div style="height: 18px; line-height: 18px; text-shadow: 1px 1px 2px black; color: #fff; width: 100%; position: absolute; text-align: center;">'.$percentage.'%</div>
</div>
</div>';
return $content;
}
我的做法基本上是从数据库中读取所有已支付的交易,然后根据目标金额计算百分比,然后将结果呈现为进度条,如下图所示。
英文:
I am building a plugin which has to do with donations and I added a progress bar whose value has to be read from the database.
For example, if I added my shortcode on /sample-page/
and the value at that moment in the database is 1000
, even if I update the value in the database, the value showing on /sample-page/
does not change unless the url change for example /smaple-page/?something=s
then it would load new content and then if the value changes in the database again, I'll need to change the URL for it to render the latest content.
Please help
You can find the code below
add_shortcode( $this->donationProgressShortCode, array($this,'progressBarShortCode'));
function progressBarShortCode($params){
if(!isset($params['id']) || !$params['id']){
return 'ID NOT PROVIDED';
}
$model = new Donation();
$donation = $model->getDonation($params['id']);
if(!$donation){
return;
}
$target = $donation->target;
if($target <= 0){
return;
}
$total = 0;
$percentage = 0;
$transactions = $model->getTransactions($params['id']);
if($transactions){
foreach($transactions as $item){
// error_log('ITEMS HERE MAN: '.json_encode($item));
$total+= $item->amount;
}
}
if($total > 0){
$percentage = ($total * 100) / $target;
}
wp_enqueue_style('tz_payment_gateway_style', $this->pluginUrl . 'assets/style/checkout-style.css');
$content = '<div style="width: 100%; padding: 2px">
<div style="display: flex; justify-content-between; font-weight: bold">
<div style="width: 100%;">'.$total.'<small>'.$donation->currency.'</small></div>
<div style="width: 100%; text-align: right">'.$target.'<small>'.$donation->currency.'</small></div>
</div>
<div style="position: relative; background-color: #ccc9; border-radius: 5px; overflow: hidden; height: 18px">
<div style="transition: 11.5s all ease-in; height: 18px; background-color: '.$donation->background_color.'; position: absolute; width:'.($percentage> 100? 100: $percentage).'%; color: transparent">.</div>
<div style="height: 18px; line-height: 18px; text-shadow: 1px 1px 2px black; color: #fff; width: 100%; position: absolute; text-align: center;">'.$percentage.'%</div>
</div>
</div>';
return $content;
}
What I do is basically read all paid transactions from the database then calculate the percentage based on the target amount, then render the result as a progress bar as seen in the image below.
答案1
得分: 1
这需要使用JavaScript在浏览器上定期执行一个请求,以向您的服务器发送请求。您可能需要设置一个自定义的REST API端点,以通过您的短代码接收来自浏览器的请求。REST API端点会响应最新更新的数值,然后您的JavaScript会使用这个新数值更新网页。
服务器无法定期自动向浏览器推送数据更新。浏览器必须定期向服务器发送请求。这就是为什么使用不同的URL来更新捐款有效。浏览器会向服务器发出另一个请求。
英文:
This requires having JavaScript execute a request from the browser to your server at a regular interval. You will likely need to set up a custom REST API endpoint to receive requests from the browser via your shortcode. The REST API endpoint would respond with the latest updated value, and your JavaScript would update the web page with this new value.
The server cannot regularly and automatically push data updates to the browser. The browser must regularly send requests to the server. This is why using a different URL worked to update the donations. The browser was making another request to the server.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论