I have recently taken over a wordpress site that needs some serious renovating. The previous developer created custom fields himself in functions.php. I am serious advocate of Advanced Custom Fields, the power of this plugin far outweighs that of self coded custom fields.
My plan is to try and find a way off transferring the data from the current custom fields to acf fields. This will be an extremely tedious job if I cannot find a way of automating the process, my first thought is editing the SQL tables but would like to know if anyone has faced a similar problem before?
Below you can see an example of how the custom fields are coded within functions.php (a very ugly method imo)
if ( !class_exists('myCustomFieldsgeneral') ) {
class myCustomFieldsgeneral {
/**
* @var string $prefix The prefix for storing custom fields in the postmeta table
*/
var $prefix = '_mcf_';
/**
* @var array $customFields Defines the custom fields available
*/
var $customFields = array(
array(
"name" => "seoblock",
"title" => "Block This Page From Search Engines",
"description" => "If this page is purely for PPC or serves no purpose on the organic site, please select yes.",
"options" => array("Yes","No"),
"type" => "select",
"scope" => array( "post", "page" ),
"capability" => "edit_posts"
),
);
/**
* PHP 4 Compatible Constructor
*/
function myCustomFieldsgeneral() { $this->__construct(); }
/**
* PHP 5 Constructor
*/
function __construct() {
add_action( 'admin_menu', array( &$this, 'createCustomFieldsgeneral' ) );
add_action( 'save_post', array( &$this, 'saveCustomFieldsgeneral' ), 1, 2 );
}
/**
* Remove the default Custom Fields meta box
*/
function removeDefaultCustomFields( $type, $context, $post ) {
foreach ( array( 'normal', 'advanced', 'side' ) as $context ) {
remove_meta_box( 'postcustom', 'post', $context );
remove_meta_box( 'postcustom', 'page', $context );
//Use the line below instead of the line above for WP versions older than 2.9.1
//remove_meta_box( 'pagecustomdiv', 'page', $context );
}
}
/**
* Create the new Custom Fields meta box
*/
function createCustomFieldsgeneral() {
if ( function_exists( 'add_meta_box' ) ) {
add_meta_box( 'my-custom-fields-general', 'General Information', array( &$this, 'displayCustomFieldsgeneral' ), 'page', 'normal', 'high' );
add_meta_box( 'my-custom-fields-general', 'General Information', array( &$this, 'displayCustomFieldsgeneral' ), 'post', 'normal', 'high' );
}
}
/* --------- Display the new Custom Fields meta box --------- */
function displayCustomFieldsgeneral() {
global $post;
?>
<div class="form-wrap">
<?php
wp_nonce_field( 'my-custom-fields-general', 'my-custom-fields-general_wpnonce', false, true );
foreach ( $this->customFields as $customField ) {
// Check scope
$scope = $customField[ 'scope' ];
$output = false;
foreach ( $scope as $scopeItem ) {
switch ( $scopeItem ) {
case "post": {
// Output on any post screen
if ( /*basename( $_SERVER['SCRIPT_FILENAME'] )=="post-new.php" ||*/ $post->post_type=="post" )
$output = true;
break;
}
case "page": {
// Output on any page screen
if ( /*basename( $_SERVER['SCRIPT_FILENAME'] )=="page-new.php" ||*/ $post->post_type=="page" )
$output = true;
break;
}
case "go page": {
// Output on any page screen
if ( /*basename( $_SERVER['SCRIPT_FILENAME'] )=="post.php" &&*/ $post->post_title=="go" )
$output = true;
break;
}
case "go2 page": {
// Output on any page screen
if ( /*basename( $_SERVER['SCRIPT_FILENAME'] )=="post.php" &&*/ $post->post_title=="go2" )
$output = true;
break;
}
case "goc page": {
// Output on any page screen
if ( /*basename( $_SERVER['SCRIPT_FILENAME'] )=="post.php" &&*/ $post->post_title=="goc" )
$output = true;
break;
}
case "gored page": {
// Output on any page screen
if ( /*basename( $_SERVER['SCRIPT_FILENAME'] )=="post.php" &&*/ $post->post_title=="gored" )
$output = true;
break;
}
case "landing page": {
// Output on any page screen
if ( /*basename( $_SERVER['SCRIPT_FILENAME'] )=="post.php" &&*/ $post->post_type=="Landing Page" )
$output = true;
break;
}
case "confirmation page": {
// Output on any page screen
if ( /*basename( $_SERVER['SCRIPT_FILENAME'] )=="post.php" &&*/ $post->post_type=="Confirmation Page" )
$output = true;
break;
}
}
if ( $output ) break;
}
// Check capability
if ( !current_user_can( $customField['capability'], $post->ID ) )
$output = false;
// Output if allowed
if ( $output ) { ?>
<div class="form-field form-required <?php echo '' . $customField[ 'name' ] .'"';?> >
<?php
switch ( $customField[ 'type' ] ) {
case "checkbox": {
// Checkbox
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><strong>' . $customField[ 'title' ] . '</strong></label> ';
foreach ( $customField['options'] as $option) {
$preselected = get_post_meta( $post->ID, $this->prefix . $customField['name']);
$selection = unserialize($preselected[0]);if (is_array($selection) && in_array($option,$selection))
echo ' '.$option.': <input type="checkbox" name="' . $this->prefix . $customField['name'] . '[]" id="' . $this->prefix . $customField['name'] . '" value="'.$option.'" checked="checked" style="width: auto;" />';
else
echo ' '.$option.': <input type="checkbox" name="' . $this->prefix . $customField['name'] . '[]" id="' . $this->prefix . $customField['name'] . '" value="'.$option.'" style="width: auto;" />';
}
break;
}
case "textarea" : {
// Text area
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><strong>' . $customField[ 'title' ] . '</strong></label>';
echo '<textarea name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" columns="30" rows="3">' . htmlspecialchars( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '</textarea>';
break;
}
case "radio" : {
// Radio buttons
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><strong>' . $customField[ 'title' ] . '</strong></label>';
foreach ( $customField['options'] as $option) {
$preselected = get_post_meta( $post->ID, $this->prefix . $customField['name']);
if ($preselected[0] == $option)
echo ''.$option.'<input type="radio" name="'.$this->prefix.$customField['name'].'" id="'.$this->prefix.$customField['name'].'" checked="checked" value="'.$option.'" /><br />';
else
echo ''.$option.'<input type="radio" name="'.$this->prefix.$customField['name'].'" id="'.$this->prefix.$customField['name'].'" value="'.$option.'" /><br />';
}
break;
}
case "select" : {
// Select dropdown
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><strong>' . $customField[ 'title' ] . '</strong></label>';
echo '<select name="' . $this->prefix . $customField[ 'name' ] .'" id="' . $this->prefix . $customField[ 'name' ] .'">';
echo '<option value=""> </option>';
foreach ( $customField['options'] as $option) {
$preselected = get_post_meta( $post->ID, $this->prefix . $customField['name']);
if ($preselected[0] == $option)
echo '<option selected="selected" value="'.$option.'">'.$option.'</option>';
else
echo '<option value="'.$option.'">'.$option.'</option>';
}
echo '</select>';
break;
}
default: {
// Plain text field
echo '<label for="' . $this->prefix . $customField[ 'name' ] .'"><strong>' . $customField[ 'title' ] . '</strong></label>';
echo '<input type="text" name="' . $this->prefix . $customField[ 'name' ] . '" id="' . $this->prefix . $customField[ 'name' ] . '" value="' . htmlspecialchars( get_post_meta( $post->ID, $this->prefix . $customField[ 'name' ], true ) ) . '" />';
break;
}
}
?>
<?php if ( $customField[ 'description' ] ) echo '<p>' . $customField[ 'description' ] . '</p>'; ?>
</div>
<?php
}
} ?>
</div>
<?php
}
/**
* Save the new Custom Fields values
*/
function saveCustomFieldsgeneral( $post_id, $post ) {
if ( !wp_verify_nonce( $_POST[ 'my-custom-fields-general_wpnonce' ], 'my-custom-fields-general' ) )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
if ( $post->post_type != 'page' && $post->post_type != 'post' && $post->post_type != 'Landing Page' && $post->post_type != 'Confirmation Page' )
return;
foreach ( $this->customFields as $customField ) {
if ( current_user_can( $customField['capability'], $post_id ) ) {
if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) ) {
if (!is_array($_POST[$this->prefix . $customField[ 'name' ]]))
update_post_meta( $post_id, $this->prefix . $customField[ 'name' ], $_POST[ $this->prefix . $customField['name'] ] );
else {
$_POST[$this->prefix . $customField[ 'name' ]] = serialize($_POST[$this->prefix . $customField[ 'name' ]]);
update_post_meta( $post_id, $this->prefix . $customField[ 'name' ], $_POST[ $this->prefix . $customField['name'] ] );
}
} else
delete_post_meta( $post_id, $this->prefix . $customField[ 'name' ] );
}
}
}
} // End Class
} // End if class exists statement