Saving plugin data to database, need help…
-
Heya,
So I’m writing my first plugin and I figured out how to save plugin settings into the options fields, but now I’m trying to save actual data into the database table my plugin creates upon activation. I’m stuck though, I’ve written the function, but I don’t know how to execute it upon page load. Can someone help? Thanks!<?php $tpc_notifier_version = "0.1"; function tpc_notifier_create_database() { global $wpdb; // THIS GETS THE WORDPRESS DATABASE CREDENTIALS THEN CONNECTS TO THE DATABASE $table_name = $wpdb->prefix . "tpc_notifier"; // THIS VARIABLE IS USE TO CREATE A TABLE IN THE DATABASE CALLED WP_TPC_NOTIFIER if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { // THIS ASKS IF THE TABLE ALREADY EXISTS. ESSENTIALLY MEANING: HAS THIS PLUGIN BEEN ACTIVATED BEFORE? // THIS ACTUALLY CREATES THE TABLE IN THE DATABASE. KEEP IN MIND THOUGH IT'S STORING IT IN A VARIABLE CALLED $SQL MEANING THIS CODE WON'T BE EXECUTED UNTIL THAT VARIABLE IS USED (WHICH IS IS A FEW LINES BELOW THIS IN DBDELTA($SQL); $sql = " CREATE TABLE " . $table_name . " ( id mediumint(9) NOT NULL AUTO_INCREMENT, title VARCHAR(200) NOT NULL, message text NOT NULL, UNIQUE KEY id (id) ); "; add_option("tpc_notifier-version", $tpc_notifier_version); } // THE DBDELTA FUNCTION REQUIRES UPGRADE.PHP TO BE INCLUDED require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } register_activation_hook(__FILE__,'tpc_notifier_create_database'); // THIS TELLS WORDPRESS TO EXECUTE THIS FUNCTION UPON THE USER ACTIVATING THE PLUGIN. function tpc_notifier_add_options_page() { add_options_page('TPC Notifier Options', 'TPC Notifier', 'manage_options', 'tpc-notifier', 'tpc_notifier_admin_page'); wp_enqueue_style('style', '/wp-content/plugins/tpc-notifier/css/admin.css', '', '1.0', 'screen'); } add_action('admin_menu', 'tpc_notifier_add_options_page'); function tpc_notifier_save_data() { global $wpdb; $title = $_POST['title']; $message = $_POST['message']; if( isset($title) || isset($message) ) { $insert = "INSERT INTO " . $table_name . " (title, message) " . "VALUES ('" . $wpdb->escape($title) . "','" . $wpdb->escape($message) . "')"; $results = $wpdb->query( $insert ); echo '<div class="updated"><p>Saved.</p></div>'; } } function tpc_notifier_admin_page() { wp_tiny_mce( false , array( "editor_selector" => "message" ) ); // THIS CALLS TINYMCE AND APPLIES IT TO ANY ELEMENT WITH A CLASS OF "MESSAGE", YOU CAN CHANGE THAT TO WHATEVER YOU LIKE OF COURSE. echo ' <div class="wrap tpc-notifier"> <h2>The Portland Company Notifier Plugin</h2> '; echo ' <form method="post" action=""> '; settings_fields( 'myoption-group' ); echo ' <p>The Portland Company Notifier Plugin allows you to display a message, in a lightbox presentation format, to users who are visiting your website for the first time. <a href="https://plugins.theportlandco.com/tpc-notifier" target="_blank">Click here</a> if you would like to view a demonstration.</p> <br /> <h3>Shortcode</h3> <p>To display this message on a particular page, code the following "shortcode" and paste it into the page you want it to display on: [tpc_notifier].</p> <br /> <h3>Message</h3> <p>Title: <input type="text" name="title" value="' . $title . '" maxlength="200" /> <p>Enter the message you would like display in the text area below:</p> <textarea name="message" class="message">' . $message . '</textarea> <br /> <button class="button-primary">Save Changes</button> <button class="button-secondary">Publish</button> <button class="button-secondary">Unpublish</button> </div> '; } // SHORTCODE API function tpc_notifier_shortcode() { echo ' <script type="text/javascript"> $(function() { $("#dialog").dialog("destroy"); $("#dialog-message").dialog({ modal: true, buttons: { Ok: function() { $(this).dialog("close"); } } }); }); </script> <div id="dialog-message" title="Download complete">' . $message . '</div> '; } add_shortcode('tpc_notifier', 'tpc_notifier_shortcode'); // FRONT END STYLES function tpc_notifier_styles() { echo ' <link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/ui-lightness/jquery-ui.css"> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script> '; } add_action('wp_head', 'tpc_notifier_styles'); // THIS ADDS THE CODE WITHIN THE TPC_NOTIFIER_STYLES FUNCTION INTO THE HEAD OF THE WEBSITES THEME. ?>
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Saving plugin data to database, need help…’ is closed to new replies.