我最近接管了一个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> \';
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