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.

source: https://wordpress.stackexchange.com/a/111318

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
	$trim_spaces=trim($non_ascii);
	
	$final=$trim_spaces;
	
	if ($option==1){
		$final=preg_replace('/\s/', '_',$trim_spaces);
	}
	if ($option==2){
		$final=preg_replace('/\s/', '',$trim_spaces);
	}
	if ($option==3){
		
		$final=str_replace('_','',$trim_spaces);
		$final=preg_replace('/\s/', '',$final);
		$final=strtolower($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";
	$username="database_admin";
	$password="password";
	$dbname="myDatabase";

	$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

mysqli_close($conn);