2

I am trying to execute a function only if its called from a particular parent (or grandparent function, or great-grandparent, etc). I can achieve what I want to do using debug_backtrace, but I feel like this is not the correct way to do this. Take this for example:

function savethepost($post_id) {
$parent = debug_backtrace();
if ($parent[5]['function'] == 'bulk_edit_posts') {
    if ( isset( $_REQUEST['post_format'] ) && $_REQUEST['post_format'] != -1 ) {
        set_post_format($post_id, $_REQUEST['post_format']);
    }
}
}

This is using backtrace in Wordpress function to execute the set_post_format function only if the savethepost function is called from the bulk_edit_posts function, which is what I trying to achieve. But since this method is meant for debugging, is okay to use it this way? If not, how else could I achieve calling a function if only coming from a certain parent function?

  • _"execute a function only if its called from a particular parent"_ this seems like a very strange way to design a function. You _can_ do this, but it doesn't follow the principle of least surprise. Can you explain what you're trying to do? – Halcyon Nov 21 '15 at 18:07
  • Sure, in the WP plugin I am writing, savethepost is the save_post callback. According to the WP codex save_post is always called after the post is updated, but for some unknown reason when using the Bulk Edit feature, save_post is called before Post Format is updated (however not the case when editing single posts). The working code above manually updates the post format before i do other stuff (not shown) for which the new post format is required. Basically, I am just trying to create a single save_post callback, or rather only execute the set_post_format when using Bulk Edit – Troy Andrew Hallisey Nov 21 '15 at 18:15
  • I don't know enough about wp to comment on that. There's probably a proper way to do it. Maybe there is a bug in the Bulk Edit feature? – Halcyon Nov 21 '15 at 18:18
  • I read some docs on the WP Trac about this, and its apparently by design as Post Format is a type of taxonomy and some devs were uncomfortable with it being in Bulk Edit. – Troy Andrew Hallisey Nov 21 '15 at 18:22
  • Came across this [old question](https://stackoverflow.com/questions/346703/php-debug-backtrace-in-production-code-to-get-information-about-calling-method) that I didn't see before. The accepted answer applies in this case as well. – Troy Andrew Hallisey Nov 21 '15 at 18:56

1 Answers1

-1

Debug_backtrace kills the performance as it has to prepare the call trace. So this is not good idea to use. If you still need such logic try to archive it with a function argument