@mslade
Couldn’t you just access the global $post variable?
global $post;
$id = $post->ID;
Thread Starter
mslade
(@mslade)
That works in some cases (including the one above, so thanks for the suggestion), but what if mail() were called via subscribe2_cron()? I believe in that case there’s a list of posts in context, but I don’t think there’s any way to get at them.
@mslade
If you are using the ‘s2_custom_keywords’ then a second parameter is passed containing the digest post ids in comma separated string format.
Thread Starter
mslade
(@mslade)
Sorry to resurrect, but I just bumped into this problem again and found a new use case where not receiving an explicit post ID is limiting.
When a post publish is triggered via WP cron, there is no global $post and get_the_ID() returns null. In this case S2 follows the logic path for publishing a single post, which fires s2_custom_keywords with only the content, but unless I’ve overlooked something there’s no way for me to see what post is being acted on.
@mslade
This strikes me as a little strange. Subscribe2 hooks into the WordPress post transitions function to generate the emails. These actions pass the $post variable so it must be populated. If is wasn’t populated then the post content and ID would not be available to Subscribe2 to collect any necessary meta.
Can you put your code on pastebin or similar for review?
Thread Starter
mslade
(@mslade)
Thanks for the quick response.
The simplest way to demonstrate is with an s2_custom_keywords hook that does nothing other than probe for get_the_ID() and the $post global. If you’re using a debugger you can pause it at the return statement to see what $post and $id are set to; otherwise toss in a var_dump or error_log.
http://pastebin.com/M6UTZmDP
You’re right that the actions that WP fires are passing the $post variable to S2’s publish(), but WP does not set them up as a global. As a result publish() knows which post triggered the email but since it doesn’t pass it along the value dies there.
@mslade
I haven’t had time to do any testing but…
get_the_ID() is a function that needs to be called from ‘In The Loop’ and this is probably going to evaluate to false when cron is used to publish a post.
The $post global should be available, and that contains the post ID already at $post->ID, so that should remove the need to call a function to access the post ID anyway – give that a try.
Thread Starter
mslade
(@mslade)
Unfortunately, the $post global is not available during cron publishing, either. It’s null. In fact, get_the_ID() uses get_post() and get_post() relies on the $post global, so any time get_the_ID() doesn’t work then the $post global will not work, either.
Are you a maintainer? If I submitted a patch to add the post context to s2_custom_keywords, would you be open to merging it into the plugin?
@mslade
I’m not a committer to the code trunk anymore. Still not sure why $post is null as Subscribe2 relies on it being set in order to work. Let me do some testing myself, by all means try submitting a patch here:
https://plugins.trac.ww.wp.xz.cn/query?component=subscribe2&status=!closed&order=id
Thread Starter
mslade
(@mslade)
Oh okay, thanks a lot. I’ll see about sending in a patch to them, although seeing your 14 month old patch with no updates doesn’t bode well for me π
@mslade
I’ve just noticed something in your example code, you have this line:
add_action ( 's2_custom_keywords', 'my_s2_custom_keywords' );
Rather than using add_action() you should be using add_filter(). add_action() is used to trigger an associated event but that doesn’t change any core content and it always return the boolean true rather than returning any content, add_filter() is the function to use to change something and return the modified value.
So, try using:
add_filter( 's2_custom_keywords', 'my_s2_custom_keywords' );
@mslade
Okay, I’ve done some testing and it does seem as though WordPress does not create a $post global for scheduled posts – or it it is created it is also quickly destroyed.
The simplest way I can think of to fix this reliably is to create a variable within Subscribe2, in the publish function that copies the post ID to a Subscribe2 class variable that can then be accessed via the $mysubscribe2 global.
It’s a single line addition within that function like this:
$this->post_id = $post->ID;