Support

Account

Home Forums Backend Issues (wp-admin) Move p2p admin box into an ACF field

Solved

Move p2p admin box into an ACF field

  • Hi there,

    I’m using the ACF Pro plugin heavily on one of my projects and I really need some connection metadata that ACF Pro doesn’t provide with it’s relationship field unfortunately.

    So I setup a Posts 2 Posts connection with a p2p_admin_box_show filter so the box only shows up if a certain flexible content field is added on my custom post type page like this:

    // Display p2p admin box only if the flexible content field 'team block' was added
    function restrict_p2p_box_display( $show, $ctype, $post ) {
      if ( 'project_to_team' == $ctype->name && 'from' == $ctype->get_direction() ) {
        if(have_rows('page_content'), $post->ID):
          while(have_rows('page_content'), $post->ID) : the_row();
            if(get_row_layout() == 'team_block'):
              move_p2p_admin_box(); // not working
              return $show;
            endif;
          endwhile;
        endif;  
      }
    }
    add_filter( 'p2p_admin_box_show', 'restrict_p2p_box_display', 10, 3 );

    Which works great!
    I would really like to take it one step further without creating the p2p admin box myself from scratch, by moving the p2p meta box into an existing ACF metabox field. So I added this function:

    // Move p2p meta box into acf meta box
    function move_p2p_admin_box() { ?>
      <script text="text/javascript">
        (function($) {
          var flexBlockName = $('.acf-flexible-content').find('[data-layout="team_block"] .row-layout [data-name="block_background"]');
          $('#p2p-from-project_to_team').appendTo(flexBlockName);
            })(jQuery);
      </script>
    <?php }

    But that’s not working yet (getting jQuery is not defined errors also). Is there a better way to do this?

    I read the WordPress add_meta_box codex: http://codex.wordpress.org/Function_Reference/add_meta_box
    but that doesn’t have the option to move a box to a specific location in the DOM.

    I also read the Creating a new field type and acf/render_field documentation, but I don’t really want to create an own custom field type. I want to visually use the p2p plugin inside the ACF plugin if possible.

    Any tips are welcome, thanks!

  • I got this working by enqueuing some jQuery in my functions.php:
    https://github.com/scribu/wp-posts-to-posts/issues/469

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Move p2p admin box into an ACF field’ is closed to new replies.