I have a php script that will generate a text file based on the post ID passed into it. I have it set so that it will run when a user publishes a post, I know this is functioning because if I screw up my script an error results while publishing a post. The problem is that it is not generating the text file like I want and I can't figure out why not. The script on it's own(if I just enter the address in my browser), works just fine and generates the text file. But when it runs from the publish action(no errors) no text file is generated, or atleast the text file did not show up where I intended.
Does anyone have any idea why this is occurring? If running from a publish hook, does it do something special to disable file generation? or does it put it somewhere that overrides my target location?
add_filter('publish_post', 'call_my_php_file');
function call_my_php_file($post_id){
$id = $post_id;
require_once('singleJSONgen.php');
}
<?php
/*$wp_root = '../../../..';
if (file_exists($wp_root.'/wp-load.php')) {
require_once($wp_root.'/wp-load.php');
} else {
require_once($wp_root.'/wp-config.php');
}*/
//$id = $_GET['p'];
$permalink = get_permalink( $id );
$thepostinfo = get_post($id);
$title = $thepostinfo->post_title;
$author = $thepostinfo->post_author;
$thecats = get_the_category( $id );
$catArr = array();
foreach ($thecats as $onecat) {
array_push($catArr, $onecat->cat_name);
}
$excerpt = $thepostinfo->post_excerpt;
//$commentCounts = $thepostinfo->comment_count;
$regularThumb = '';
$relatedArticleArr = '';
$includedVideos = get_post_meta($id, 'youtube-video');
$includedGalleries = get_post_meta($id, 'image-gallery');
$refGames = wp_get_object_terms($id, 'game');
$refGamesArr = array();
foreach ($refGames as $onegame) {
array_push($refGamesArr, $onegame->cat_name);
}
$viewcount = get_post_meta($id, 'views', true);
$swfFileLocation = get_post_meta($id, 'swfFile', true);
$specialThumb = get_post_meta($id, 'thumb-special', true);
$articleType = get_post_meta($id, 'article-type', true);
$articleDetails = array(
'articleID' => $id,
'articleURL' => $permalink,
'articleType' => $articleType,
'articleTitle' => $title,
'articleAuthor' => $author,
'articleThumb' => $regularThumb,
'articleThumbSpecial' => $specialThumb,
'articleExcerpt' => $excerpt,
'articleSWFLocation' => $swfFileLocation,
//'articleCommentCount' => $commentCounts,
'articleViewCount' => $viewcount,
'articleCategories' => $catArr,
'articleRelatedArticles' => $relatedArticleArr,
'articleIncludedVideos' => $includedVideos,
'articleIncludedImageGalleries' => $includedGalleries,
'articleReferencedGames' => $refGamesArr
);
//echo json_encode($articleDetails);
$target = '/json/' . $id;
//get the data
$jsondata = json_encode($articleDetails);
//write the data
file_put_contents($target, $jsondata);
//echo $jsondata;
//echo 'if you got this far without any errors, you are awesome, and probably have an xml file named '.$target.' in your folder.';
?>
/json/directory exist in the root?, when you start the path with/you're basically telling the script to use a path based on the root level folder, i'd guess that's where you issue lies(also turn on debugging if you havn't already). – t31os Dec 23 '11 at 12:44