Plugin won’t save to database, need help…
-
Heya,
So I’m writing my first plugin, and I have written the code below to deposit the saved content into the database, but I’m not sure how to execute my function (tpc_notifier_save_data) upon the page reloading. I assume if I can do that it will properly deposit the content into the database. 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; $save_and_publish = $_POST['save-and-publish']; $unpublish = $_POST['unpublish']; $title = $_POST['title']; $message = $_POST['message']; if( isset($save_and_publish) ) { echo "<h2>Saved and Published.</h2>"; $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>'; } elseif ( isset($unpublish)) { echo "<h2>Unpublished.</h2>"; } } 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 /> <table class="instructions"><tr><td> <h3>Shortcode</h3> <p>To display this message on a particular page, copy the following "shortcode" and paste it into the page you want it to display on:</p> <p><b><i>[tpc_notifier]</i></b></p> </td> <td> <h3>Developer Notes</h3> <p>If you are a developer you may use the following PHP code:</p> <p><i><b><?php tpc_notifier_shortcode(); ?></b></i></p> </td></tr></table> <br /> <h3>Message</h3> <p>Title: <input type="text" name="title" value="' . $title . '" maxlength="200" /> <p>Enter the message you would like displayed in your notification into the text area below:</p> <textarea name="message" class="message">' . $message . '</textarea> <br /> <button class="button-primary" name="save-and-publish">Save and Publish Notification</button> <button class="button-secondary" name="unpublish">Unpublish Notification</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 ‘Plugin won’t save to database, need help…’ is closed to new replies.