Create Post Automatically

This will create a Post using PHP.

<pre class="wp-block-syntaxhighlighter-code"><?php
     Code SOURCE:
      <blockquote class="wp-embedded-content" data-secret="HobdKZDRPY"><a href="">wp_insert_post()</a></blockquote><iframe title="&#8220;wp_insert_post()&#8221; &#8212; WordPress Developer Resources" class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" src="" data-secret="HobdKZDRPY" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>	
//set timezone

//require native WordPress function

// Post variables
	$userID = get_current_user_id();
	$leadTitle='Operation Test Post'
	$leadContent='Hello there.  this is the post!';
     //ID# automatically assigned to categories.  For multiple
     // categories, separate with a comma
//Time variables - Sets time for now so post can be published without delay
	$timeStamp = date_create();
	$postdate = date("Y-m-d H:i:s", $timeStamp);	

//wordpress Array and Variables for posting
	$new_post = array(
//THIS submits the array data to creat the new post
	$post_id = wp_insert_post($new_post);

//Error Checking
     if ($post_id){
          echo "SUCCESS!";
     } else {
          echo 'OOPS!  something failed, please try again!';

Dealing Date/Time

Setting the default timezone (otherwise, everything will be UTC)


Creating a NOW timestamp as Object

$today=new DateTime();
echo $today->format('Y-m-d H:i:s');

Creating a NOW timestamp procedurally

$today = date_create();
echo date_format($today, 'Y-m-d H:i:s');

Time between two dates (not the cleanest way)

$this_date = (new DateTime()) -> getTimestamp();  //today
$other_date = strtotime('2020-07-14'); //some other date

//Result is in number of seconds, will need to be converted
$delta = $that_date-$this_date;

Quick code for time between two dates:

$date_format='F jS, Y';
$this_date = (new DateTime()) -> getTimestamp();  //today 
$that_date = strtotime("2020-07-07");  //random date mySQL format

$delta = $that_date-$this_date;
$delta = round($delta/60/60/24,1);

$that_date = date($date_format,$that_date);

if ($delta > 0) {
		$that_date is $delta day(s) from today
		" ;
	echo $result;
    } else {
		$that_date was $delta day(s) ago
		" ;
	echo $result;;

Disable WordPress Nickname

Place the following code in your function.php file. (Appearance > Editor > Theme Functions – functions.php)

// remove nickname
function prefix_hide_personal_options() {
        if (current_user_can('manage_options')) return false;
<script type="text/javascript">
  jQuery(document).ready(function( $ ){
if (is_admin()) add_action('personal_options', 'prefix_hide_personal_options');


Why use “if (!function_exists(‘…”

Checking to see if built in WordPress functions exist before calling them is for backward compatibility which IMHO is not needed.

So if you see if ( function_exists( 'register_nav_menus' ) ) the theme author is supporting versions earlier than 3.0.

You still sometimes see
 if ( function_exists( 'dynamic_sidebar' ) ) 
Why? I couldn’t tell you because dynamic_sidebar was introduced in 2.2.

Another reason to use it is to make your theme or plugin pluggable. A pluggable function is one that can be overridden in a child theme or another plugin.

This is done on the definition not the call and you use the ! operator to make sure it doesn’t already exist before you define it.

if ( ! function_exists( 'my_awesome_function' ) ) {
 * My Awesome function is awesome
 * @param array $args
 * @return array
function my_awesome_function( $args ) {
  //function stuff
  return array();

When this is done a child theme or other plugin can override that function with there own.


WordPress Form Submit (POST)

I can’t explain why this was so difficult, but it kicked my butt for weeks. I needed to create a simple form and then submit to a new page.

This is the simple form:

First Name:

Last Name:

This is the snippet

// code for the form
	$form_html = '
		<style type="text/css">
			 .label { font-size: 100%;
				     font-size: large;
				     font-family: Arial, Helvetica, sans-serif;
					 text-decoration: underline;
			 .content { margin-left: 60px;
			 		font-size: x-large;
					font-family: Arial, Helvetica, sans-serif;
			 .button {font-size: x-large;
					font-family: Arial, Helvetica, sans-serif;
					position: relative;
  					left: 50%;
  					transform: translateX(-50%); 

		<form method="post" action="/wordpress-form-receive/" >
			 <p class="label">First Name:</p>
  				<input type="text" name="firstname" value="Mickey"><br>
  			<p class="label">Last Name:</p>
  				<input type="text" name="lastname" value="Mouse"><br><br>
		<input type="submit" class="button" value="Submit">
		</form>	';
	echo $form_html;

Clean Text Function

I needed a way to remove non-printing and irregular ascii character. This is helpfull when controlling user name formatting or comparing two strings. The output will be the cleaned up string based on the options.

Usage: $result=text_clean(<text to be reviewed>,<optional variable>);

	this function will strip all non-printing and 
	irregular ascii symbols and repetitive spaces.  
	Option 0: (default) leaves spaces; 
	Option 1: replaces spaces with underscores; 
	Option 2: removes all spaces.
	Option 3: removes all spaces, underscores, converts to lowercase

function text_clean($submitted,$option=0){

	//reduces multiple spaces to single
	$excess_spaces=preg_replace('/\s\s+/', ' ',$submitted);
	//removes all non_printing/extended ascii characters
	$non_ascii=preg_replace('/[^\da-z ]/i', '', $excess_spaces);

	//Truncates the leading and ending spaces
	if ($option==1){
		$final=preg_replace('/\s/', '_',$trim_spaces);
	if ($option==2){
		$final=preg_replace('/\s/', '',$trim_spaces);
	if ($option==3){
		$final=preg_replace('/\s/', '',$final);
	return	$final;


mySQL database connection

This is how *I* connect to mySQL database(s). CPanel & Godaddy.

Step 1: create the connection string

// Create connection	
	$servername ="localhost";

	$conn = new mysqli($servername, $username, $password, $dbname);

Step 2: Check for a connection; die if not working

// Check connection
	if ($conn->connect_error) {	
		die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);

Step 3: build the SQL query. Note: the “\n” is the php line break.

$sql_query = "SELECT table.column,table.column,table.column,  \n"
	   . "FROM table \n"
	   . "WHERE table.column=conditiom \n"
	   . "ORDER By table.column";

Step 4: run the query

$result = $conn->query($sql_query);

Step 5: loop through the results

while($row = $result->fetch_assoc()) {
	$temp_variable = $row['column'];
        //do work with $temp_variable

Step 6: close out the connection



Quick code that checks to see if current user is logged in. Anything other than “0” is logged in.

//Grab the current user details from WordPress
	$current_user = wp_get_current_user();	
	if ( 0 == $current_user->ID ) {
    	echo 'Sorry.  You need to be logged in to see this information....';