将自制函数.php自定义域传递到高级自定义域

时间:2015-09-23 作者:James

我最近接管了一个wordpress网站,该网站需要进行一些认真的翻新。以前的开发人员自己在函数中创建自定义字段。php。我是高级自定义字段的拥护者,这个插件的威力远远超过了自编码自定义字段。

我的计划是设法将数据从当前自定义字段传输到acf字段。如果我找不到自动化过程的方法,这将是一项极其乏味的工作,我的第一个想法是编辑SQL表,但想知道以前是否有人遇到过类似的问题?

下面是自定义字段在函数中的编码示例。php(一种非常丑陋的方法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

1 个回复
SO网友:Vanessa King

我正处于这样一个项目的末尾,这有点棘手,但它是可以完成的。

使用CSV导出/导入插件(我尝试了一些,但最终使用WP Ultimate CSV Importer)将当前数据导出到CSVfile从这里开始,您所要做的就是匹配新CSV文件中的列标题,并将其粘贴到旧数据列的匹配标题中。这一部分有点棘手,因为我有ACF和标准字段的组合,但如果列标题正确,它会工作得很好。

在现场尝试之前,我测试了MAMP的导入。一旦您准确地知道新的头是什么,导入将(几乎)完美地工作。

我遇到的唯一问题是,一些列最初没有显示在管理表中(我仍在研究,因为批量编辑无法修复它,有3000个条目,手动保存每个条目……噩梦),但一旦保存了每篇文章,它会显示数据确实显示在管理页面中,当从模板查询时,这是一个小问题。

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: