Support Forum
Support › MB Relationships › auto complete relationship connector fieldsResolved
I am using Metabox to create various CPTs. I am using various post types that are relations and I use the relationship module for that.
I have a CPT called Participant.
I have a CPT called entries.
The participant is enrolled in the Entries for a talent competition.
These entries are made by the parents who register as users on the system and then add the participants (their children)Once they register the children as participants they will then create an entry into the competition with the items( also a separate cpt) the participant.
we have created formed forms with piotnet forms for the parents to capture the entries as we do not want to give them access to the back end to enter the entries directly into the CPTs. In the Piotnet fields, we have created select fields with code in their option fields to list the entries of the external cpt (participant) in the select field. This is all working 100% at this stage.
However, the relationship connector fields are not populated when this happens and as it is relational fields we do need them to complete but it seems it can only be done in the backend cpt fields on the dashboard. We need this information for reporting to the admins of the competition but because of the volume of entries, it is not viable to do it manually.
My question is how can the relationship fields be auto-completed on grounds of the values selected in the piotnet forms? In other words, if a parent entries a child named john in the competition for the event "poetry" we need to create a relationship with the "participant" and "entry" and the "item" and "entry" automatically when data is captured
Please advise how we can achieve this. I can supply you with an admin login if it will help
Hello Pat,
I see you've created a similar question in another ticket https://support.metabox.io/topic/auto-create-the-values-for-relationships/
If you want to create a relationship between two objects (posts, terms, users) by coding, please follow this documentation https://docs.metabox.io/extensions/mb-relationships/#managing-connections-programmatically
If you cannot accomplish this, I recommend hiring an expert developer to help you with this case.
Good morning ,
I have followed your suggestion and contacted a developer to assist with the code as you suggested however we are still running into issues with the code to achieve the above. Here is a full breakdown by the developer of the challenges he encountered in getting this to work.
Please advise how we can overcome these challenges
We did the code as set out in the documentation you sent me. We did use JS to call the Function and it worked to auto complete the relationship metaboxes. However, this had the effect of disabling the metabox custom field group option if you need to edit the group fields. Once deactivated the edit function would work again.
Here are the details of the bug I ran into.
Summary, when you add JavaScript in either the functions.php file and or the advanced script plugin located under Dashboard > tools > Advanced Scripts
The error is caused when you add JavaScript eventListeners, querySelectors, getElementById and getElementByClassName in between the script tags on the functions.php file or the JS file provided by Advanced Scripts plugin.
It stops you from running any other JS on the site making it impossible to save code in the functions.php file or do searches for plugins or saving any other changes or saves that require JS (including AJAX).
Steps to recreate: (functions.php)
1. Login to the https://registrations.dellarte.co.za admin dashboard.
2. Go to Appearance > Theme File Editor.
3. Open the functions.php file on the right hand side.
4. Close the php tag with "?>" at the end.
5. Create the opening and closing tags of the HTML <script> tag: <script></script>
6. Enter console.log("Hello I am working") between the <script> tags.
7. Go to the site (https://registrations.dellarte.co.za/) and open the developer console and you will see it will log out "Hello I am working".
8. Go back to the functions.php file in location of point 2.
9. Replace the console.log("Hello I am working") with "window.addEventListener('load', function() { console.log("Still working") })" and save and go back to site and you will see the it logs out "Still working"
10. Replace the text with "Not working" in the console.log and save, you will get the error prompt "Something went wrong. Your change may not have been saved. Please try again. There is also a chance that you may need to manually fix and upload the file over FTP." and go back to site and you will see the log is still "Still working".
There is the recreation of the bug in the functions.php file, the error seems to be the same in the Advanced Scripts plugin, if you copy and place the code in the Advanced Scripts plugin by creating a JS file naming it and pasting the code in there and click "Save and Activate", remove the code saved in the functions.php file and save you will still get the same error.
For further causes of this bug, you can go to plugins > Add New Plugin and try to search you will see that it does not return any results.
Hope this helps, if there are any questions please let me know if I need to clarify anything or expand the steps to recreate with the Advanced Scripts plugin.
I am prepared to supply you with credentials to the site on a more private platform if you wish to assess it directly.
the code for the function is as follow
<?php
/**
* Register/enqueue custom scripts and styles
*/
add_action( 'wp_enqueue_scripts', function() {
// Enqueue your files on the canvas & frontend, not the builder panel. Otherwise custom CSS might affect builder)
if ( ! bricks_is_builder_main() ) {
wp_enqueue_style( 'bricks-child', get_stylesheet_uri(), ['bricks-frontend'], filemtime( get_stylesheet_directory() . '/style.css' ) );
}
} );
/**
* Register custom elements
*/
add_action( 'init', function() {
$element_files = [
__DIR__ . '/elements/title.php',
];
foreach ( $element_files as $file ) {
\Bricks\Elements::register_element( $file );
}
}, 11 );
/**
* Add text strings to builder
*/
add_filter( 'bricks/builder/i18n', function( $i18n ) {
// For element category 'custom'
$i18n['custom'] = esc_html__( 'Custom', 'bricks' );
return $i18n;
} );
add_action('init', function(){
if(isset($_GET['test'])){
}
});
?>
<script>
window.addEventListener('load', function() { console.log('Still workling') })
</script>
And the js
window.addEventListener('load', () => {
jQuery(function ($) {
$('.piotnetforms-button').on('click', function () {
jQuery(document).ready(function($) {
var form = $('#3050').serializeArray()
var data = {
'action': 'my_action',
'formData': form
};
console.log(form)
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
// jQuery.post(ajaxurl, data, function(response) {
// alert('Got this from the server: ' + response);
// });
});
})
})
});
Hello,
Support a customization code is beyond our scope of support, please read more here https://support.metabox.io/topic/support-policy/?swcfpc=1
You can request a customization service by submitting a ticket here https://metabox.io/contact/
I will forward it to our development team to take a look and give you a quote.