我制作了一个简单的表单,我可以在WordPress post.php
页面中添加和更新自定义字段。
我使用的工作代码
我在主题 function.php :
的末尾添加了此代码define('MY_WORDPRESS_FOLDER',$_SERVER['DOCUMENT_ROOT']);
define('MY_THEME_FOLDER',str_replace('\\','/',dirname(__FILE__)));
define('MY_THEME_PATH','/' . substr(MY_THEME_FOLDER,stripos(MY_THEME_FOLDER,'wp-content')));
add_action('admin_init','my_meta_init');
function my_meta_init()
{
// review the function reference for parameter details
// http://codex.wordpress.org/Function_Reference/wp_enqueue_script
// http://codex.wordpress.org/Function_Reference/wp_enqueue_style
//wp_enqueue_script('my_meta_js', MY_THEME_PATH . '/custom/meta.js', array('jquery'));
wp_enqueue_style('my_meta_css', MY_THEME_PATH . '/custom/meta.css');
// review the function reference for parameter details
// http://codex.wordpress.org/Function_Reference/add_meta_box
foreach (array('post','page') as $type)
{
add_meta_box('my_all_meta', 'My Custom Meta Box', 'my_meta_setup', $type, 'normal', 'high');
}
add_action('save_post','my_meta_save');
}
function my_meta_setup()
{
global $post;
// using an underscore, prevents the meta variable
// from showing up in the custom fields section
$meta = get_post_meta($post->ID,'_my_meta',TRUE);
// instead of writing HTML here, lets do an include
include(MY_THEME_FOLDER . '/custom/meta.php');
// create a custom nonce for submit verification later
echo '<input type="hidden" name="my_meta_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
}
function my_meta_save($post_id)
{
// authentication checks
// make sure data came from our meta box
if (!wp_verify_nonce($_POST['my_meta_noncename'],__FILE__)) return $post_id;
// check user permissions
if ($_POST['post_type'] == 'page')
{
if (!current_user_can('edit_page', $post_id)) return $post_id;
}
else
{
if (!current_user_can('edit_post', $post_id)) return $post_id;
}
// authentication passed, save data
// var types
// single: _my_meta[var]
// array: _my_meta[var][]
// grouped array: _my_meta[var_group][0][var_1], _my_meta[var_group][0][var_2]
$current_data = get_post_meta($post_id, '_my_meta', TRUE);
$new_data = $_POST['_my_meta'];
my_meta_clean($new_data);
if ($current_data)
{
if (is_null($new_data)) delete_post_meta($post_id,'_my_meta');
else update_post_meta($post_id,'_my_meta',$new_data);
}
elseif (!is_null($new_data))
{
add_post_meta($post_id,'_my_meta',$new_data,TRUE);
}
return $post_id;
}
function my_meta_clean(&$arr)
{
if (is_array($arr))
{
foreach ($arr as $i => $v)
{
if (is_array($arr[$i]))
{
my_meta_clean($arr[$i]);
if (!count($arr[$i]))
{
unset($arr[$i]);
}
}
else
{
if (trim($arr[$i]) == '')
{
unset($arr[$i]);
}
}
}
if (!count($arr))
{
$arr = NULL;
}
}
}
然后我在我的主题文件夹中创建了一个名为“ custom ”的文件夹,并在其中保存了这两个文件:meta.php和meta.css
meta.php 看起来像:
<div class="my_meta_control">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras orci lorem, bibendum in pharetra ac, luctus ut mauris. Phasellus dapibus elit et justo malesuada eget <code>functions.php</code>.</p>
<label>Name</label>
<p>
<input type="text" name="_my_meta[link]" value="<?php if(!empty($meta['link'])) echo $meta['link']; ?>"/>
<span>Enter in a name</span>
</p>
<label>Description <span>(optional)</span></label>
<p>
<textarea name="_my_meta[description]" rows="3"><?php if(!empty($meta['description'])) echo $meta['description']; ?></textarea>
<span>Enter in a description</span>
</p>
</div>
meta.css 看起来像:
.my_meta_control .description { display:none; }
.my_meta_control label { display:block; font-weight:bold; margin:6px; margin-bottom:0; margin-top:12px; }
.my_meta_control label span { display:inline; font-weight:normal; }
.my_meta_control span { color:#999; display:block; }
.my_meta_control textarea, .my_meta_control input[type='text'] { margin-bottom:3px; width:99%; }
.my_meta_control h4 { color:#999; font-size:1em; margin:15px 6px; text-transform:uppercase; }
使用上面的代码,我得到了这个结果:
现在,我可以在此处添加和更新名称和说明自定义字段,其中很棒。
这就是我想要的但问题是:
此框将使用来自_my_meta
的“名称”和“描述”,我不想要它。
以前,我已将自定义字段值保存到我发布的所有帖子中:phone
,home_city
,home_country
,name
++
我的问题是:
如何修改此代码以使用我已保存的自定义字段,而不是使用此新_my_meta
?如上所述,我的自定义字段名称是:phone
,home_city
,home_country
,name
++
答案 0 :(得分:1)
简单,您不需要_my_meta
。在此之前值得注意的是,您的示例使用此单个元键将多个值存储为serialized array,_my_meta[link]
和_my_meta[description]
。因此在反序列化后它看起来像这样:
'my_meta' = array(
'link' => 'some value',
'description' => 'another value'
)
问题是,使用单个自定义字段作为许多键/值的容器是切实可行的,但如果您计划使用这些字段搜索结果或按元键排序查询,则这不是很好。在这种情况下,最好使用单个字段。
正如您所说,您已经拥有自定义字段,很可能它们是单个字段,在这种情况下,您可以执行以下操作来调整代码。
在my_meta_setup()
:
$phone = get_post_meta( $post->ID, 'phone', TRUE );
$phone = !empty($phone) ? $phone : ''; // this makes the HTML cleaner
$home_city = get_post_meta( $post->ID, 'home_city', TRUE );
$home_city = !empty($home_city) ? $home_city : '';
?>
<input type="text" name="phone" value="<?php echo $phone; ?>"/>
<input type="text" name="home_city" value="<?php echo $home_city; ?>"/>
在my_meta_save()
:
$phone = get_post_meta( $post_id, 'phone', TRUE );
$home_city = get_post_meta( $post_id, 'home_city', TRUE );
update_post_meta( $post_id, 'phone', $new_phone );
update_post_meta( $post_id, 'home_city', $new_home_city );
其他说明:
将wp_enqueue_style
放入wp_enqueue_scripts
动作挂钩中。
避免使用constants
。我们可以使用get_stylesheet_directory_uri()
获取主题文件夹,使用get_stylesheet_directory()
获取路径。
WordPress路径已在ABSPATH
中定义,我们可以使用site_url()
获取的网址,但您可能不需要此。
动作挂钩save_post
有2个参数,第二个是$post
,我们不需要全局
add_action( 'save_post','my_meta_save', 10, 2 );
function my_meta_save( $post_id, $post ) {}