0

First off, I have been able to successfully implement custom fields within a custom meta box in Wordpress. I understand how this works.

My question is, is there a way for me to add a custom field to an already existing meta box? For instance, I want to add field within Wordpress's "Author" meta box because it is directly related to the author of a post. I haven't found any documentation or examples of this, just many regarding adding an entirely new meta box. Is it possible to add a custom field to one of Wordpress's existing meta boxes?

Thanks.

1

I don't know if I'm missing something here but there are several ways to add Html to Existing core Author meta box and save a custom field -related to the post.

One option is to assign a new callback for the authordiv html content, or add the html by filtering the output of the author dropdown select list on this box.

I prefer the latter, in case some plugin already created a callback. The filter is applied just before the boxes are rendered, and removed immediately after the first occurence of wp_dropdown_users.

However, In any case, you need an action to the add_meta_boxes hook:

function ua_add_meta_boxes($post_type, $post){
    global $wp_meta_boxes;
    if(!isset($wp_meta_boxes[$post_type]['normal']['core']['authordiv'])) return;

    // Change the callback
    $wp_meta_boxes[$post_type]['normal']['core']['authordiv']['callback'] = 'my_callback_authordiv_html';

    // Or hook into the dropdown html created Html
    add_filter('wp_dropdown_users', 'ua_extend_metbox_authordiv', 10, 1);
}
add_action('add_meta_boxes', 'ua_add_meta_boxes', 5, 2);

In case callback:

function my_callback_authordiv_html(){
    // copy the default select stuff and add some more HTML of our own
}

In case filter the dropdown (with custom field example added):

function ua_extend_metbox_authordiv($html){

    global $post;
    remove_filter('wp_dropdown_users', 'ua_expand_author_box', 10, 1);
    $meta = get_post_meta($post->ID, 'my_custom field', true);

    $label = __('Check my checkbox', 'text-domain');
    $html .= '<input type="hidden" name="ua_author_metabox_check_nonce" value="'. wp_create_nonce(basename(__FILE__)). '" />';
    $html .= '<label><input type="checkbox" name="my_custom field" id="my_custom field"' . ( $meta ? ' checked="checked"' : '' ) . ' /> '.$label.'</label>';

    return $html;
}

Then, you need the Save post actions for the custom field, but that is another topic.

  • This works perfectly and should be the accepted answer. The second option, filtering the callback is much better of course since it doesn't replace the author metabox completely. – Matt Cromwell Aug 1 '16 at 3:39
0

I don't believe this is possible without editing WordPress Core. Your best option would be to deregister the metabox, then re-register your own that mimics the original functionality with your changes.

Stephen Harris outlined how to do this here: Add custom option to Standard Page Attributes Meta Box

0

Have you look for a plugin called ACF (Advanced Custom Fields) the plugin can be found here:

https://wordpress.org/plugins/advanced-custom-fields/

with this plugin you can add as many custom fields as you want and also there is the logic functionality in the plugin you can make dependencies. for example you want to create some additional custom field for the author and you called authors extra meta and you can make a rule that this custom filed, only to apply to authors and no one else.

have a good look for more info here:

http://www.advancedcustomfields.com/resources

The best thing is that you can use these custom field in your WP_Query directly.

Cheers

-1

After some more digging, I came across a solution that works.

I simply remove Wordpress's author meta box using remove_meta_box() and then re-add it with a custom function, adding the fields I want within it.

  • If this was your solution, please mark it as such so that the question is not left unanswered. – mor7ifer Feb 11 '15 at 17:52
  • 1
    Please post the code you used to answer the question. – Brad Dalton Jul 30 '15 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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