Monthly Archives: August 2018

Don’t Hard Code WP-Content Directory!

Plugin development tip of the day!
DO NOT HARD CODE wp-content Directory!

I made this mistake not realizing how many people change that directory as a security precaution.

Alternatives

The best alternative is probably the WP_CONTENT_DIR in my opinion. You can also use WP_CONTENT_URL, depending if you want a relative path or a full url.

Examples:
Here is an example of each followed by the output code:
<?php
echo WP_CONTENT_DIR;
echo "<br />";
echo WP_CONTENT_URL;
echo "<br />";
?>

/var/www/html/jafty.com/public_html/wp-content
http://jafty.com/wp-content

So now you know!

Want more? Here are some other ways to get similar paths or directories  from within a plugin file:

<?php
echo "<br>";
echo plugins_url( 'myscript.js', __FILE__ );
echo "<br>";
echo plugins_url();
echo "<br>";
echo plugin_dir_url(__FILE__) ;
echo "<br>";
echo plugin_dir_path(__FILE__) ;
echo "<br>";
echo plugin_basename(__FILE__) ;
?>

The above would output the following lines if it were ran from a plugin on jafty.com:

http://jafty.com/wp-content/plugins/jafty_plugin_tester/test_code/myscript.js

http://jafty.com/wp-content/plugins

http://jafty.com/wp-content/plugins/jafty_plugin_tester/test_code

//var/www/html/jafty.com/public_html/wp-content/plugins/jafty_plugin_tester/test_code/

jafty_plugin_tester/test_code/wp_content_url.php

EDD Instant Updater WordPress Plugin

The EDD Instant Updater WordPress plugin is a plugin I created while working with a custom WordPress plugin that needed to have instant update notifications while using Easy Digital Downloads(EDD). EDD comes with it's own updater class, but it uses the default WP standards which means you have to wait up to 12 hours after you push an update to get the notice in the WordPress dashboard! With the EDD Instant Updater plugin, you no longer have to wait, the plugin update notices come to you instantly! No more delays!

Interested? Contact Ian L of Jafty.com by email at linian11@yahoo.com for more information.

How to Fix Max File Upload Size on Godaddy and Other Shared hosting Accounts

I ran into this problem using WordPress today where I reached the max upload size on my client's Godaddy server on a shared hosting account. If you get this error in WordPress, while uploading a file, by the way, it's probably a max file size issue:

Error: 1

That's all it said! So it took me a few guesses to figure out what that "Error: 1" message meant.

How to increase the Max File Size Setting in Cpanel

1 - go to your site's Cpanel by going to yoursite.com/cpanel and log in with the user and password your hosting provider provides for you. You should see this when logged in:

cpanel1

2) scroll down until you see the "Select PHP Version" link in Cpanel that is circled in the below image and click on it:

cpanel1b

3) That will take you to the PHP version selection page. Then click the link in the upper right corner of the page that you see circled in the below image:

cpanel2

4) Clicking the "Switch to PHP Options" link in the above image will take you to a PHP settings page. Look for the upload_max_filesize setting that you see circled in the image below and set it to something higher than 2M according to your individual needs:

cpanel3b

5) After you change it from "2M" to something higher as needed, click the "Apply" button directly to the right of the dropdown and then be sure to also click the "Save" button near the bottom-left of the page as well and then you are done!

PHP WordPress Large background Importer Code

My wonderful client wanted me to add a .CSV importer that imports posts into a Custom Post Type(CPT) in large quantities of around 10k to 50k at a time.

What a standard WordPress site can handle

From my experience building plugins and importers for WordPress over the past 20 years, I have learned to expect trouble if you try to import anything more than 4k to 5k posts into WP at a time. The main requirement for the importer that I am building now is that it is able to import 20k posts in under 15 minutes. Not too difficult, but a challenge none-the-less because the average WP site will timeout after the first 4k posts on average. I also wanted to accomplish this goal without using any bloated PHP Que libraries or Worker Libraries, so I'll be developing it completely from scratch, just like a grown up!

Step One, Break Large File into Smaller Chunks

The first thing I did while developing my large WP file import feature, was to handle the issue of having a huge .csv file with 20 thousand posts saved in it. What do you do? What I did was wrote a PHP script that reads in that huge file and breaks it up into manageable chunks and saves each chunk to its own separate .csv file. Kind of simple really, this is the easy part!

I created a file named file_split.php to handle this portion of the importer's code. Here are the contents of that file:

<?php
$chunk_size = 2000;//size of ea. chunk file given in the no. of lines written to the file(excluding the first line with headings that will be at the top of ea. chunk file created).
$max_files = 0;//it will stop after writing this many files, set to 0 for no limmit
$u = $uploadpath;//"double_accounts-test.csv";
//read a file line by line:
echo "<p>Opening $u to spit it into chunks......</p>";
$file = fopen($u, "r") or exit("Unable to open $u!");
//extract the first line from the file:
$first_line = fgets($file);
//Output a line of the file until the end is reached:
$i=0;
$ii=0;
$iii=0;
$chunk=$first_line;
while(!feof($file) &&($iii < $max_files||$max_files==0)) {
$i++;
$ii++;
$line = fgets($file);
//echo "$i) $line<br>";
//add line to lines that get written to files:
$chunk .= $line;
//once we get to chunk_size lines, write a new file
if($ii == $chunk_size){
$iii++;
//echo "<h3>Chunk #$iii:</h3>";
//echo $chunk;
//create dynamic filenames:
$fn = ABSPATH."wp-content/plugins/YOUR-PLUGIN/temp/chunk$iii.csv";
//echo "Writing file $fn....<br>";
$fh = fopen($fn, 'w') or die("can't open $fn file to overwrite");
fwrite($fh, $chunk);
fclose($fh);
$m = "<span style='color:green'>Chunk Written to $fn</span><br>";
//reset $ii var back to zero:
$ii = 0;
//empty the $chunk var and add first line to top of file content($chunk)
$chunk=$first_line;
//echo "<hr><span style='text-align:center;color:orange;font-weight:bold;width:100%;margin-right:111px'>End File #$iii</span>$m<hr>";
echo $m;
//add the file name to the fnames array:
$fnames[] = $fn;
}//end if $ii = $chunk_size
}
fclose($file);
//if $ii is greater than 0 and less then chunk_size, then there was a left over chunk at the end, so write it to a file too:
if($ii > 0 && $ii < $chunk_size){
$noof_files = $iii+1;
$fn = ABSPATH."wp-content/plugins/YOUR-PLUGIN/temp/chunk$noof_files.csv";
echo "Left over chunk of $ii lines remains, so writing it to final file named $fn....<br>";
//echo "Writing Final file $fn....<br>";
$fh = fopen($fn, 'w') or die("can't open $fn file to write final chunk!");
fwrite($fh, $chunk);
fclose($fh);
echo "<h3 style='color:green'>FINAL Chunk Written to $fn! DONE chunking $u file! Total files created: $noof_files</h3>";
//add final file name to fnames array:
$fnames[] = $fn;
}else{
echo "Wow! We broke even with no left over chunk of less than $chunk_size lines, so finished writing files! Final file is $fn.<br>";
}

?>

Put the file_split.php file up for now. We will include it in another file later when we have the rest of our import feature code written. You can test file_split.php easy enough by

Delete Entire Folder with PHP Code

Today, I had to write a block of pure PHP code to delete an entire folder full of files, sub-folders and more files from a server. This particular job was for a WordPress plugin, but the code I used is not specific to WordPress and can run just fine outside of WordPress or inside a WordPress plugin. The only requirement is an up to date PHP installation for this code to function as intended.

<?php

$path = ABSPATH.'wp-content/plugins/cultivate-crm/test';
$dir_arr = parse_dir($path);
global $darr;

function parse_dir($path){
static $cntf = 0;
global $darr;
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
//Check whether the entry is a file or folder.:
$fof = $entry;
    if($fof != "." && $fof != "..") {
    $cntf++;
    $fof = $entry;
    $file_type = filetype($filepath);//get file type.
    $file_size = filesize($filepath);//get file size.
    echo "$cntf) $fof<br />Type: $file_type<br />Size: $file_size<hr />";
    if($file_type=='dir'){$darr[]=$filepath;parse_dir($filepath);}else{echo "Deleting $filepath....<br>";unlink($filepath);}
    }//end if is file etc.
}//end while going over files in excel_uploads dir.
}//end parse_dir PHP function by Ian L. of jafty.com

$c = count($darr);
echo "$c empty directories remain...deleting them now....<br />";
$rdarr = array_reverse($darr);
foreach($rdarr as $dr){
    rmdir($dr);
    echo "Folder $dr deleted!<br />";
}
//and finally remove the test folder:
$test_folder = ABSPATH.'wp-content/plugins/cultivate-crm/test';
rmdir($test_folder);
echo "Folder $test_folder deleted!<br /><p style='color:green'>Now all temporary folders have been removed from the server!</p>";
?>

Make sure you set your path variable at the top of the script correctly and if you are not in WordPress, replace ABSPATH or set a new PHP constant named ABSPATH and it will work as it is. Since the above code deals with deleting files permanently from the server, I must insist that you use it at your own risk! Be careful because one wrong path entered into this function can destroy your entire file system if you don't know what you are doing!

MailChimp API 3.0 Helper PHP Function

I've been doing a lot of work with the MailChimp API V. 3.0 over the past year while developing the Cultivate CRM WordPress Plugin and I created the following helper function that has made live a lot easier for me since I have to make probably a dozen calls to the API within this one plugin.

MailChimp API 3.0 Helper Function

<?php
function mailchimp_request($mcAPIkey,$url,$req_type,$json=''){
//send a HTTP POST, GET or PUT request with curl:
 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $mcAPIkey);
 curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_TIMEOUT, 100);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $req_type);//GET, PUT, POST
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 if($req_type!="GET")curl_setopt($ch, CURLOPT_POSTFIELDS, $json);//not needed for GET requests
 $result = curl_exec($ch);
 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 curl_close($ch);
 // echo "http code: $httpCode<br />";

$body = json_decode($result);
 //echo "successfully got list for Cultivate CRM!<br />result:<hr />$result<hr />";
 $results['code'] = $httpCode ;
 $results['result'] = $result;
 // store the status message based on response code
 if ($httpCode == 200) {//if no error occurred:
 return $results;
 } else {//else an error occurred:
 return false;
 }//end else an error occurred
 }//end mailchimp_request PHP function by Ian L. of Jafty.com

Calling mailchimp_request Function

Here is how you use the above mailchimp_request PHP function:

<?php
//Construct the MailChimp API URL:
$mcAPIkey = 'YOUR-MC-API-KEY';
$mcListID = 'YOUR-MC-LIST-ID';
$email= 'MEMBER_EMAIL@EMAIL.COM';
$memberID = md5(strtolower($email));
$dataCenter = substr($mcAPIkey,strpos($mcAPIkey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $mcListID . '/merge-fields/';    
//get the response:
$response = mailchimp_request($mcAPIkey,$url,'GET');
$httpcode = $response['code'];
$result = $response['result'];
$decoded_result = json_decode($result);
echo "http code: $httpcode<br>";
echo "Response data from getting address field data from MC API:
";
$merged_field_obj = $decoded_result->merge_fields;//should be an array of merged fields
print_r($merged_field_obj);
?>