0

Whats the best way to create a wordpress plugin. I have started out learning to make plugins and i want to make a plugin to calculate 2 numbrs and save to a mysql database.

since i didnt know OOP in PHP i decided to code what i know.

my code looks like this only my instructor wants me to do something Awesome.. better than what i coded earlier.

Edit

Now i am trying to make this add and then send to the database.

Code looks like this now

  <?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');
?>
5
  • What is your issue/question? Does your code work? I mean, "my instructor wants me to do something Awesome.. better than what i coded earlier" isn't particular helpful and doesn't tell us anything about anything. Shouldn't your instructor be the one that helps you with it? Jul 27, 2017 at 12:46
  • @MagnusEriksson My instructor wants me not to use this method to save to a mysql database after Adding using wordpress plugins, he wants me to code something and send to a database after addition using wordpress plugins . he wants to see how far i can go first before he helps out. Jul 27, 2017 at 12:47
  • Sorry, but that comment doesn't clarify anything. Just saying that you want to do it differently or "Awesome" isn't good enough. We're not here to refactor your code for you. A tip, check out Wordpress documentation about doing database calls and use their existing classes instead. Here's the link: codex.wordpress.org/Class_Reference/wpdb - Learning OOP would be another good start. Jul 27, 2017 at 12:51
  • @MagnusEriksson where can i get materials for OOP Jul 27, 2017 at 13:05
  • Just google something like "learn php oop" and find some tutorials and guides that you can read and do. Jul 27, 2017 at 13:10

1 Answer 1

1

you are using php for inserting dta into database. use wordpress functions to insert data into data.

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' 
    ) 
);

for more information how to insert or get data click here

2
  • thanks prince, i figured.Now i am trying to figure how to send it to the database... like how to i trigger it. Jul 27, 2017 at 13:29
  • welcome James, click on the link, there you can get more information.
    – Prince
    Jul 27, 2017 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.