publish_post not working
-
Please help! I can not for the life of me get this to work, it never fires. I am trying to create a function to do something when a post is first published.
This code never seems to get called:function on_post_publish( $ID, $post ) { // A function to perform actions when a post is published. echo '$ID: '; var_dump($ID); echo '$post: '; var_dump($post); echo '<script type="text/javascript">alert("Made it to the function!");</script>'; } add_action( 'publish_post', 'on_post_publish', 10, 2 );
-
explain what exactlt you are trying to accomplish. hopefully, any body expert in that area may chime in to help you
Thanks Tara,
Right now I have a custom post type for projects. The client will be entering details along with the address of the location. When the post is first published I want to Geoencode the address to the LAT and LON for the address. So I am trying to hook when the post is first published to fire off the code to do the encoding. No mater what I do I can’t get it to run when the post is saved. I have tried about 20 different ways to do it and nothing works.
My last attempt was this, and still nothing!function on_post_publish( $strOldStatus, $strNewStatus, $post) { // A function to perform actions when a post is published. if( ( $strOldStatus === 'draft' || $strOldStatus === 'auto-draft' ) && $strNewStatus === 'publish' ) { echo '<script type="text/javascript">alert("This was a NEW post");</script>'; } if( $strOldStatus === 'publish' && $strNewStatus === 'publish' ) { echo '<script type="text/javascript">alert("This was an UPDATE to a post");</script>'; } } add_action( 'transition_post_status', 'on_post_publish', 10,3);Thanks,
GavinYou can use the second param in the publish_post hook to find out which post type you are looking for – In this case, your CPT, projects.
Something like this should work – I didn’t test it though.
function on_post_publish( $ID, $post ) { if ('project' == $post->type) { // your code } } add_action('publish_post', 'on_post_publish', 10, 2);Craig,
Wouldn’t just the publish_post hook any and all postings, both standard as well as CPT?
Thanks,
Gavin@ghmedia – you could also try changing the hook from publish_post to publish_project to adhere to the post status transition {status}_{post_type}
Wouldn’t just the publish_post hook any and all postings, both standard as well as CPT?
I do not believe that is the case. There is even a seperate hook for pages which is really just a post type of ‘page’. If publish_post caught everything, there would be no need for that hook or many others.
Using your original publish_post code, does it fire off when you publish a normal post?
No, and using the new it never fires either. This is driving me nucking futs! I don’t get why it never fires. Not on localhost or on a hosted site.
This is the new code, and still no joy, I am at a total loss.function on_post_publish( $ID, $post ) { if ('project' == $post->type) { echo '<script type="text/javascript">alert("This got called, project");</script>'; } } add_action('publish_post', 'on_post_publish', 10, 2);Just did a little testing – If you drop a die(); after the echo, you will see the alert show. Something with the way WordPress is doing the redirection is causing the alert not to show.
In short: It IS firing off – You just aren’t seeing any visible output.
UGH!!!! Thanks Craig! That just kills me! Spent ALL day yesterday trying to figure out what the hell I did wrong, only to find nothing! 🙂
I appreciate your input and help! Means a lot to me!
GavinNo problem – That was an interesting one and something I wasn’t aware of either.
Just a side note: Not sure if you are using this function in a theme or plugin but I would recommend namespacing your custom functions (even just a ghm_ prefix or something) – Chances are, somewhere down the line there will be another plugin or theme that uses a function named on_post_publish and it could cause some problems.
Good luck!
That one I did already do Craig, I do have it’s own namespace, want to keep the conflicts to a minimum! Thanks again for the tips!
Ok, new problem. The custom post fields are not being updated unless I die(); in my functions.php
Is there something I need to call after this in order to commit it it to the DB?update_post_meta($postID, 'latitude' , $latitude); update_post_meta($postID, 'longitude', $longitude);If I follow with a die(); it will update the post, if I don’t it doesn’t? WTH??
Thanks,
GavinOk, new problem. The custom post fields are not being updated unless I die(); in my functions.php
Is there something I need to call after this in order to commit it it to the DB?update_post_meta($postID, 'latitude' , $latitude); update_post_meta($postID, 'longitude', $longitude);If I follow with a die(); it will update the post, if I don’t it doesn’t? WTH??
Thanks,
GavinGavin, can you provide more context to the latest problem? Where are those update_post_meta() calls happening? Inside the on_post_publish() function you had in your previous code examples, or somewhere else?
Hi Dave,
Yes it is called from that function, does the Geoencoding, then updates the post_meta. At that point it exits. If I put a die() there it actually updates the post_meta, if I don’t have the die it goes back to the edit screen and nothing gets saved.
die() must be doing some cleanup that I need to do, just don’t know what.
Thanks again
GavinHmm.. not really sure what is going on there but I can do some more testing tonight and get back to you if you haven’t already figured it out.
The topic ‘publish_post not working’ is closed to new replies.