I have this code in functions.php in order to have a meta box displayed in the backend. The problem is, this was functioning normally until I've moved the website from a subdomain (dev) to the root and now even if the metabox appears in the backend, it doesn't retain the values anymore.
<?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')));
<?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()
{
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
if ($post_id == '2') {
foreach (array('post','page') as $type)
{
add_meta_box('my_all_meta_home', 'Additional info', 'my_meta_setup', $type, 'normal', 'high');
}
}
add_action('save_post','my_meta_save');
}
function my_meta_setup()
{
global $post;
$meta = get_post_meta($post->ID,'_my_meta',TRUE);
include(MY_THEME_FOLDER . '/meta.php');
echo '<input type="hidden" name="my_meta_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
}
function my_meta_save($post_id)
{
if (!wp_verify_nonce($_POST['my_meta_noncename'],__FILE__)) return $post_id;
if ($_POST['post_type'] == 'page')
{
if (!current_user_can('edit_page', $post_id)) return $post_id;
}
$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;
}
}
}
Would you please help me understand what's wrong? I can't figure this out and is quite frustrating. The code is not changed at all, the output for my metabox is in another php file in the theme root, nothing is changed really.
The only thing I can think of, is that developing the website on a subdomain, when I've copied the files to the root I didn't create another database as I didn't need one and have used the one I had and then used a plugin to change all the URL's. And then I've used a plugin to search and replace URL values in the database, but this was happening before altering the database. I can't be this anyway.
When I edit the metabox in the back end, I have no error, the post/page is updated, but it's obvious the database doesn't retain the new values and after editing the fields are emtpy. Here's what I have in the meta.php:
<input type="text" name="_my_meta[title]" value="<?php if(!empty($meta['title'])) echo $meta['title']; ?>"/>
<span>Enter in a title</span>
<label>Body text</label>
<textarea name="_my_meta[body-text]" rows="5"><?php if(!empty($meta['body-text'])) echo $meta['body-text']; ?></textarea>
<span>Enter in the body text</span>
<label>Link</label>
<input type="text" name="_my_meta[link]" value="<?php if(!empty($meta['link'])) echo $meta['link']; ?>"/>
<span>Enter in the URL of the link</span>
<span>e.g. <code><a href='services/' > View our Services page < /a ></code></span>
Later edit:
This is how I get my metabox values:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$array = get_post_meta($postid,'_my_meta',TRUE);
echo "<h1>"; echo $array['title']; echo "</h1>";
echo '<p>'; echo $array['body-text']; echo '</p>';
echo $array['link']; ?>
get_template_directory();
to get the Parent theme's directory (andget_stylesheet_directory()
if you are after the child theme directory). See Codex.my_meta_save
, on failure, die with some message:wp_die('Reason for failure')
to determine which check you are failing on. Also, you should check its not an auto-save routine.<input type="text" name="_my_meta[title]" value="<?php if(!empty($meta['title'])) echo $meta['title']; ?>"/>
, do I have to define $meta in meta.php?