• 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)
  • Thread Starter Spencer Hill

    (@ws5f3dj7)

    I discovered a variety of problems, the main thing is that I was trying to run a query without using the special functions WordPress has built in so I encountered some errors, here is a link that taught me what I needed to do and here is my code, email me or PM me if you need help!

    https://codex.www.ads-software.com/Function_Reference/wpdb_Class

    <?php
    /*
    Plugin Name: The Portland Company Notifier
    Plugin URI: https://plugins.theportlandco.com
    Description: This plugin allows back end users to create a message that appears upon users entering a website. It displays it using the traditional lightbox effect and can be enabled or disable. <a href="https://plugins.theportlandco.com/tpc-notifier" target="_blank">Click here</a> if you would like to view a demonstration.
    Version: 0.1
    Author: The Portland Company
    Author URI: https://www.ThePortlandCo.com
    License: GPL2
    
    Copyright 2010  THE PORTLAND COMPANY  (email : [email protected])
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    */
    
    $tpc_notifier_version = "0.1";
    
    $table_name = $wpdb->prefix . "tpc_notifier"; // THIS VARIABLE IS USE TO CREATE A TABLE IN THE DATABASE CALLED WP_TPC_NOTIFIER
    
    $save_and_publish = $_POST['save-and-publish'];
    $unpublish = $_POST['unpublish'];
    
    	$posted_notification_title = $_POST['title'];
    	$posted_message = $_POST['message'];
    	$posted_status = $_POST['status'];
    
    if ( isset($save_and_publish) ) {
    
    	$wpdb->update( $table_name, array( 'title' => $posted_notification_title, 'message' => $posted_message, 'status' => 'published'), array('id' => 1) );
    
    } elseif ( isset($unpublish) ) {
    
    	$wpdb->update( $table_name, array( 'title' => $posted_notification_title, 'message' => $posted_message, 'status' => 'unpublished'), array('id' => 1) );
    
    }
    
    $show_results = $wpdb->get_row('SELECT * FROM ' . $table_name . ' WHERE id = 1 ', ARRAY_A);
    
    $notification_title = $show_results['title'];
    $message = $show_results['message'];
    $status = $show_results['status'];
    
    function tpc_notifier_create_database() {
    
       global $wpdb; // THIS GETS THE WORDPRESS DATABASE CREDENTIALS THEN CONNECTS TO THE DATABASE 
    
       global $table_name;
    
        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,
    			status VARCHAR(7) 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_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.
    
    	global $wpdb;
    	global $table_name;
    
    	global $notification_title;
    	global $message;
    	global $status;
    
    	global $save_and_publish;
    	global $unpublish;
    
    	echo '
    		<div class="wrap tpc-notifier">
    		<h2>The Portland Company Notifier Plugin</h2>
    	';
    
    	if ( isset($save_and_publish) ) {
    		echo '<div class="updated"><p>You're notification has been saved and <b>published</b>.</p></div>';
    	} elseif ( isset($unpublish) ) {
    		echo '<div class="updated"><p>You're notification has been <b>unpublished</b>.</p></div>';
    	}	
    
    	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>Message</h3>
    			<p>Status: <b>' . ucwords($status) . '</b>.
    			<p>Title: <input type="text" name="title" value="' . $notification_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>
    
    			<br />
    			<br />
    
    			<div id="sm_rebuild" class="postbox">
    				<h3 class="hndle"><span>Shortcode</span></h3>
    				<div class="inside">
    					<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>
    				</div>
    			</div>
    
    			<div id="sm_rebuild" class="postbox">
    				<h3 class="hndle"><span>Developer Notes</span></h3>
    				<div class="inside">
    					<p>If you are a developer you may use the following PHP code:</p>
    					<p><i><b><?php tpc_notifier_code(); ?></b></i></p>
    				</div>
    			</div>
    
    		</div>
    	';
    
    }
    
    // SHORTCODE API
    function tpc_notifier_code() {
    
    	global $notification_title;
    	global $message;
    	global $status;
    
    	if ( $status == 'published' ) {
    
    		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="' . $notification_title . '">' . $message . '</div>
    		';
    
    	} elseif ( $status == 'unpublished' ) {
    	}
    
    }
    
    add_shortcode('tpc_notifier', 'tpc_notifier_code');
    
    // 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)
  • The topic ‘Plugin won’t save to database, need help…’ is closed to new replies.