我正在制作自己的 WP 插件,它有一個包含一些模板的文本欄位,我將其保存到資料庫中(表 = 模板,欄位 = 模板),如下所示:
<div class="myclass">
<p>Hello {$user_name}</p>
</div>
然后我試圖向用戶顯示一條訊息:
<php
$user_name = "Bob";
$template_query = 'select * from ' . $wpdb->get_blog_prefix();
$template_query .= 'templates where id='.$some_id;
$template_data = $wpdb->get_row( $template_query, ARRAY_A );
$template = $template_data['template'];
echo $template;
?>
它是輸出“Hello {$user_name}”而不是“Hello Bob”
我不知道如何將 PHP 變數傳遞給 HTML 拋出資料庫。
我會感謝任何幫助。
uj5u.com熱心網友回復:
從資料庫$template中將只包含一個純文本字串,而不是 PHP。您必須通過 PHP將占位符替換為{$user_name}in$template的值。$user_name
例如:
代替
echo $template;
嘗試:
echo str_replace('{$user_name}', $user_name, $template);
這將輸出:
Hello Bob
另請注意,您的開始標簽?中缺少。<?php
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/486492.html
下一篇:C 函式模板實參推導指南
