Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.';

?>
share|improve this question
So does the /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
Since the script runs by itself, and the add_filter needs to be an add_action, I think the issue is that the PHP "script" is fine but the hook needs fixing. – brandwaffle Dec 23 '11 at 15:29

closed as too localized by toscho Feb 19 at 0:03

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

2 Answers

Filters require that you return the variable that's inputted in the function. If you don't do this, then subsequent code might not get the correct values and weird behavior might result. Make sure you're doing this at the end of your call_my_php_file function:

return $post_id;

That may or may not fix the problem, but it's a key first step.

Also, are you sure publish_post is a filter hook, and not an action hook? According to the codex, it seems to be the latter: http://codex.wordpress.org/Plugin_API/Action_Reference

In that case, you'd want to change your code to

add_action('publish_post', 'call_my_php_file');

and you would NOT need to return anything.

UPDATE: One more idea, depending on the version of WP that you're using, you may actually want the 'published_post' hook: http://adambrown.info/p/wp_hooks/hook/publish_post

share|improve this answer
I'll look into changing that to action instead of filter. I read somewhere it should be filter. It seems to be working now on filter though. – Ghost9 Dec 23 '11 at 15:53

Turns out it runs out of the wp-admin folder and that's where it saves files by default. Now that I know the base folder I can tell it where to save the files accordingly.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.