Created
November 16, 2015 16:18
-
-
Save tyxla/663f68baa7b22fd78084 to your computer and use it in GitHub Desktop.
Association field with custom values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| # Our available options | |
| function crb_test_options() { | |
| return array( | |
| 123 => array( | |
| 'id' => 123, // ID of the entry, value that is saved in the DB, should be the same as the array key. | |
| 'title' => 'Test Title', // title of the item, used for displaying the item in the admin | |
| 'type' => 'test', // type of the item, used for identifying this type of item | |
| 'subtype' => 'test item', // subtype of the item, in this solution it will be used only for presentation purposes | |
| 'label' => 'test item', // label of the item, used for displaying the item in the admin | |
| 'is_trashed' => false, // trashed status | |
| ), | |
| 456 => array( | |
| 'id' => 456, | |
| 'title' => 'Test Title', | |
| 'type' => 'test', | |
| 'subtype' => 'test item', | |
| 'label' => 'test item', | |
| 'is_trashed' => false, | |
| ), | |
| ); | |
| } | |
| # Add the custom options to that custom field | |
| add_filter( 'carbon_relationship_options', 'crb_carbon_relationship_options', 10, 2 ); | |
| function crb_carbon_relationship_options( $options, $field_name ) { | |
| # Targeting only the necessary association field | |
| if ( $field_name === '_crb_assoc_test' ) { | |
| $options = crb_test_options(); | |
| } | |
| return $options; | |
| } | |
| # Make sure that the association field knows how to retrieve the item title | |
| add_filter( 'carbon_relationship_title', 'crb_carbon_relationship_title', 10, 5); | |
| function crb_carbon_relationship_title( $title, $field_name, $id, $type, $subtype ) { | |
| # Targeting only the necessary association field | |
| if ( $field_name !== '_crb_assoc_test' ) { | |
| return $title; | |
| } | |
| $options = crb_test_options(); | |
| $title = $options[ $id ][ 'title' ]; | |
| return $title; | |
| } | |
| # Make sure that the association field knows how to retrieve the item label | |
| add_filter( 'carbon_relationship_label', 'crb_carbon_relationship_label', 10, 5); | |
| function crb_carbon_relationship_label( $label, $field_name, $id, $type, $subtype ) { | |
| # Targeting only the necessary association field | |
| if ( $field_name !== '_crb_assoc_test' ) { | |
| return $label; | |
| } | |
| return 'Test Item'; | |
| } | |
| # Define the carbon container and the association field | |
| Carbon_Container::factory( 'custom_fields', __( 'Custom Data', 'crb' ) ) | |
| ->show_on_post_type( 'page' ) | |
| ->add_fields(array( | |
| Carbon_Field::factory( 'association', 'crb_assoc_test' ) | |
| ->set_types( array( | |
| ) ), | |
| )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment