创建 wordpress 插件的最佳方式。我已经开始学习制作插件,我想制作一个插件来计算 2 个数字并保存到 mysql 数据库。
因为我不知道 PHP 中的 OOP,所以我决定编写我所知道的代码。
我的代码看起来像这样只有我的教练要我做的东西真棒..比我之前编码的更好。
Edit现在我试图使这个添加,然后发送到数据库。
代码现在看起来像这样
<?php
/*
Plugin Name: Addition DB Plugin
Description: Plugin to Add to Database and call back from database
Author: James Ibrahimov
*/
global $wpdb;
function add2numbersandsavetoDB()
{
$num1 = '5';
$num2 = '11';
$ans = $num1 + $num2;
$wpdb -> insert('answer',array(
'id' => 'id',
'ans'=> $ans
),
array('%d','%s') );
}
add_action('main_menu', 'add2numbersandsavetoDB');
function displayDatafromDB()
{
$results = $wpdb-> get_results("select * from answer");
foreach($results as $result){
echo $result-> id;
echo $result-> ans;
}
}
add_action('main_menu','displayDatafromDB');
?>
您正在使用 php 将 dta 插入数据库。使用 wordpress 函数将数据插入数据。
change
$host = '127.0.0.1';
$user = 'root';
$pass = '';
$db = 'wordpress_tut';
to
global $wpdb;
use this to insert your data
$wpdb->insert(
'table_name',
array(
'column1' => 'value1',
'column2' => 123
),
array(
'%s',
'%d'
)
);
更多信息如何插入或获取数据click here
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(3条)