Part ONE -- changing the way Custom Fields are named 3.05.1
ClassiPress 3.05.1 and upcoming 3.1 could use, in my opinion, a very basic but important change that would liberate the Ad Display capabilities.
I modded the admin-options.php file of 3.05.1 an hour ago so that I could continue using part of a mod I made for 3.0.4.
Now that ClassiPress finally has the ability to create checkboxes and radio buttons there's just one little IMPORTANT detail that is missing. The way that the Create New Custom Fields action needs one little rewrite when it comes to NAMING the field.
ClassiPress now and in the past took the field label name you typed in such as "Extra Features" and when the new Custom field was Saved it forever named that field "cp_extra_features" and it was saved in the cp_ad_fields table as "field_name" : cp_extra_features.
So, when you assigned this custom field to a special form and put it into use the resulting ad stored in wp_postmeta would call up the stored values your user had input from that custom field whose "meta_key" stored the name "cp_extra_features" followed by the associated "meta_values"--whatever they were.
BUT, suppose that meta_key actually also contained the information about what FIELD TYPE these meta_values were saved in and to be retrieved for page display.
Suppose that your custom field "cp_extra_features" had been saved by your choice as a CHECKBOX, as a for instance. And the meta_values in the the wp_postmeta table read like this "Frost-free freezer,new dishwasher,laundry room,carport parking,broadband cable internet"
What if the associated meta_key value stored in wp_postmeta table said instead of "cp_extra_features"...... this: "cp_checkbox_extra_features"?
And what if every single custom field that you created said in the wp_postmeta table "cp_checkbox_special_options" or "cp_dropdown_choices2" or "cp_radio_district_choices" ?? etc. etc.....
Then in one lookup you could create a customized way of styling the page output of individual ads. The first lookup could aggregate all the custom fields for the upper right hand Ad DETAILS but EXCLUDE, for instance, your CHECKBOX values because you do not want them displayed as a single string of text separated by commas.
You could create a separate lookup function that ONLY INCLUDED the Checkbox custom fields and their meta values and output them as unordered lists anywhere on the single ad listing page you wanted, or even simply output them in the right hand sidebar.
And, as long as the custom field meta_key value in the wp_postmeta table CONTAINS the actual field type name then you can customize look up and display functions to exclude or include just those field types for special treatment in display or page placement. Text, Text area, dropdown, radio buttons, checkboxes can now be styled differently and even displayed in different places on the page if that is useful to you.
So, for this part I am just going to show you the slight mods to the admin-options.php file of ClassiPress 3.0.5.1 that will from this point create a better naming standard (in my opinion) when you create Custom fields. This will not, of course, automatically rename custom fields created before this mod would be installed. However, a little work with phpmyadmin could revamp the names of previous custom fields.
admin/admin-options.php
line 132
look for the following section--this is the section that will be replaced:
/**
* Take field input label value and make custom name
* Strip out everything excepts chars & numbers
* Used for WP custom field name i.e. Middle Name = cp_middle_name
*/
function cp_make_custom_name($cname) {
$cname = preg_replace('/[^a-zA-Z0-9\s]/', '', $cname);
$cname = 'cp_' . str_replace(' ', '_', strtolower(substr(appthemes_clean($cname), 0, 30)));
return $cname;
}
modified code update:
PHP Code:
/**
* Take field input label value and make custom name
* Strip out everything excepts chars & numbers
* Used for WP custom field name i.e. Middle Name = cp_middle_name
*/
function cp_make_custom_name($cname,$fieldtype) {
$cname = preg_replace('/[^a-zA-Z0-9\s]/', '', $cname);
$cname = 'cp_' .$fieldtype.'-' . str_replace(' ', '_', strtolower(substr(appthemes_clean($cname), 0, 30)));
return $cname;
}
line 1928:
it says--
case 'addfield':
this is my modification
PHP Code:
case 'addfield':
?>
<div class="wrap">
<div class="icon32" id="icon-themes"><br/></div>
<h2><?php _e('New Custom Field','appthemes') ?></h2>
<?php cp_admin_info_box(); ?>
<?php
// check and make sure the form was submitted
if(isset($_POST['submitted'])) {
$insert = "INSERT INTO " . $wpdb->prefix . "cp_ad_fields" .
" (field_name, field_label, field_desc, field_tooltip, field_type, field_values, field_search, field_owner, field_created) " .
"VALUES ('" .
$wpdb->escape(appthemes_clean(cp_make_custom_name($_POST['field_label'],$_POST['field_type']))) . "','" .
$wpdb->escape(appthemes_clean($_POST['field_label'])) . "','" .
$wpdb->escape(appthemes_clean($_POST['field_desc'])) . "','" .
$wpdb->escape(esc_attr(appthemes_clean($_POST['field_tooltip']))) . "','" .
$wpdb->escape(appthemes_clean($_POST['field_type'])) . "','" .
$wpdb->escape(appthemes_clean($_POST['field_values'])) . "','" .
$wpdb->escape(appthemes_clean($_POST['field_search'])) . "','" .
$wpdb->escape(appthemes_clean($_POST['field_owner'])) . "','" .
gmdate('Y-m-d H:i:s') .
"')";
$results = $wpdb->query($insert);
Please note in the second change that this is the one line that was MODIFIED:
Code:
$wpdb->escape(appthemes_clean(cp_make_custom_name($_POST['field_label'],$_POST['field_type']))) . "','" .
Proceed with caution and make sure that you are ready to replace the admin-options.php ORIGINAL file if you make some slight mistake in modifying it.
This mod does not affect the running of ClassiPress 3.05.1
It simply creates a change that sets up ClassiPress for Part 2 of my Tutorial.
In Part 2 I will show 2 functions that use my Part 1 mod to create Unordered list displays for all Checkbox Custom fields.
Last edited by vienna; November 23rd, 2010 at 11:35 AM.