Skip to content

Fixed save callback to call before save OR delete (rather than only on the save branch) - #1

Closed
bobbysmith007 wants to merge 1 commit into
Automattic:developfrom
AccelerationNet:accel
Closed

Fixed save callback to call before save OR delete (rather than only on the save branch)#1
bobbysmith007 wants to merge 1 commit into
Automattic:developfrom
AccelerationNet:accel

Conversation

@bobbysmith007

Copy link
Copy Markdown

Also made the argument order match $custom_metadata_manager->
save_metadata_field for ease of calling the default
method. (Essentially I am trying to allow custom save permissions).

This seems like it could break existing code, but it also seems like a better use / definition of save_callback than previously.

This is my callback, does that seem like a reasonable use case for the save callback?
Is it safe to clear the save_callback then recall the default? It seems like this should work, but I dont yet have a grasp on how everything is fitting together.

function eit_readonly_save_cb($field_slug, $field, $object_type, $object_id, $value){
  global $custom_metadata_manager;
  if(current_user_can('edit_users')){
    $field->save_callback = null;
    $custom_metadata_manager->save_metadata_field( $field_slug, $field, $object_type, $object_id);
  }
}

Hope this helps,
Russ

the save branch)

Also made the argument order match $custom_metadata_manager->
save_metadata_field for ease of calling the default
method. (Essentially I am trying to allow custom save permissions).
@jkudish

jkudish commented Feb 2, 2012

Copy link
Copy Markdown
Contributor

We already have a required_cap paramater when registering a field, wouldn't that be enough for what you're trying to do?

@bobbysmith007

Copy link
Copy Markdown
Author

The problem I was having with required_cap, was that it completely removes the field. I started with a display_callback that removed the input tags in favor of a span when needed. This caused the data to always be removed when the user saved their own profile.

function eit_readonly_display_cb($slug, $field, $otype, $oid, $value){
  global $custom_metadata_manager;
  if(current_user_can('edit_users')){
    $field->display_callback = null;
    $custom_metadata_manager->_display_metadata_field($slug, $field, $otype, $oid);
  }
  else{
    $label = $field->label;
    if(is_array($value)) $value = implode(', ',$value);
    echo "<span class=\"readonly custom-metafield $slug\">$label: $value</span>";
  }
}

Its certainly possible I am missing an obviously better way to accomplish these tasks. Aside from that though, it seemed to make more sense to call the "save_callback" before saving/deleting ANY changes. Previously it would only call on the save branch (not when deleting values). It also seemed to make more sense to allow the callback to call the original function in certain situations, which made the public save function a more obvious place to call from.

@jkudish

jkudish commented Feb 2, 2012

Copy link
Copy Markdown
Contributor

Fair enough. I'll review your proposed changes in more details and act accordingly either later today or over the weekend.

Thanks for the input!

@ghost ghost assigned jkudish Feb 16, 2012
@jkudish jkudish removed their assignment Jan 8, 2016
@GaryJones
GaryJones changed the base branch from main to develop August 19, 2026 10:26
@GaryJones

Copy link
Copy Markdown
Contributor

Thank you for this, @bobbysmith007 — and sincere apologies it's been open since 2012. The save/delete path has been substantially refactored since, so the patch no longer applies, and the cleaner approach to hooking deletion would be a dedicated delete callback rather than reusing the save callback. There's been no further demand in the years since, so I'm closing it as part of a backlog tidy-up rather than opening a speculative issue. Thank you for taking the time to contribute.

@GaryJones GaryJones closed this Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants