2

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.

  • Where/when exactly you need a hook? Before the content of main post? I ask becasue I'm not sure what you mean with "before anything is displayed" .... even before the head, menus, and so on, or just before the content of the post? – cybmeta Nov 30 '17 at 10:51
  • Excuse me, should have been more specific : Before the post content is displayed, because I need to either : Do logic (which would be an action) or modify the content of the post (which would be a filter). – Adam S Nov 30 '17 at 10:54
2

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
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.