最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to position a custom field before the editor

programmeradmin0浏览0评论

I have installed Advanced Custom Fields 4.0.1 and created a new Field group containing a single field called preamble. I would like to position this new field before the editor in the posts edit screen. It seems like all custom fields always are added after a posts ordinary fields.

A solution wouldn't be to drop the custom fields created with ACF and use ordinary custom fields.

I have installed Advanced Custom Fields 4.0.1 and created a new Field group containing a single field called preamble. I would like to position this new field before the editor in the posts edit screen. It seems like all custom fields always are added after a posts ordinary fields.

A solution wouldn't be to drop the custom fields created with ACF and use ordinary custom fields.

Share Improve this question edited Oct 31, 2013 at 7:56 Grant Palin 1,0492 gold badges13 silver badges35 bronze badges asked Apr 4, 2013 at 8:48 CyclonecodeCyclonecode 1,1841 gold badge9 silver badges32 bronze badges 2
  • This will only be a duplicate if the OP is willing drop ACF for a regular post meta, otherwise the solution here is jQuery. – brasofilo Commented Apr 4, 2013 at 10:58
  • No, I'm not able to replace the ACF with regular post meta. Yes, I have figured out how to achive this by using jQuery, but I was wondering if there was a way to do it using only php and built in wordpress functions? – Cyclonecode Commented Apr 4, 2013 at 13:34
Add a comment  | 

4 Answers 4

Reset to default 6

This can be solved using this nice snippet and edit_form_after_title hook. But I haven't tested what happens when more than one meta box exists. With a single ACF field (position:normal, style:no-metabox) it works:

add_action( 'edit_form_after_title', 'pre_title_metabox_wpse_94530' );

function pre_title_metabox_wpse_94530() 
{
    global $post, $wp_meta_boxes;

    do_meta_boxes( get_current_screen(), 'normal', $post );

    unset( $wp_meta_boxes['post']['normal'] );
}

And if it has to be solved with jQuery, adjust subtitle_text to your Field Name:

// Add hook to Edit and New post
foreach( array( 'post.php', 'post-new.php' ) as $hook )
    add_action( "admin_footer-$hook", 'move_acf_to_title_wpse_94530' );

function move_acf_to_title_wpse_94530()
{
    // Check post type
    if( 'post' != get_post_type() )
        return;

    ?>
    <script type="text/javascript">
        jQuery(document).ready( function($) 
        {
            $( '#acf-field-subtitle_text' ).css( 'width', '100%' );
            $( '#acf-subtitle_text' ). insertAfter( '#titlewrap' );
        });
    </script>
    <?php
}

I've checked into this a bit. I looked at the ACF source. The plain fields that ACF uses are still actually meta boxes. ACF is just using CSS to make them look like more generic fields. They are still being handled as meta boxes by WP (do_metaboxes action).

To add plain fields to other parts of the edit form, you'll need to use the appropriate hooks. More hooks on the edit screen:

add_action( 'edit_form_after_title', 'myprefix_edit_form_after_title' );
function myprefix_edit_form_after_title() {
    echo '<h2>This is edit_form_after_title!</h2>';
}

add_action( 'edit_form_after_editor', 'myprefix_edit_form_after_editor' );
function myprefix_edit_form_after_editor() {
    echo '<h2>This is edit_form_after_editor!</h2>';
}

add_action( 'edit_form_advanced', 'myprefix_edit_form_advanced' );
function myprefix_edit_form_advanced() {
    echo '<h2>This is ye olde edit_form_advanced!</h2>';
}

There is a native solution provided by ACF nowadays. You can set the position of your group of fields. By default it's set to "Position : High (after title)" but you can change it when you edit your group of fields.

With thanks to @brasofilo for the initial answer.

I only needed to move one fields before the editor field. Instead of all additional the fields.

This is how i fixed it:

add_action( 'edit_form_after_title', 'pre_title_metabox_wpse_94530' );

function pre_title_metabox_wpse_94530()
{
    global $post, $wp_meta_boxes;

    if ($post->post_type == "event"){

        // store current fields
        $tmp_wp_meta_boxes = $wp_meta_boxes;

        // isolate one field
        $excerpt = $tmp_wp_meta_boxes['event']['normal']['core']['postexcerpt'];
        $wp_meta_boxes = ['event'=>['normal'=>['core'=>['postexcerpt'=>$excerpt]]]];

        // render isolated field
        do_meta_boxes( get_current_screen(), 'normal', $post );

        // restore field without
        $wp_meta_boxes = $tmp_wp_meta_boxes;
        unset( $wp_meta_boxes['event']['normal']['core']['postexcerpt'] );
    }
}
发布评论

评论列表(0)

  1. 暂无评论