Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Liam Maclachlan
Liam Maclachlan
22,805 Points

Does anyone know of a coditional to detect what action has been triggered?

I need to try and find a way of detecting what action has caused a particular function to fire off.

I'm want to avoid duplicating my code and run the some function 'save_post' and 'delete_post' with a condition on the save that does not run when status it 'trash'

My current solution is:

<?php

function sliced_audit_trail_main( $post_id ) {

    function main_stuff_here() {
        #code
    }
    return $post_id;
}


function sliced_onSave_audit_trail( $post_id ) {

    /* Get the post type object. */
    $the_post = get_post( $post_id );

    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    // don't save on 'new post'
    if ( get_post_status( $post_id ) == 'auto-draft' ) return;
    // don#'t save on delete 
    if ( get_post_status( $post_id ) == 'trash' ) return;

    sliced_audit_trail_main( $post_id )

}

function sliced_onDelete_audit_trail( $post_id ) {

    /* Get the post type object. */
    $the_post = get_post( $post_id );

    // run code without conditional used in save

    sliced_audit_trail_main( $post_id )

}

add_action( 'save_post', 'sliced_create_audit_trail' );
add_action( 'before_delete_post', 'sliced_onDelete_audit_trail' );

?>

In pseudo code, I would like to achieve something like this:

<?php

function sliced_audit_trail_main( $post_id ) {

    /* Get the post type object. */
    $the_post = get_post( $post_id );

    if ( is_action('save_post') ) :

        # conditionals to only be used when save submitted by user

    endif;

    function main_stuff_here() {
        #code
    }
    return $post_id;
}

add_action( 'save_post', 'sliced_audit_trail_main' );
add_action( 'before_delete_post', 'sliced_audit_trail_main' );

?>

1 Answer

I'm new to wordpress development too. I'm not sure if I can really help you.

I found the current_filter() helper function. The same function can you use for actions too. https://codex.wordpress.org/Function_Reference/current_filter

Does this solve your problem ?

Another interesting action hook I found is 'all'.

This action hook will be called for all actions. Could be interesting for logging purpose.

Regards, Felix

Liam Maclachlan
Liam Maclachlan
22,805 Points

Hey Felix,

That is really useful to know. This does help dry out my code quite nicely. I am calling this function from multiple hooks where each hook requires different conditionals; this will be perfect for that.

This edit to my code (in my initial question) runs perfectly now. Thanks for the info! Really appreciated :)