Skip to content

Conversation

faisal-alvi
Copy link
Member

@faisal-alvi faisal-alvi commented Aug 8, 2025

Description of the Change

This PR implements custom field targeting for the ClassifAI excerpt generation feature, allowing users to save generated excerpts to custom fields beyond the default WordPress excerpt field. This enhancement supports editorial workflows that use structured content blocks, custom meta fields, or ACF fields for storing excerpts, etc.

  • Added support for targeting custom meta fields (e.g., editorial_subtitle, custom_excerpt, dek)
  • Integrated ACF field support for seamless ACF integration
  • Maintained default excerpt field as primary option
  • Added fallback mechanisms for field detection and value retrieval
  • New "Target field" setting in excerpt generation configuration
  • Dropdown selection for field type (Default excerpt, Custom meta, ACF field)
  • Dynamic field selection based on chosen field type
  • ACF field integration with automatic field detection
  • Classic Editor: Full custom field support with real-time updates and field detection
  • Block Editor: Integrated custom field display and synchronization
  • Context-aware messaging for different field types
  • Multiple detection methods for ACF fields (data attributes, field keys, DOM structure)
  • Flexible meta field detection and updating

New Setting Field:

image

Block Editor (Custom Field):

image

Block Editor (ACF):

image

Classic Editor (ACF):

image

Closes #921

How to test the Change

  1. Custom Meta Field Testing:

    • Create custom meta fields (e.g., editorial_subtitle, custom_excerpt)
    • Configure excerpt generation to target custom meta fields
    • Generate excerpts and verify they save to the correct fields
  2. ACF Field Testing:

    • Install/activate ACF plugin
    • Create ACF fields (text/textarea)
    • Configure excerpt generation to target ACF fields
    • Test excerpt generation and field updates
  3. Editor Testing:

    • Test in both classic and block editors
    • Verify real-time field updates
    • Confirm proper field detection and synchronization
  4. Settings Testing:

    • Navigate to ClassifAI settings > Language Processing > Excerpt Generation
    • Verify "Target field" dropdown with all options
    • Test field type switching and configuration saving

Credits

Props @jeffpaul @faisal-alvi @iamdharmesh

Changelog Entry

Added - Add custom field targeting for Excerpt Generation

@faisal-alvi faisal-alvi self-assigned this Aug 8, 2025
@github-actions github-actions bot added this to the 3.6.0 milestone Aug 8, 2025
 ------ ---------------------------------------------------------------------
  Line   includes/Classifai/Admin/Settings.php
 ------ ---------------------------------------------------------------------
  246    Function acf_get_fields not found.
         🪪  function.notFound
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
 ------ ---------------------------------------------------------------------

 ------ ---------------------------------------------------------------------
  Line   includes/Classifai/Features/ExcerptGeneration.php
 ------ ---------------------------------------------------------------------
  521    Function acf_get_fields not found.
         🪪  function.notFound
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
  774    Expected 4 @param tags, found 3.
         🪪  paramTag.count
  834    Function acf_get_fields not found.
Elapsed time: 17 seconds
         🪪  function.notFound
         💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
 ------ ---------------------------------------------------------------------
@faisal-alvi faisal-alvi marked this pull request as ready for review August 8, 2025 15:22
@faisal-alvi faisal-alvi requested review from jeffpaul, dkotter and a team as code owners August 8, 2025 15:22
@github-actions github-actions bot added the needs:feedback This requires reporter feedback to better understand the request. label Aug 8, 2025
Copy link

github-actions bot commented Aug 8, 2025

@faisal-alvi thanks for the PR! Could you please fill out the PR template with description, changelog, and credits information so that we can properly review and merge this?

@faisal-alvi faisal-alvi requested review from a team and iamdharmesh and removed request for jeffpaul, a team and dkotter August 8, 2025 15:22
@github-actions github-actions bot added needs:code-review This requires code review. and removed needs:feedback This requires reporter feedback to better understand the request. labels Aug 8, 2025
Copy link
Member

@iamdharmesh iamdharmesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the working on this and raising the PR @faisal-alvi. Overall it looks good. However, I have added few comments.

Additionally, I noticed that we are marking the default excerpt field as read-only/disabled when a custom meta field or ACF field is selected as the target field. I think we should keep the excerpt field as it is. We might instead show a notice near the excerpt generation button, indicating that the generated content will be saved into the selected custom field/ACF field. Otherwise, I believe we should leave the excerpt field unchanged, as there is no direct connection between them. I just wanted to know if there is a specific reasoning behind this. (cc: @dkotter any thoughts on this?)

Image

Thanks!

'Change the target field in',
'classifai'
) +
'<a href="' +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'<a href="' +
' <a href="' +

Add space before settings link.

Image

Comment on lines +455 to +467
add_settings_field(
'target_field',
esc_html__( 'Target field', 'classifai' ),
[ $this, 'render_target_field_settings' ],
$this->get_option_name(),
$this->get_option_name() . '_section',
[
'label_for' => 'target_field',
'default_value' => $settings['target_field_type'] ?? 'post_excerpt',
'description' => __( 'Choose where to save the generated excerpt. You can target the default excerpt field, custom meta fields, or ACF fields.', 'classifai' ),
]
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not supporting legacy settings panel now. So, this is no longer needed now.

Comment on lines +577 to +578
'target_custom_field' => '',
'target_acf_field' => '',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: As we are already storing target_field_type, can we merge this two into single as a target_field?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for keeping these separate is to ensure that if the admin switches back to the previous type, they will still get the same field they were originally using.

*
* @return array Array of ACF fields.
*/
public function get_available_acf_fields() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed similar get_acf_fields() function in Admin/Settings.php. Maybe better to move this to helper functions to prevent code duplication here?

@dkotter dkotter modified the milestones: 3.6.0, 3.7.0 Aug 19, 2025
@dkotter
Copy link
Collaborator

dkotter commented Aug 20, 2025

might instead show a notice near the excerpt generation button, indicating that the generated content will be saved into the selected custom field/ACF field. Otherwise, I believe we should leave the excerpt field unchanged, as there is no direct connection between them

Yeah, I agree feels a little weird to have that field disabled.

I also think we may need further discussion on how exactly this should work. For instance, feels like we may want to allow multiple selections here, so someone can store this in the excerpt field and a custom field (similar to our Descriptive Text Generator where you can choose one or multiple options for storage).

In addition, if I choose ACF or custom meta, I don't love the idea of having the Generate Excerpt button show in the Excerpt panel, as you're not storing the excerpt there anymore. There's probably a better location for that button in those scenarios, though that would require some more discussion.

And finally, if this Feature is on, we remove the core WordPress excerpt panel and replace it with our own, which allows us to add that generate button. Since that was introduced, WordPress has changed how the excerpt panel works and it's been on my list to bring those changes over to ClassifAI. Doesn't have to be done in this PR but might be a good opportunity to align our approach with core (or even investigate if there's a better option now to add that button).

@faisal-alvi
Copy link
Member Author

faisal-alvi commented Aug 29, 2025

@iamdharmesh Thanks for the feedback!

I noticed that we are marking the default excerpt field as read-only/disabled when a custom meta field or ACF field is selected as the target field.

  1. Just to clarify, we’re not disabling the core excerpt field; we’re hiding it.
    Instead, we show a read-only field that reflects the targeted Custom/ACF field value, along with a note for users explaining what to do if they want to edit it.

Reference:

// If custom field is enabled, hide the default excerpt field and show custom field info.
if (
targetFieldSettings &&
targetFieldSettings.field_type !== 'post_excerpt'
) {
hideDefaultExcerptField();
showCustomFieldInfo();
}

image

@dkotter Thanks for adding details. A few follow-ups from my side:

  1. Multiple Selections

feels like we may want to allow multiple selections here, so someone can store this in the excerpt field and a custom field

My assumption is that the AI-generated excerpt would be inserted into both the custom/ACF field and the Core field. If that’s the case, should we consider adding:

  • A new sub-setting when a custom/ACF target is chosen (checkbox: “Also insert into core excerpt”), or
  • New dropdown options in the existing “Target field” selector, like “Both: Custom & Default” and “Both: ACF & Default”.
  1. Location of the Generate Excerpt Button

if I choose ACF or custom meta, I don’t love the idea of having the Generate Excerpt button show in the Excerpt panel

Personally, I feel the current location is still reasonable for now since we provide a clarifying note (screenshot above), but in the future, we could revisit placement. However, do you have a preferred spot in mind? Happy to make those changes. @iamdharmesh would also love your input here or on any other points.

  1. Bringing Core Changes to ClassifAI

WordPress has changed how the excerpt panel works, and it’s been on my list to bring those changes over to ClassifAI

Could you elaborate on what specific changes have been made in core? That would help us plan how best to align.

@dkotter dkotter modified the milestones: 3.7.0, 3.8.0 Sep 11, 2025
@faisal-alvi faisal-alvi added the needs:feedback This requires reporter feedback to better understand the request. label Sep 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs:code-review This requires code review. needs:feedback This requires reporter feedback to better understand the request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow Excerpt Generation to target alternate fields (e.g. custom excerpt, subtitle, or dek fields)
3 participants