Skip to content

Admin box display

clariner edited this page Sep 10, 2014 · 1 revision

When registering a connection type, you can control if and where the connections metabox shows up in the admin:

<?php
p2p_register_connection_type( array(
  'name' => 'my_connection_type',
  'from' => 'page',
  'to' => 'user',
  'admin_box' => array(
    'show' => 'any',
    'context' => 'side'
  )
) );

Possible values for the 'show' key:

  • 'any' - show admin box everywhere (default)
  • 'from' - show the admin column only on the 'from' end
  • 'to' - show the admin column only on the 'to' end
  • false - don't show admin box at all

Possible values for the 'context' key:

  • 'side' - show admin box in the right column
  • 'advanced' - show the admin box under the main content editor

Customising the labels

By default, P2P will use the labels that the custom post type has. If you want to overwrite them, you can use the 'from_labels' and 'to_labels' parameters, like so:

<?php
p2p_register_connection_type( array(
  'name' => 'users_to_stuff',
  'from' => 'user',
  'to' => array( 'car', 'house' ),
  'title' => array(
    'from' => __( 'People', 'my-textdomain' ),
    'to' => __( 'Items', 'my-textdomain' )
  ),
  'from_labels' => array(
      'singular_name' => __( 'Person', 'my-textdomain' ),
      'search_items' => __( 'Search people', 'my-textdomain' ),
      'not_found' => __( 'No people found.', 'my-textdomain' ),
      'create' => __( 'Create Connections', 'my-textdomain' ),
  ),
  'to_labels' => array(
      'singular_name' => __( 'Item', 'my-textdomain' ),
      'search_items' => __( 'Search items', 'my-textdomain' ),
      'not_found' => __( 'No items found.', 'my-textdomain' ),
      'create' => __( 'Create Connections', 'my-textdomain' ),
  ),
) );

Limiting the number of connections that can be made

Its possible to limit the number of connected items a post can on a per connection basis by using javascript. You will need to hook into admin_enqueue_scripts so the javascript file is included in the page.

add_action('admin_enqueue_scripts', array(&$this, 'limit_p2p_connections'));

function limit_p2p_connections($hook) {
        global $post_type;
        if ($post_type == '[POST_TYPE]' && 'edit.php' != $hook) {
            wp_enqueue_script('limit_p2p_connections', [PATH_TO_JS_FILE] . '/p2plimit.js');
        }
    }

The example limits the maximum connected items to 1, increase the value to allow more connections.

jQuery(function($){
	
	$('div[data-p2p_type="[CONNECTION_NAME]"]').bind("DOMSubtreeModified", function() {		
		if($('div[data-p2p_type="[CONNECTION_NAME]"] > table.p2p-connections >tbody >tr').length >= 1)
		{
    		$('div[data-p2p_type="[CONNECTION_NAME]"] > div.p2p-create-connections').hide();
		}
		else 
		{
    		$('div[data-p2p_type="[CONNECTION_NAME]"] > div.p2p-create-connections').show();
		}
});
	
	$('div[data-p2p_type="[CONNECTION_NAME]"] > table.p2p-connections >tbody >tr').ready(function() {
		if($('div[data-p2p_type="[CONNECTION_NAME]"] > table.p2p-connections >tbody >tr').length >= 1)
		{
    		$('div[data-p2p_type="[CONNECTION_NAME]"] > div.p2p-create-connections').hide();
		}
		else 
		{
    		$('div[data-p2p_type="[CONNECTION_NAME]"] > div.p2p-create-connections').show();
		}
	});

});