WordPress短代码未更新动态数据。

huangapple go评论56阅读模式
英文:

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-&gt;donationProgressShortCode,  array($this,&#39;progressBarShortCode&#39;));


 function progressBarShortCode($params){


   if(!isset($params[&#39;id&#39;]) || !$params[&#39;id&#39;]){
     return &#39;ID NOT PROVIDED&#39;;
   }

   $model = new Donation();
   $donation = $model-&gt;getDonation($params[&#39;id&#39;]);

   if(!$donation){
     return;
   }

   $target = $donation-&gt;target;
   if($target &lt;= 0){
     return;
   }

   $total = 0;
   $percentage = 0;


   $transactions = $model-&gt;getTransactions($params[&#39;id&#39;]);
   if($transactions){
     foreach($transactions as $item){
       // error_log(&#39;ITEMS HERE MAN: &#39;.json_encode($item));
       $total+= $item-&gt;amount; 
     }
   }

   if($total &gt; 0){
     $percentage = ($total * 100) / $target;
   }


   wp_enqueue_style(&#39;tz_payment_gateway_style&#39;, $this-&gt;pluginUrl . &#39;assets/style/checkout-style.css&#39;);



   $content = &#39;&lt;div style=&quot;width: 100%; padding: 2px&quot;&gt;
  &lt;div style=&quot;display: flex; justify-content-between; font-weight: bold&quot;&gt;
    &lt;div style=&quot;width: 100%;&quot;&gt;&#39;.$total.&#39;&lt;small&gt;&#39;.$donation-&gt;currency.&#39;&lt;/small&gt;&lt;/div&gt;
    &lt;div style=&quot;width: 100%; text-align: right&quot;&gt;&#39;.$target.&#39;&lt;small&gt;&#39;.$donation-&gt;currency.&#39;&lt;/small&gt;&lt;/div&gt;
  &lt;/div&gt;  
  &lt;div style=&quot;position: relative; background-color: #ccc9; border-radius: 5px; overflow: hidden; height: 18px&quot;&gt;
    &lt;div style=&quot;transition: 11.5s all ease-in; height: 18px; background-color: &#39;.$donation-&gt;background_color.&#39;; position: absolute; width:&#39;.($percentage&gt; 100? 100: $percentage).&#39;%; color: transparent&quot;&gt;.&lt;/div&gt;
    &lt;div style=&quot;height: 18px; line-height: 18px; text-shadow: 1px 1px 2px black; color: #fff; width: 100%; position: absolute; text-align: center;&quot;&gt;&#39;.$percentage.&#39;%&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;&#39;;



   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.
WordPress短代码未更新动态数据。

答案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.

huangapple
  • 本文由 发表于 2023年4月11日 03:05:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定