0

I have a built in form I created on my WP website . I want to store the inputs to a table on my DB. All tutorials that explane how to create a talke include the fact that I have to create a plugin for that.

All my functions are written in functions.php and I prefer to write the function that creates the DB there too.

If I write the function that creates the table in functions.php - will it make multiple tables or something like that? or does it work only once?

can I just create one using PHPMYADMIN ? or it will not include the built-in wordpress security ect'?

2

When I started out with Wordpress I was anti-plugin. I wanted to add everything in my theme's functions.php. When I looked at the bigger picture it began making sense having and leaving some functionalities inside a plugin.

There are many write-ups on the subject of what should go into a plugin and what should go into a theme. I, for one, have done one or two posts on this subject, so do yourself the favor and read up on this. I believe it will be very helpful and insightful.

Functions in a plugin and functions in a theme's functions.php does exactly the same job, there is no difference. The only thing is, you cannot declare a function in plugin and redeclare it in your theme. This will throw a fatal error crashing the site

To answer your issue, yes, if you think logically about this, your functionality should go into a plugin as this gives functionality to your site, and not the theme. This is functionality that you would need when you switch themes. It is just easier in plugin as you don't need to write or copy this code over and over again into a new theme.

But the choice is still yours. Adding it to your theme is your own choice, although, as I said, not really recommended. But either way, the code will work perfectly fine in a theme and in a plugin, so the choice is yours to make where to add it

  • Hey, Thanks a lot for your comment. Defiantly helped me to understand the big picture. can you give me links to the things you mentioned that you wrote? – Kar19 Aug 3 '15 at 13:33
  • 1
    I'm just quicly tied up. Will post those links asap. :-) – Pieter Goosen Aug 3 '15 at 14:07
  • This is the first one that came to mind. In that specific post is a link to an excellent post which will answer a lot of questions you might have ;-) – Pieter Goosen Aug 3 '15 at 15:56
1

I didn't do this recently but in 2014, in a few WordPress projects that I need additional tables I used Laravel components.

In WordPress, if you want to create your own tables by using wpdb and mysql queries. But with the Laravel database classes I managed to do this much more elegantly.

A quick rundown how I do this: (note: At the time, I used Laravel 4 components, I haven't tried with 5)

"require": {
    "illuminate/database": "*",
    "illuminate/container": "*",
    "shuber/curl": "dev-master",
    "sunra/php-simple-html-dom-parser": "v1.5.0"

},

I installed the necessary packages (first two) with composer in functions.php (or in a functionality plugin)

I used it for saving an index of magazine articles content, a Recorder class (not very SOLID approach, sorry)

<?php
namespace Dion\Volumes;

use Illuminate\Database\Capsule\Manager as Capsule;


class Recorder {


    public static $table = 'volume_content';

    public function __construct(Page $page){
        $this->recordPage($page);
    }

    public static function createTables()
    {
        if( ! Capsule::schema()->hasTable(static::$table) ) {

            Capsule::schema()->create(static::$table, function($table) {
                $table->increments('id');
                $table->string('volume');
                $table->integer('month');
                $table->integer('year');
                $table->integer('page');
                $table->longtext('page_content');
            });
        }
    }


    public function recordPage($page)
    {
        //check if exists
        $exists = Capsule::table(static::$table)->where('volume',$page->volume)
                                    ->where('page',$page->page)->get();


        if( empty( $exists ) ) {

            $insert = Capsule::table(static::$table)->insert($page->toArray());
        } else {
            $update = Capsule::table(static::$table)->where('volume',$page->volume)
                                                    ->where('page',$page->page)
                                                    ->update($page->toArray());
        }
    }
}

and in the functions.php I added the Laravel database component and made it work like this.

global $wpdb;
$capsule = new Capsule;

$capsule->addConnection(array(
    'driver'    => 'mysql',
    'host'      => DB_HOST,
    'database'  => DB_NAME,
    'username'  => DB_USER,
    'password'  => DB_PASSWORD,
    'prefix'    => $wpdb->prefix,
    'charset'   => DB_CHARSET,
    'collation' => 'utf8_unicode_ci',
));


$capsule->bootEloquent();

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

//this one creates table if not exists. Its better to run it in a WP action, like admin_init
$createVolumes = Dion\Volumes\Recorder::createTables();

Hope it helps, if you like this solution and have questions, I'll be happy to add more.

1

can I just create one using PHPMYADMIN ?

Yes.You can. And its a shortest way to do it.

  • Will it make any security problems? And if I update to a newer WP version - will it remove the table? What are the consequences? Should I make sure I'm using the right prefix? – Kar19 Aug 3 '15 at 13:53
  • I don't see any problems here. WP woudn't touch the table (since it's not native). As for prefix - better to use it (at least just for unification). But its all up to you to use it as "{$wpdb->prefix}tblname", or 'sometablename'... IMHO most easiest way to declare name of table in $wpdb object and use a referense for this. – Butuzov Aug 3 '15 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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