Note that there are some explanatory texts on larger screens.

plurals
  1. POWordpress add_meta_box() weirdness
    text
    copied!<p>The code below is working nearly flawlessly, however my value for page title on one of my pages keeps coming up empty after a few page refreshes... It sticks for awhile, then it appears to reset to empty. I'm thinking I must have a conflict in the code below, but I can't quite figure it.</p> <p>I'm allowing the user to set a custom page title for posts as well as pages via a custom "post/page title input field). Can anyone see an obvious issue here that might be resetting the page title to blank?</p> <pre><code>// =================== // = POST OPTION BOX = // =================== add_action('admin_menu', 'my_post_options_box'); function my_post_options_box() { if ( function_exists('add_meta_box') ) { //add_meta_box( $id, $title, $callback, $page, $context, $priority ); add_meta_box('post_header', 'Custom Post Header Code (optional)', 'custom_post_images', 'post', 'normal', 'low'); add_meta_box('post_title', 'Custom Post Title', 'custom_post_title', 'post', 'normal', 'high'); add_meta_box('post_title_page', 'Custom Post Title', 'custom_post_title', 'page', 'normal', 'high'); add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core'); add_meta_box('categorydiv', __('Page Options'), 'post_categories_meta_box', 'page', 'side', 'core'); } } //Adds the custom images box function custom_post_images() { global $post; ?&gt; &lt;div class="inside"&gt; &lt;textarea style="height:70px; width:100%;margin-left:-5px;" name="customHeader" id="customHeader"&gt;&lt;?php echo get_post_meta($post-&gt;ID, 'customHeader', true); ?&gt;&lt;/textarea&gt; &lt;p&gt;Enter your custom html code here for the post page header/image area. Whatever you enter here will override the default post header or image listing &lt;b&gt;for this post only&lt;/b&gt;. You can enter image references like so &amp;lt;img src='wp-content/uploads/product1.jpg' /&amp;gt;. To show default images, just leave this field empty&lt;/p&gt; &lt;/div&gt; &lt;?php } //Adds the custom post title box function custom_post_title() { global $post; ?&gt; &lt;div class="inside"&gt; &lt;p&gt;&lt;input style="height:25px;width:100%;margin-left:-10px;" type="text" name="customTitle" id="customTitle" value="&lt;?php echo get_post_meta($post-&gt;ID, 'customTitle', true); ?&gt;"&gt;&lt;/p&gt; &lt;p&gt;Enter your custom post/page title here and it will be used for the html &amp;lt;title&amp;gt; for this post page and the Google link text used for this page.&lt;/p&gt; &lt;/div&gt; &lt;?php } add_action('save_post', 'custom_add_save'); function custom_add_save($postID){ if (!defined('DOING_AUTOSAVE') &amp;&amp; !DOING_AUTOSAVE) { return $postID; } else { // called after a post or page is saved and not on autosave if($parent_id = wp_is_post_revision($postID)) { $postID = $parent_id; } if ($_POST['customHeader']) { update_custom_meta($postID, $_POST['customHeader'], 'customHeader'); } else { update_custom_meta($postID, '', 'customHeader'); } if ($_POST['customTitle']) { update_custom_meta($postID, $_POST['customTitle'], 'customTitle'); } else { update_custom_meta($postID, '', 'customTitle'); } } } function update_custom_meta($postID, $newvalue, $field_name) { // To create new meta if(!get_post_meta($postID, $field_name)){ add_post_meta($postID, $field_name, $newvalue); }else{ // or to update existing meta update_post_meta($postID, $field_name, $newvalue); } } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload