0

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>&nbsp;&nbsp;';
                                    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 '&nbsp;&nbsp;'.$option.': <input type="checkbox" name="' . $this->prefix . $customField['name'] . '[]" id="' . $this->prefix . $customField['name'] . '" value="'.$option.'" checked="checked" style="width: auto;" />';
                                        else
                                            echo '&nbsp;&nbsp;'.$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="">&nbsp;</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

closed as off-topic by Mark Kaplun, TheDeadMedic, brasofilo, Pieter Goosen, Howdy_McGee Sep 25 '15 at 13:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Interesting question, but you should at least try to find how to insert ACF data programmatically. With that in hands, it should be a matter of looping through posts (by plugin not by sql) and convert the data. I'd search their forums as probably someone already dealt with that. – brasofilo Sep 24 '15 at 0:20
1

I'm at the end of a project exactly like that, and it's a bit tricky, but it can be done.

  1. Using a CSV export/import plugin, (I've tried a few, but ended up with WP Ultimate CSV Importer) export the current data to a CSV file.
  2. Create the custom fields you want to use to replace existing fields.
  3. Create a sample post with the new ACF fields.
  4. Export that data to a CSV file.

From there, all you have to do is match the column headers from the new CSV file and paste them into the matching headers of the old data columns. This part was a bit tricky as I had a combination of ACF and standard fields, but if you get the column headers right, it works beautifully.

I did test imports into MAMP before trying it on the live site. Once you figure out exactly what the new headers are, the import will work (almost) perfectly.

The only issue I've had is that some of the columns don't show up in the admin tables initially, (I'm still researching that because bulk editing doesn't fix it and with 3000 entries, manually saving each one… nightmare), but once each post is saved, it will show up—the data does show up in the admin page, and when queried from the templates, so it's a small issue.

  • Hi Vanessa, Excellent suggestion I will give this a go! Just to clarify when talk about "admin tables" what are you referring to, wp-admin? – James Sep 24 '15 at 17:45
  • I thought of the start of 15 complicated answers to this. All terrible—export/import makes way more sense. For generating the ACF, you might look at scripting it based on the existing fields advancedcustomfields.com/resources/register-fields-via-php – Will Sep 24 '15 at 21:51
  • Hi James, sorry if that wasn't clear… The tables I was referring to are the ones you see when you go to say, All Posts or All Pages in the WordPress dashboard. I used the WordOress fields for the titles and taxonomies, so those showed, but until I went into an individual post's edit page and saved it, the rest of the custom fields I built into the admin tables didn't. Not a big issue, technically, but some clients may not understand. I'll update this if I find a solution. – Vanessa King Sep 24 '15 at 23:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.