If you're a web developer and are considering getting more into mobile application development for IOS and/or Android operating systems for mobile phones, then you are likely go ask the question, "What Programming Language is Best for Mobile App Development?".

The answer is somewhat complicated because there are many answers. To narrow down the possible options, I am going to make a couple assumptions. I will assume that you, like myself, like to stay away from platforms and drag and drop type UI tools to program with. I will also assume that you want to develop something for both Android and iPhone, not just one or the other.

Considering the above assumptions, there are still many options, so what makes it such a complicated choice? In my opinion the main factor that makes it so difficult to pick a coding language to specialize in for mobile app development is the fact that there are two main and competing mobile platforms that have chosen to use different languages with their operating systems. Of course I am speaking about Android Vs. iOS.

Android Vs. iOS, Which is More Popular?

A quick Google search shows me that just over 85% of smartphones are Android, while most of the remaining 15% are Apple iOS phones. According to an article I read on 9to5mac.com, Android and iOS together account for 99.1% of all smartphones in use today, which makes any other devices almost pointless to develop for. It is worth mentioning that of the 0.9% of devices that are not running Android or iOS operating systems, are mostly running Windows operating systems. Windows OS accounts for 0.6%, while 0.1% are Blackberry and the remaining 0.2% are various other operating systems that are not popular enough to be worth even mentioning here.

Best Languages for Android OS

JAVA

Since Android controls the majority of the market for smartphones today, let's look at them first. If you are going with the most popular answer, than Android wins by far! Android operating systems use Java. If you are coding an app for Android phones, Java is most likely going to be your best programming language to choose as the base language for your app. In reality, many apps use a combination of several different languages. However, in my experience your language of choice will make up the majority of the app's code.

Kotlin

Kotlin, a second app development language option, was designed and developed by JetBrains. They are a Czech company known for the popular IDE, IntelliJ IDEA. Google's Android team recently announced that they are officially adding support for the Kotlin programming language. Kotlin was created to address some of the Java issues. Some say the Kotlin syntax is clean, simple, and leads to less code bloat.

Best Languages for iOS

While Apple's iOS operating system only accounts for roughly 15% of the smartphone market, that is still a significant number of devices out there. Smart developers create apps for both Android and iOS, whether it be with separate apps for each or some sort of platform used to create a mashup that will allow a single app to function on both operating systems.

Apple iOS apps primarily use SWIFT or Objective-C

SWIFT

SWIFT is a quickly growing open source language popular with many starting out with app development today. Apple has heavily promoted the use of SWIFT making it clear that they intend for SWIFT to be the leading language choice for iOS application developers.

Objective-C

Objective-C was the original language of the iOS operating system, but is being phased out apparently since Apple is making it clear that SWIFT is the future, then that leaves Objective-C stuck in the past. While it is still a viable option, I would suggest using it only on an as-needed basis for iOS application development purposes.

Cross -Platform Mobile App Language Options

For some of you, particularly those of you who don't want to make two separate apps, a cross-platform language option might seem more attractive. A cross-platform language in this context, is one that will allow a mobile application to function on both Android and iOS without the need to make two applications, one for each OS.

JavaScript

If you started out as a web developer as I did, then you are surely familiar with JavaScript. Not to be confused or associated with Java, JavaScript is it's own language despite the similar name. JavaScript is very versatile in that you can complete many different types of tasks using the language. It is probably one of the most popular cross-platform options because it is effective and rather light-weight.

Several JavaScript frameworks exist today that target the mobile application development market. While I am not a huge fan of most frameworks in general, some of these deserve some serious consideration none-the-less. Some of the well known JavaScript frameworks popular today include PhoneGap by Cordova, Angular.js, jQuery Mobile and React. There are many more that are worth researching if you are looking for a cross-platform JavaScript framework or library to help build your next mobile application for Android and iOS. Actually, JavaScript apps will also work on the other less used Operating Systems as well such as Windows OS and Blackberry.

Today, I'm writing a rather simple WordPress Ajax example that demonstrates how to user Ajax in a wp-admin page from a WordPress Plugin.

Using Ajax in wp-admin is a little easier than using it on the front-end of a WordPress site because of the ajaxurl JavaScript global variable which refers to the admin-ajax.php url.

In the following code example the goal is to provide an admin page with a delete post link that is powered by Ajax. Therefore the code enables the deletion of a WordPress post without the obtrusive page refresh!

This is a pretty bare-bones example meant as a learning tool. It could be fleshed out much more to be more practical, but I wanted to keep it simple to get the main point across, which is how to do a simple Ajax request from wp-admin.

The Essential Code

First, for those of you that only need to see the basics, I'll demonstrate the essential code for the Ajax to work. Further down this page, I'll put it all together in a simple plugin for those of you that may need to see a working example to grasp the concept better. Here is the essential code to enable an Ajax delete post button:

First, a simple HTML line to call our JavaScript function onclick:

In the above, you'll notice the ajax_delete_posts JavaScript function is called. ajax_delete_posts is the function we must create! Of course you'll need to define the post id in $ID and the post tile in $title using PHP, but this is just to show you how I intend to call the Ajax/JavaScript function

Second, we use PHP to generate our ajax_delete_posts function in JavaScript code:

You can rename the ajax_delete_posts as you wish, just make sure to rename it in your HTML above too if you do. Here's the code to generate the JavaScript from within your PHP file, the plugin's main file normally:

Notice in the above code how we pass the post id to the JavaScript function using the "t" variable. Also it's important to note your data variable's action value as you will need to use the same value for the function name in the below code. Feel free to use whatever text you want for the action as long as it can be an appropriate PHP function name in the code below as well.

Finally, the third step is to add your PHP function named after the action named above and also hook into it with "wp_ajax_" followed by the action named above as well, so in our case the hook we use would be "wp_ajax_del_posts_with_ajax". However, if you use a different action, such as "do_aj" for example, then your action hook would read "wp_ajax_do_aj". Got it? I hope so, but the code example should help too, so here it is:

In the above code example, we first set up our action hook by using the hook "wp_ajax_" followed by the action we used in the "var data" line further above. Then we write a PHP function named exactly like the action we used above and be sure to also name the function as the parameter in the action hook as well and you will be good to go!

Putting it all together in a simple Ajax WordPress Plugin

Next I'll show you the example plugin I created to demonstrate exactly how this all works together in case some of you failed to grasp it from my previous explanations. You can cut and paste the following PHP code into a file named "ajax_post_delete.php" and place it on your desktop in a folder named "ajax_post_delete" and I promise you the code will function on any standard WordPress installation as of the day I wrote this. Here is the complete "Ajax Post Delete" WordPress plugin code in a single file example plugin:

That's it! copy and paste the above code into your .php file and you've got a simple WordPress plugin that will demonstrate the use of Ajax effectively in a custom admin page of your WordPress website. Feel free to modify the code as needed, but please change the plugin name if you intend to distribute it. Thank you and Good Luck!

Adding a custom field to the WordPress Quick Edit screen for a custom post type was a rather tricky task, so I wanted to be sure to share my solution with my readers in case any of you have a difficult time with it as well.

Let's get our hands dirty shall we?

There are six basic high level steps to adding a field to quick edit which are:

Add a new column to the posts screen for the post type - I thought this was not necessary at first, but I assure you that it is 100% required for the new field to work within the quick edit UI. Therefore if this turns you off, you can surely find a way to hide the field from view if you don't want it to show up on the posts admin screen.

Fill the column you added in the previous item with data - While this step isn't actually required for the field to work in the quick edit screen, it is needed if you wish for the column to have relevant data in it. However, if you wish to hide the column as I suggested could be done, then you may surely skip this step. I don't recommend skipping it however.

Add HTML code to the Quick Edit User Interface - this will be accomplished, as you'll soon see below, with WordPress's quick_edit_custom_box action hook and your custom HTML for your custom field.

Save the new custom field's value that you've added to the Quick Edit UI - This can be easily achieved by making use of the WordPress save_post action hook as you will discover below.

Add some JavaScript code in the admin page's footer to update your custom fields value within Quick Edit UI - this is where it gets a little tricky for some. I'll explain as best I can below, but suffice it to say for now that you have to update the input field you created in the quick edit menu with JavaScript that's generated with PHP code using WordPress's admin_footer hook. I have seen some alterations of this in other posts online where people use JQuery, but I tend to find that using JQuery is often more likely to lead to plugin conflicts if you are not very careful, so I prefer to use plain JavaScript whenever possible.

Finally you'll need to link your JavaScript function from the previous step to the Quick Edit link for each post using a JavaScript OnClick event - this is done with the elusive WordPress filter named post_row_actions that allows you to change the context of the Quick Edit link itself with some PHP magic.

Now that the high level steps have been explained in very little detail to give you an idea of what we'll be doing in this tutorial, I will demonstrate each step with a working code example that you can use to paste into your own Plugin or Theme files as needed. If you don't know where to paste the code, then frankly, this tutorial may not be for you, so I won't go into that part in detail since this tutorial is really meant for plugin developers and all of them surely know where to place the code I'll be showing below.

Step 1 - Add a new column to the posts screen for the post type

Step 1 is by far the easiest step with very little code. All we need to accomplish here is to add a column to the Custom Post Type admin screen. Here is the simple PHP code:

Not a lot you need to do to the above code but change the custom post type name and the field name to your own CPT and Custom Field Name, so search and replace "dogs" with the name of your Custom Post Type. Then search and replace "groomer" with your custom field name. Your now done with step 1! Don't worry, they will get harder!

Step 2 - Fill the column you added in the previous item with data

Now all we have to do in step 2 is fill our column from step 1 with relevant data. In our case, the name of the groomer for each dog in the dogs custom post type. Here's the PHP code:

Again, all you have to do to make this code your own is replace "dogs" with the name of your custom post type and replace "groomer" with the name of your custom field and be sure to get the ones with the underscore in front of them too! I like to put underscores in front of field names when saving them to the database. That's all for step 2! Another easy one! Okay on to step 3.

Step 3 - Add HTML code to the Quick Edit User Interface

In step 3 we have to add a PHP function using the quick_edit_custom_box WordPress hook that adds HTML content to the Quick Edit User Interface. Here's the PHP code:

Again to make the above code your own, you'll want to replace all occurrences of dogs and groomer with your own names as in the previous two steps. Another option you may wish to change in the above code will be the input type. Here we have decided to use a select input to create a drop-down of possible groomer names to assign to each dog. You may prefer to use a text field, checkbox or even a textarea here, you can use any form element you want to collect your custom field's data. Simply change the above code to fit your individual needs and your done with step 3!

In step 4, we save the new custom field value to the WordPress database when it is edited from the Quick Edit dialog. As with all the previous steps, change the names of dog and groomer according to your own post type and field name respectively. In the below code we use the common "save_post" WordPress hook to detect when a new field value is submitted and save it with the following PHP code:

Here is where it gets a little tricky! There's no simple PHP code to update the Quick Edit UI with the latest updated field value, so we have to generate some JavaScript with the following PHP code. We'll use the admin_footer WordPress hook to inject JS code into the footer of the admin page like so:

Now the above code maybe a little complicated for some, especially those of you who want to use a different type of form field rather than use the drop-down select like in our code. Where you see the section of code that has the comment "//check option selected" before it is where you'll need to make significant code changes if you use anything other than a select field in your own code. Also be sure to also change all occurrences of dog and groomer as in each of the previous steps.

Step 6 - Link your JavaScript function from step 5 to the Quick Edit link for each post using a JavaScript OnClick event

Next due to the fact that it's impossible to update the option updated in the quick edit UI, we use the following PHP and JS magic to make it happen with the help of the WordPress post_row_actions filter hook:

Changing each occurrence of dogs and groomer in the above code should be sufficient in most cases to make it your own. Good luck!

Going a Step Further with Bulk Edit Ability Too!

Okay, this technically makes it seven steps instead of six, but I added this as an afterthought because it is so easy to add the bulk edit feature to the above code. All you have to do in order to make the same custom field from the above code show up in the bulk edit screen or UI, is add one line of code! Therefore I thought it wise to add it here. Here is how:

If you look above at step 3, the first line of PHP code is the quick edit hook which looks like this:

All you have to do to also make the same field show up in the bulk edit screen too is add the following code that contains the bulk_edit_custom_box hook in place of the quick_edit_custom_box hook in the above line of code like this:

That's it! you can add the above l line directly before or after the quick_edit_custom_box hook as in step 3 above. Now if you select all posts from the custom post type screen and then in the "bulk actions" dropdown, select "edit" and you can edit the new custom field value for all of your posts at one time! Yes, we saved the simple step for last! Enjoy your new bulk and quick editor plugin!

Summary

In conclusion, you'll want to incorporate the above code examples into your own plugin for best results. I have created such a plugin for anyone who needs it. Please feel free to contact me by email at linnian11@yahoo.com if you would like the plugin.

If you've been using WordPress for a good length of time now, you have likely come across a time when you published a post and tried to visit the page and it gave you a 404 not found error, right? Of course I'm right or you probably wouldn't be here reading this, so here are a couple common fixes you should try first when this happens.

Re-Save Permalinks

The first thing I always try is to simply go to wp-admin/settings/Permalinks and make sure the settings are correct and even if they are, save them again just by clicking the button at the bottom of the page even if you do not change a thing! This will often fix your problem, but not always.

Re-Name Your Post and Permalink

Sometimes simply giving your post a new name and changing the Permalink at the top of the page will resolve the problem as well. To do this, simply change the title by adding a word or two and then change the permalink right under the title by changing it to match the new title but make it all small letters and replace spaces with hyphens. One reason this might work is if you had accidentally used a reserved word for your title or a title that exists for some other purpose in WordPress. When that happens it will simply ignore it and show you a 404 link instead usually. If you do not want to change your posts title, then you can probably get away with just changing the permalink right under the title, but you always have to change the permalink at least to be sure you're not using a reserved word or link that is already in use somewhere else within WordPress.

This is going to be a simple WP Cron tutorial that explains the basic use of WordPress cron, the wp_schedule_event function and it's 3 built in intervals. I'll write a more complex one soon that explains how to add your own intervals, but this is a good starting point:

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.

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:

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

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:

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:

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!

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

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.

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!

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.

I was working on a custom plugin for an important client today and one of the plugin's admin pages would scroll half way down the page every time I landed on it and I couldn't figure out why at first!

Long story short, I found out what was causing it was an inline JavaScript block of code, so If anyone has this issue in a WordPress admin page while making a plugin, try getting rid of the JavaScript and it should stop the page from scrolling as it did in my case.

If you experience something similar, please mention how you solved it in the comments.

I've been doing a lot of work with the MailChimp API lately and figured it would be good to have a page dedicated to it here for future reference because it's not very well documented online anywhere else... Hoping this will help some people who also have to use MailChimp's API.

Understanding MailChimp Lists and the API

If you've ever dealt with MailChimp, you know that it is based on lists which can be segmented, grouped and managed all from the API as needed. Next I will demonstrate how to get a list of MailChimp lists and information on each list that exists.

Getting MailChimp List Information from API

Here's the code I created to get all MailChimp Lists and information on each list from the MailChimp API using PHP code:

I find myself having to do this all the time! I need to know how many times a function is running and for some strange reason, I almost never remember how to do it correctly. That's why I am posting this simple trick on my blog, so others can find it easily and so I can look at how I did it next time I need to do it again!

Here's a simple example to illustrate the need here:

<?php function runMe(){

$i = 0;

$i++;

echo "$i<br>";

}

//If you were to execute the above function in a loop like this:

for($x=0;$x<10;$x++){

runMe();

} ?>

The output of the above would be:

1
1
1
1
1
1
1
1
1
1

Not what you wanted? or expected? Often we need it to output something like this instead:

1
2
3
4
5
6
7
8
9...

Static Variable to the Rescue

In comes what is know in PHP as Static Variables. Here's a quick code example that should make you understand how they work:

<?php function countRuns(){

static $c = 0;

$c++;

echo "$c<br />";

}

//Then, Executing the countRuns function in a for loop will increment the number printed ea. time:

for($x=0;$x<10;$x++){

countRuns();

} ?>

If you were to run the above tested code in your browser you would get the following output:

1
2
3
4
5
6
7
8
9
10

That's what I was looking for! okay, so that's how it's done. I hope this helps someone else besides me.

What is WooCommerce API Manger?

WooCommerce API Manger is used to secure your software with API License Key activation, deactivation and automatic updates of WordPress plugins and/or themes. However it can also be used for some applications outside of WordPress as well, but we will be talking mostly about how to use it with a custom WordPress plugin.

Prerequisites

You need to at least make sure you have at least PHP Version 7.00. Here's how you can check from the linux command line:

php -v

Type the above command and press enter and you should get results that look similar to this:

Alternative Solution to WooCommerce API Manager

You may have noticed this article isn't complete. That is because I decided to go with a different system for selling WordPress plugins on my site with API keys, WooCommerce didn't provide the level of support I needed and their documentation was poor. I discovered Easy Digital Downloads which was much simpler to deploy. Easy Digital Downloads(EDD) in combination with the EDD Software Licensing add-on and the EDD Instant Updater for plugins that I developed myself, became the ultimate solution for selling WordPress plugins outside of WordPress.org. If anyone is interested in how I did it, email me at linian11@yahoo.com and I can assist you.

So, I spent a good 3+ hours doing what I thought would be a simple task today. I was working on a custom WordPress plugin named "Cultivate" for a client and made some additions to the admin menu in wp-admin. I added a Main menu item with the plugin name on it and there were 8 sub-menu items under it with links to various admin pages for the plugin.

The Problem

The problem is that the cultivate main menu item should stay open when someone clicks on one of the sub-menu items, but doesn't. So let me demonstrate with a couple of images of the admin area in WordPress. Here's an image of how the admin nav looks just after I clicked on the main menu item "Cultivate". As you can see in the below image, the Cultivate menu is open and you can see the 8 submenu items below it. When it is closed all you see if the "Cultivate" menu item, but here it is open:

Okay, that's all good and fine. You can see the sub-menu items and you can see "Cultivate" main menu item is highlighted because that page is now open. So, everything looks great! However, below is another image of how it looks after clicking on one of the sub-menu links below the "Cultivate" main menu item. In the next image, I have just clicked on the "Pods" sub-menu item under "Cultivate":

Now do you see the problem? Not only is the main "Cultivate" menu item closed, but the "Pods" sub-menu item isn't even present. Pods should be visible and highlighted which is not possible without the sub-menu being opened! So, after hours of research and coding, I finally figured out how to make this custom wp-admin menu work as it should. Here is how it looks after clicking on "Pods" under "Cultivate" after I added my code which I'll show you shortly:

See how in the above image, the main "Cultivate" menu remained open and the "Pods" sub-menu item is highlighted after going to the custom admin page? That's how it should work! That's how it works after adding my custom PHP and jQuery code shown below.

The Solution

The solution is a block of PHP code added to your plugin file or functions.php file and a custom jQuery script for each sub-menu item. It might sound like a lot to make a separate .js file for each sub-menu item, but you only have to change one word in each file, so not a huge deal really.

The PHP Code

Here's the PHP code you can add to your main plugin file or to functions.php if you're adding it to a theme:

First we will just add the following PHP code, then below that you can see the complete script, but you'll want to run this partial script first to get your page names for the if statements to function properly:

Now with the above code installed in your main plugin file or functions.php file, you'll also need to add a file to your plugin's main directory named my_log.txt and give it write permissions. This way it will write the page names and any possible taxonomy or post types you need to insert later in the script to the log file when you click on the admin nav links. So you click on a sub link, then see what was written to the log file. Repeat for ea. sub link. and write down the values written to the log file. Then you use what you wrote down to plan your PHP if statements correctly according to the page, post type and taxonomies used.

With that said, here's the entire script that worked for my Main menu item with 8 sub menu items below it"

Now the jQuery Code:

You'll need one .js file with the following contents for each sub-menu item you have in your admin nav. All you have to change in the text that reads "MailChimp Admin" in my example below to the link text of the sub-menu item for ea. separate .js file you use:

(function($) { // Close all the other parent menus $('.wp-has-current-submenu').removeClass('wp-has-current-submenu');

Nothing like a good Unterminated String Literal Error to fog up your day huh? Well I found a neat little trick to squash this nasty bug with JavaScript today!

The Problem

The problem in my case is that I wanted to place the contents of a PHP variable, filled with data from a database table, into a JavaScript variable which I thought could be easily done like in the following JavaScript code example:

var sc = "<?php echo $email_content; ?>";

WRONG! I discovered that when the PHP variable holds text with newline characters in it, like you'll often encounter when retrieving data from a database or from a textarea, or even a file, JavaScript throws the nasty old "Unterminated string literal" error! I've learned to hate this general JavaScript error over the years, but today, I stumbled upon a small, yet useful, trick that makes this error go away in cases like this.

The Solution:

Believe it or not, the solution is as simply as replacing quotes in the JavaScript code with backticks. If you don't know what a backtick is, read on, otherwise your JavaScript code should look like this after replacing quotes with backticks:

var sc = `<?php echo $email_content; ?>`;

What is a Backtick?

A backtick is the little character that looks like a slanted single quotation mark that normally calls the key above the tab key home on a normal English keyboard. Otherwise known as the Marigold key, grave accent or backquote, the character is a tricky one simply because no one knows what to call it. However, in the land of computer programming, it is commonly referred to as the backtick character, so that's what we'll call it. Cool? Okay!

So I'm going to cover a lot in a little time in this tutorial so pay close attention and you'll learn fast I hope.

What You'll Learn

How to create a simple WordPress plugin

How to create a custom post type in WordPress

How to add custom fields to a custom post type

How to add meta boxes to wp-admin

How to save custom post type's custom fields

How to handle form validation for custom meta boxes

How to display errors in wp-admin

So while it might seem like a lot to cover, I will demonstrate all of the above points with a simple WordPress plugin that adds a custom post type to WordPress and validates the user's input.

Here are the steps you'll follow:

Start a new plugin

Add a new custom post type

Add meta boxes to a custom post type

Add WordPress Error Handler Functions

Write PHP Function to Save Custom Fields Data Meta Boxes

Step One - Start a new plugin

Create a folder on your desktop, pick a name for your plugin and name the folder accordingly. Lets say for the purpose of making this tutorial, we'll name our plugin "Cars" so we created a folder named "cars" on our desktop. Then add a php file named cars.php and make enter the proper comments at the top of the cars.php file so WordPress will recognize your new plugin. Here's how I did it:

Step Five - Save Meta Box Data in WordPress

Now that we have our error handling functions ready, we can write in our function to save the data. You can copy and paste my save_car_data function and it's action hook as follows:

//############### SAVE ALL ABOVE METADATA FROM META BOXES HERE: ##################//hook to save the meta box data://add_action('save_post', 'save_touchpoint_data');add_action('save_post_cars', 'save_car_data');function save_car_data($post_id) { //get the car post's title title and verify it first: $ti = get_the_title($post_id); //if the title is blank, throw an error: if($ti=='')car_error('title_missing','Oops! You forgot to enter a title. Please enter a valid car title and click the update button.');

//save the car's year: //first make sure they picked a year: $y = $_POST['car_year']; if($y == 'none'){ car_error('missing_car_year','Opps! You have not selected a year for the car. Please select a year and click the update button to save it afterwards.'); }else{ update_post_meta($post_id, '_car_year', strip_tags($y)); } //save the car's make: //first make sure they picked a make: $y = $_POST['car_make']; if($y == 'none'){ car_error('missing_car_make','Opps! You have not selected a make for the car. Please select a make and click the update button to save it afterwards.'); }else{ update_post_meta($post_id, '_car_make', strip_tags($y)); } //save the car's model: //first make sure they picked a model: $y = $_POST['car_model']; if($y == 'none'){ car_error('missing_car_model','Opps! You have not selected a model for the car. Please select a model and click the update button to save it afterwards.'); }else{ update_post_meta($post_id, '_car_model', strip_tags($y)); }}//end save_car_data function

That's all there is to it! Copy and paste the above code into your cars.php file just before the closing PHP tag and save the file, then upload the entire cars folder to your site's plugins directory and navigate to wp-admin and click on "plugins" then find your new plugin and click "activate" to activate it and you will see the new custom post type, "Cars" appear in the main wp-admin nav that runs down the left side of the page.

Screen Shots:

Here is what it will look like if you just try to click on "Publish" when adding a new car without entering any values into the add new car page at all:

Below is what the add new Car screen looks like:

Summary:

There you have it! A simple plugin with everything you need to get you started creating your own custom post types in WordPress that include custom fields, meta boxes and even error handling code! A WordPress beginner's dream! Good Luck with your next theme or plugin!

Learning how to sort a multi-dimensional associative array in PHP was a bit difficult for me to figure out, so I hope documenting it here will help others figure it out a little faster.

In many cases, sorting multi-dimensional arrays is used to sort data retrieved from a database. For this exercise, let's say we have the following database structure:

The above image is from a database I was working on for a client while building a custom WordPress plugin in PHP. I needed to present the data from the table in order according to two fields, "term" and "qty". The task in my case was extra difficult because it also had to be sorted with a custom function. First we will do a simple sort with built in functions and then I'll explain how I solved my custom sorting issue.

Fetching Data From WordPress Database to Build an Associative Array

I was building a WordPress plugin when I encountered the need for this code, so that is what I'm using as an example, but the code is basically the same if you do it outside of WordPress also. The only difference would be how you do the database query. I am leaving the WordPress code in tact in case some of my readers could benefit from it. We have to build the array first. I'll provide test code below to build an array without having to query the database for those of you who may need it. Here is the PHP code I wrote to fetch data from a custom table in WordPress

This was a relatively short ans sweet tutorial on PHP arrays as far as such things often go. There is however a lot more to learn when it comes to working with arrays in PHP. To see all of the PHP array related functions in one place, check out this link:

Today, I had the unfortunate pleasure of working with Contact Form 7. I must admit that it was a little refreshing to see such an under-developed WordPress plugin for a change. I've gotten used to the over-developed monstrosities of today, but CF7 is truly bare-bones when it comes to functionality. It does on basic task and, I assume, does it well, since its such a popular WordPress plugin today. Contact Form 7 allows you to set up a contact form on your website that will email specific information you set up in the form to collect. It is supposed to be simple and sometimes it is. If it works the first time out of the box, you're one of the lucky ones. I had issues. The issues I had were not necessarily the plugin's fault, however, they could have saved me some time with more complete documentation regarding what to do when it fails. My issue ended up being that PHP mail function wasn't functioning, so I downloaded an SMTP plugin to resolve the issue before continuing to develop the code in this guide.

How to retrieve information submitted in Contact Form 7 Forms

The goal of this guide is to teach you how to retrieve data submitted in CF7 forms and work with it in a plugin or from your functions.php file if you prefer it that way.

Why Retrieve Data from Contact Form 7 Forms?

There are a lot of good answers to this question, but it basically depends on your individual needs and desires. The best answer perhaps is to save the form data in a database or log file because CF7 doesn't store any data from form submissions! I've heard there are plugins or add-ons for CF7 that enable CF7 to store it's data in a database, but for the sake of learning how to retrieve data, we won't be using one of those plugins today. Actually, we won't even store the info in a database. Everybody should know how to do that if they are advanced enough to be attempting this guide, so we will save form data to a log file instead, just because, as far as I know, there is no guide available that tells you how to do that as of the time I am writing this guide.

Okay that's enough on why, you can think of your own reason why....let's get to the how!

Retrieving Data From CF7 Forms

The first thing we need to accomplish is to hook into the form when it is submitted somehow. I've found that the action hook named "wpcf7_before_send_mail" works great for this purpose. Here is how to use wpcf7_before_send_mail:

...that is your basic action and call back function set up. Now all we need is to add some code inside of the empty process_contact_form_data function. We need to gather data submitted in the Contact Form 7 form, so let's look at how we can do that, shall we?

An integral class used in retrieving form data since CF7 Version 3.9 is known as the "WPCF7_Submission" class which includes the "get_instance()" method used to fetch data arrays. To be complete you should check for the class and then use get_instance() to fetch the data like this:

What the above code does is puts the posted form data into an array named $formdata. To get a specific form field's data you need to use the field's name attribute as a key to the $formdata array. For example, if you used the default CF7 form setup, you would access the submitted name, email, subject and message like this:

$name = $formdata['your-name'];

$email= $formdata['your-email'];

$subj = $formdata['your-subject'];

$name = $formdata['your-message'];

The above code would go inside the above function just before the closing bracket, then you'll need code to write those variables to a log file as we discussed earlier. The basic code to write to a file from PHP looks like this:

Okay! Now we just have to put all the pieces together inside the process_contact_form_data PHP function inside of your plugin file or functions.php file. To keep things safe, I suggest making your own little plugin for this, so that's what I'll do next, create a single file plugin that simply writes CF7 form data to a log file inside the plugin's main folder. Let's call our plugin CF7_logger.

You can easily make the plugin described by piecing together the code snippets in this guide, or you can purchase the entire tested and debugged version from me by emailing linian11@yahoo.com. Good Luck!