Which action fire in front-end single post only

I need to add an action in the following situation : When it load a single page post in the frontend, before anything is displayed. I looked the ‘the_post’ action, but it’s triggered at several areas, including admin.

There are probably a lot of way to do this, but I’m fairly new in wordpress developpment, and can’t find the answer.

1 Answer
1

You can use the_content filter if you need to modify the content:

add_filter( 'the_content', 'cyb_content_filter_callback' );
function cyb_content_filter_callback( $content ) {

    // Only for singular post views and for posts of main loop
    if( is_singular( 'post' ) && in_the_loop() ) {
        // modify $content here
    }

    return $content;

}

If you need an action that runs before the post is displayed, maybe loop_start is what you are looking for (not sure without knowing what exactly you need to do, but this action is the one that fires just before the post data is setup when the loop starts):

add_action( 'loop_start', 'cyb_loop_start_action_callback' );
function cyb_loop_start_action_callback( $wp_query ) {

    // Only for singular post views and for posts of main loop
    if( is_singular( 'post' ) && in_the_loop() ) {
        // do something
    }

}

Leave a Comment