0

this hook should normally be triggered on new post but it doesn't work

add_action("new_to_publish", "doSomething", 10,1);

function doSomething($post){

global $post;

$_SESSION['yeni'] = 'test';
}

echo $_SESSION['yeni'];

1 Answer 1

1

When you create a new post, the initial post status is not new, but rather auto-draft. Try this:

add_action("draft_to_publish", "doSomething", 10,1);

function doSomething($post){

    global $post;

    $_SESSION['yeni'] = 'test';
}

echo $_SESSION['yeni'];

Also note that you may want to make sure your session is properly initialized. To verify the callback works at least, enable WP_DEBUG and WP_DEBUG_LOG and do something like

add_action("draft_to_publish", "doSomething", 10,1);

function doSomething($post){
    error_log( 'Inside the new post transition');
}

Then publish a post and look at wp-content/debug.log

4
  • thanks so how can we do when post modified Mar 3, 2021 at 14:59
  • 1
    There are several actions that fire when a post is updated, edit_post_{$post->post_type}, edit_post, and post_updated. You probably want the latter.
    – phatskat
    Mar 3, 2021 at 16:15
  • These codes bring the topic to the top when comments are made, as I comment on the topics, I want to capture them with the function and count them as if there are 5 current topics to the user. array( 'post_modified' => current_time('mysql'), // string ), Mar 3, 2021 at 16:33
  • @MehmetCemil I'm not sure of exactly what you need here - it also seems like you might want to ask a new question for this as I can't tell if it's related.
    – phatskat
    Mar 4, 2021 at 14:45

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.