Home › Forums › Backend Issues (wp-admin) › Call A Field Programatically In Admin Area
I have a series of child Pages, all of which can belong to one of three different Parent Pages. Each of these Child Pages calls in a particular field, depending on which Parent is selected.
When I do this using the field rules, it works perfectly. E.g. a User creates a new Page then selects a Parent for the Page and then the correct custom field for that Parent type appears.
However, some of my Authors are very, very inexperienced and often forget to assign a Parent to a new Page. I have tried assigning a jQuery confirm alert on Page Publish to remind Authors to assign a Page to a Parent but the more experienced Users find this annoying.
I have got round this by creating an Admin Dashboard widget that contains links to create a new Page with the Parent pre-selected. I’ve done this by adding the Parent ID to the URL of the new post:
https://www.example.com/wp-admin/post-new.php?post_type=page&parentid=1
Then I have some code in functions.php to test for this:
function my_preselect_parent() {
if(!isset($_GET['parentid']) || empty($_GET['parentid'])) {
return;
}else{
$ppid = $_GET['parentid'];
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#parent_id').val(<?php echo $ppid; ?>);
});
</script>
<?php
}
add_action('admin_head-post-new.php', 'my_preselect_parent');
This works perfectly.
However, my issue is – how do I now call the right custom field in? Is there a way to programmatically call in a custom field to the new post screen?
Trigger the change event on the field after setting the value
$('#parent_id').trigger('change');
A facepalm moment. Of course.
Thank you John, worked perfectly.
Out of curiosity, is there a way to call in a field server-side to an Admin screen?
Not always, it is counterintuitive to many that altering the value of a field in JS does not automatically trigger the change event.
Initially, field groups are loaded based to the location rules of the page when it is loaded. Field groups are changed with an AJAX call. In the case of a new post you would have to set the parent page before the admin page is generated.
See this stack exchanged topic https://wordpress.stackexchange.com/questions/59007/setting-a-default-parent-page
You could use this to set the parent page based on your $_GET['parentid']
value and this should cause the parent ID set and the correct field groups to be loaded initially rather than altering the field and triggering the fields to be reloaded.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.