Sync wp post title with custom field
Closed this issue · 8 comments
I'm digging into your (very very enjoyalbe) plugin, but have a question/concern UX wise :)
once set up, my wp custom post type wp_contact
will be synced with Civi indivudual contact type 💯
as I understood, wp post title will be synced with civicrm display name, then I can map whatever I want.
Obviously I would like Civicrm contact name (first and last) to be filled, on top of "display name" (well it seems a good practice)
If I create a contact in CiviCrm, then it's easy, i set the first and lastname ; but If I create it in wordpress, I'll only define the display name, unless I creat two custom filed that I sync. Ok why not, but the user will have to type the name of the contact in the title of the post, then type the first name in a custom field and the lastname in another.
This do not seems very userfriendly ?!
I guess I coudl easylly code some javascript to auto-populate some field based on the other, but It might not be the best way to go ?
Do you have any thought on that ?
Thank's
Are you asking if there's a way to use contact fields for the UI? I think so. If so, I've added an ACF field group with general contact fields to multiple CPTs (if needed) and then a seperate field group for the fields that are for that CPT/Contact Type specifically. Something like this:
Is that what you mean?
@jbonlinea I agree that the WordPress Post Edit screen isn't great UX for this purpose, but it is what it is. I would recommend doing what @danaskallman showed you - i.e. create an ACF Field Group that has all the details of the Contact Name in it. Here's mine:
I have linked each ACF Field to the corresponding Contact Field. The WordPress Post Title (and Contact Display Name) will be built from the content of these ACF Fields when the Post is saved. If you wanted to, you could include some CSS on the Edit Post screen which hides the Post Title field, but publicly visible Posts do seem to require a Title, so it can't be removed completely.
Obviously I would like CiviCRM contact name (first and last) to be filled, on top of "display name" (well it seems a good practice)
There is a setting in CiviCRM which tells it how to build the Display Name for Contacts. When the post is created or edited, it is this which is synced back to the WordPress Post's Title. If you want to allow people to fill out a custom Display Name, then this "reverse sync" logic needs to be disabled and you need to allow people to edit the Post Title.
To disable reverse sync, you can do something like the following:
$cai = civicrm_acf_integration();
remove_action( 'civicrm_acf_integration_contact_acf_fields_saved', [ $cai->post, 'maybe_sync_title' ], 10 );
You can then build your sync between an ACF Field and the Post Title or allow the Post Title to be edited directly.
Are you asking if there's a way to use contact fields for the UI? I think so. If so, I've added an ACF field group with general contact fields to multiple CPTs (if needed) and then a seperate field group for the fields that are for that CPT/Contact Type specifically. Something like this:
Is that what you mean?
Hi !
no but that's an idea I had as well :)
Hey !
Thank's for your replies !
oh ! Imay have not expressed myself very well ! I don't mind wp editor ui, most will be done thank's to a front end form :)
I have linked each ACF Field to the corresponding Contact Field. The WordPress Post Title (and Contact Display Name) will be built from the content of these ACF Fields when the Post is saved. If you wanted to, you could include some CSS on the Edit Post screen which hides the Post Title field, but publicly visible Posts do seem to require a Title, so it can't be removed completely.
Well, maybe it's a bug, maybe I missed something, but if I create a contact (CPT) in wp editor fill the custom field for firstname lastname, but not the title, the post is created in wp but no contact are created in Civi.
If I add a tile to the contact it is created in Civi.
Otherwise I would just don't mind it
Obviously I would like CiviCRM contact name (first and last) to be filled, on top of "display name" (well it seems a good prIactice)
I guess we could use two ACF group, one that is displayed above the wp title in the editor, and one other below (heavy machinery)
To disable reverse sync, you can do something like the following:
Ok, good to know event if it's not my need
You can then build your sync between an ACF Field and the Post Title or allow the Post Title to be edited directly.
Yes I guess I could technically use something like this, but I don't knwo how would it behave. I mean timing is a question, when do your module porceess the sync, compare to this, who's first ? if yours then it won't work, at the same time ? then it might fail... ?
Well, maybe it's a bug, maybe I missed something, but if I create a contact (CPT) in wp editor fill the custom field for firstname lastname, but not the title, the post is created in wp but no contact are created in Civi.
If I add a tile to the contact it is created in Civi.
@jbonlinea Ah, nice catch. The reason can be found in this section of code where posts considered "empty" are not processed. You'll need to override this by using the filter.
@jbonlinea I'm going to close this issue since this plugin isn't really responsible for how you set up your WordPress Post Types. FYI, here is the code you need in order to allow "empty" posts and their ACF fields to be saved in the usual way. Edit YOUR_POST_TYPE
with whatever your Post Type's name (not label) is.
function my_prefix_allow_empty_content( $maybe_empty, $postarr ) {
// Grab post type from post array.
$post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
// Allow insert if post type is ours.
if( $maybe_empty AND in_array( $post_type, [ 'POST_TYPE_ONE', 'POST_TYPE_TWO' ] ) ) {
return false;
}
// Otherwise pass through.
return $maybe_empty;
}
add_filter( 'wp_insert_post_empty_content', 'my_prefix_allow_empty_content', 10, 2 );
Hope that helps.
Hi @christian
thank you for that !
I've only truly implemented it now, (but had tried it back then), works like a charm.
For anyone planing to do something like this, be awere that if you change Civi display name order (for instance swap first name and last name), the change won't be reported on your post if you do sync contact to post in civi-acf admin, you'll have to re-save each post individually (bulk change won't work either).
Cheers
if you change Civi display name order (for instance swap first name and last name), the change won't be reported on your post if you do sync contact to post in civi-acf admin, you'll have to re-save each post individually (bulk change won't work either).
Ah, thanks for the heads-up on this @jbonlinea - I should probably code something to handle this when it happens.