我的functions.php 中有以下代码,它在发布时执行脚本:
function save_new_post($post_ID) {
$site_root = '/home/forexmag/public_html/directory2';
$post = get_post($post_ID);
$title = $post->post_title;
$breaking_news = false;
$categories = get_the_category($post_ID);
if (is_array($categories) && !empty($categories))
{
foreach ($categories as $cat_det)
{
//this is the id for the breaking (bad) news
if (5668 == $cat_det->cat_ID)
{
$breaking_news = true;
break;
}
}
}
$exec_code = "/usr/local/bin/php
$site_root
/crons/cron_notify.php '$title' $post_ID 2 " . intval($breaking_news);
exec(
$exec_code
);
} add_action('draft_to_publish', 'save_new_post', 10, 1);
当我发布新帖子时,我不断收到以下错误:
Catchable fatal error: Object of class WP_Post could not be converted to string in /home/forexmag/public_html/wp-content/themes/forex_magnates/functions.php on line 712
这条线是我设置的地方$exec_code
变种然而,正如你所看到的,正如我通过 var_dump 确认的那样,$title
变量是一个字符串。我缺少什么?
编辑:更奇怪的是当我 var_dump$exec_code
并终止脚本,我得到一个完美的字符串:
string(121) "/usr/local/bin/php /home/forexmag/public_html/directory2/crons/cron_notify.php '2011 Free Forex Report' 9934 2 0"
在 WordPress Answers 上得到了正确答案。我想我应该分享它:
$post_ID
is a WP_Post
目的。您的执行代码应该有效地使用$post_ID->ID.
function save_new_post($post_ID)
{
print_r($post_ID);
die();
Returns
WP_Post Object ( [ID] => ...
致谢vancoder https://wordpress.stackexchange.com/users/26778/vancoder
请注意,这取决于您使用的挂钩。例如,如果您使用publish_page
你得到一个整数(这实际上是我困惑的根源)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)