0

I need to load a meta box for editors to choose the parent post first before editing. But dont see the wide meta box i expect and the normal edit page just loads: This is the code at the begining of wp-admin/post-new.php :

            if ( $post_type == 'news' && !isset($_GET['parent_id']) ):
                       //checking if parent_id exists on querystring

            //Give them the news meta box only
            add_action('add_meta_boxes', function() { add_meta_box('news-parent', 'Please select the Parent Project for this news:', 'news_attributes_meta_box', 'news', 'normal', 'high');});//add wide meta box

              function news_attributes_meta_box($post) {
                  $parentselectbox = wp_dropdown_pages(array(
                  'post_type' => 'project',
                  'name' => 'parent_id',
                  'show_option_none' => __('(no parent)'),
                  'sort_column'=> 'menu_order,
                  post_title',
                  'echo' => 0
                  ));
                  if ( ! empty($parentselectbox) ) {
                    echo $parentselectbox;
                  } // end empty pages check
                };

            exit;//stop page load after meta box is loaded
            //if chosen, give a move forward link with parent_id on querystring
            endif;

1 Answer 1

0

You shouldn't ever be modifying any of the WordPress core files, your changes will be lost as soon as the next WordPress update is released.

The easiest way to do this would be with Advanced Custom Fields, create a "relationship" custom field type and make it a required field. That way the user can't save the post without selecting a "parent post".

If, however, you really need to show the meta box before the rest of the edit window loads (which I would advise against since I think it breaks what most people would expect as a standard interface), you can still create the post meta boxes inside your functions.php file. See here for a great tutorial: http://wp.smashingmagazine.com/2011/10/04/create-custom-post-meta-boxes-wordpress/.

Once you have the metabox, you could use some simple javascript to hide all of the page elements until something has been selected in the metabox, for example (untested):

$(document).ready(function() {
    $('div:not(#mypostboxid)').fadeTo("slow", 0.33); // fade the page to .33 opacity
    $('div:not(#mypostboxid)').find('input, textarea, button, select').attr('disabled','disabled'); // disable all other inputs
    $('.myselectbox').change(function() {
         $('div:not(#mypostboxid)').fadeTo("slow", 1); // fade back to visible
         $('div:not(#mypostboxid)').find('input, textarea, button, select').attr('disabled','enabled'); // re-enable inputs
    }
});
4
  • Good advice but I have a plugin to stop wordpress auto-update. Then the interface is ok as required. Please just look through my code and see how I can make it work. Alternatively, offer some javascript that would pop up or focus the meta box once the post-new page is loaded. Thank you.
    – Timothy
    Commented Mar 6, 2013 at 14:47
  • See the edit above for a simple jQuery solution. You may want to move the box around on the page, etc., this can all be done very easily with a little research. Again, I have to stress not modifying the WordPress core. It's fine for now, but sooner or later your plugins are going to stop working with an outdated version of WP, and you may expose your users to serious security vulnerabilities.
    – bcorkins
    Commented Mar 6, 2013 at 15:17
  • Thank you once again, and great advice on security. Where do i add this jQuery ?
    – Timothy
    Commented Mar 8, 2013 at 3:52
  • You'll want to place the script in a .js file and then enqueue it on the admin pages. See here codex.wordpress.org/Plugin_API/Action_Reference/… for more info, or google it, there are many examples.
    – bcorkins
    Commented Mar 8, 2013 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.