Last active
September 12, 2024 14:27
-
-
Save MjHead/f215cfbf4e5b1d003d94de63e353d18f to your computer and use it in GitHub Desktop.
JetEngine Relations. Add custom fields into create new item popup. Add custom columns into related item table
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 | |
/** | |
* Adds custom fields into the insert new related item popup. | |
* Hook name - jet-engine/relations/types/posts/create-fields - depends on type of created item, | |
* for CPT it will be - jet-engine/relations/types/posts/create-fields | |
* for terms - jet-engine/relations/types/terms/create-fields | |
* for users - jet-engine/relations/types/mix/create-fields/users | |
* | |
* just replace 'jobs' in the example with your actual post type/taxonomy slug | |
* for users you can completely remove thic condition and just set up the fields | |
*/ | |
add_filter( 'jet-engine/relations/types/posts/create-fields', function( $fields, $post_type ) { | |
if ( 'jobs' === $post_type ) { | |
$fields[] = array( | |
'name' => 'salary', | |
'title' => __( 'Salary', 'jet-engine' ), | |
'type' => 'text', | |
); | |
$fields[] = array( | |
'name' => 'job_type', | |
'title' => __( 'Type', 'jet-engine' ), | |
'type' => 'select', | |
'options' => array( | |
array( | |
'value' => '', | |
'label' => 'Select...', | |
), | |
array( | |
'value' => 'Remote', | |
'label' => 'Remote', | |
), | |
array( | |
'value' => 'In Office', | |
'label' => 'In Office', | |
), | |
), | |
); | |
} | |
return $fields; | |
}, 10, 2 ); | |
/** | |
* Allow to store previously added custom fields in the any way you want. | |
* | |
* Hook name depends on the processed items type | |
* jet-engine/relations/types/posts/on-create - for CPT | |
* jet-engine/relations/types/terms/on-create - for terms | |
* jet-engine/relations/types/mix/on-create/users -for users | |
* | |
* Just replace jobs with reauired CPT/taxonomy slug, or completely remove this condition for the users. | |
* Than you can update required field by input, stored in the $data variable | |
* | |
* | |
*/ | |
add_action( 'jet-engine/relations/types/posts/on-create', function( $post_id, $data, $post_type ) { | |
if ( 'jobs' !== $post_type ) { | |
return; | |
} | |
if ( $post_id && ! empty( $data['salary'] ) ) { | |
update_post_meta( $post_id, 'salary', absint( $data['salary'] ) ); | |
} | |
if ( $post_id && ! empty( $data['job_type'] ) ) { | |
update_post_meta( $post_id, 'job_type', $data['job_type'] ); | |
} | |
}, 10, 3 ); | |
/** | |
* Here you can add custom columns to the related items table. | |
* First of all you need to find your relation ID and use it as part of hook name. | |
* ID you can find in the address bar of the edit realtion page - https://prnt.sc/24tk842 | |
* | |
* Than add this ID into the end of hook name - jet-engine/relations/init/22, where 22 - is relation ID | |
* | |
* Than you can use $relation->add_table_column() method inside table hook callback to add new columns. | |
* Example: | |
* $relation->add_table_column( | |
* 'child_object', - 'child_object' - is columns for the children items table, 'parent_object' - for the parent related items table. | |
* 'salary', - key of the column | |
* 'Salary', - title of the column | |
* function( $post_id ) { | |
* $price = get_post_meta( $post_id, 'salary', true ); | |
* if ( $price ) { | |
* return '$' . $price; | |
* } else { | |
* return 'Not set'; | |
* } | |
* } ); | |
* | |
* Last parameter is a callback to show column content. It accepts item ID as an argument. By this item ID you can get any data about this item | |
*/ | |
add_action( 'jet-engine/relations/init/22', function( $relation ) { | |
$relation->add_table_column( 'child_object', 'salary', 'Salary', function( $post_id ) { | |
$price = get_post_meta( $post_id, 'salary', true ); | |
if ( $price ) { | |
return '$' . $price; | |
} else { | |
return 'Not set'; | |
} | |
} ); | |
$relation->add_table_column( 'child_object', 'job_type', 'Type', function( $post_id ) { | |
$job_type = get_post_meta( $post_id, 'job_type', true ); | |
if ( $job_type ) { | |
return $job_type; | |
} else { | |
return '--'; | |
} | |
} ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment