0

I am making some log for all the action done in the site that can be store in a .txt file.

my php code in working fine when doing a normal load, but when i use ajax it seems like i doesn't read the fopen, fpost method on php..

please check my code.

This is my function and ajax handler

function action_log( $msg ) {
    $current_user = wp_get_current_user();
    $path = "wp-content/themes/creation/templates/inc/log.txt";
    $myfile = (file_exists($path)) ? fopen($path, "a+") : fopen($path, "w+");
    $txt = $msg.$current_user->display_name."\n";

    if ( $myfile ) {
        fwrite( $myfile, $txt );
        fclose( $myfile );
        chmod($path, 0777);
        $result = $txt;
        return $result;
    } else {
        $result = $myfile;
        return $txt;
    }
}

add_action('wp_ajax_action-log-ajax', 'action_log_ajax');
add_action('wp_ajax_nopriv_action-log-ajax', 'action_log_ajax');
function action_log_ajax(){

    $post = $_REQUEST;
    $msg = $post['msg'];
    $write = action_log( $msg );

    exit( wp_send_json_success( $write ) );
}

My jquery plugin function

  $.send_log_changes = function( $msg ) {

    var msgs = $msg;
    var action = 'action-log-ajax';
    console.log(msgs);

    $.post({
      url: plugins_ajax.ajaxUrl,
      method: 'POST',
      data: {
        action: action,
        msg: msgs
      },
      dataType: 'json',
      success: function( respones ) {
        console.log(respones);
      }

    });
 }

I tried using .ajax, .get, .post and still the same error. it always return in Falseand cant write/open in the file.

  • Can you check the code here: add_action('wp_ajax_nopriv_action-log-ajax', 'action_log_ajax');, I believe it's should be wp_ajax_nopriv_action_log_ajax – Tung Du Apr 15 '16 at 3:20
  • @TungDu - hey bud, thanks for the answer, but the action-log-ajax is having no problem about the code. it is only a call, and the ajax is working and returning value, but it returns a false value, that cant read the fopen() and fwrite() – Sheldon Alag Apr 15 '16 at 3:25
  • sorry, a bit misreading. For file handling in WP, I think you should use WP FileSystem. Tut here, it may help: ottopress.com/2011/tutorial-using-the-wp_filesystem – Tung Du Apr 15 '16 at 3:54
  • 1
    you should never write anything to the theme directory. It will most likely be write protected or you will cuase huge mess on a network install. – Mark Kaplun Apr 15 '16 at 5:00
  • okay, thanks for the tip. i will transfer the log when its fully done. – Sheldon Alag Apr 15 '16 at 5:05
3

I guess the issue with this line:

$path = "wp-content/themes/creation/templates/inc/log.txt";

You need to use the full path, not a relative part, while using fopen() or file_exists().

Something like this should fix your issue:

$path = get_template_directory() . "/templates/inc/log.txt";

get_template_directory() will return the absolute server path, without a trailing slash.

|improve this answer|||||
  • get_template_directory() will return the absolute path of the parent theme. so i used get_stylesheet_directory() and it seems like the problem is all about the path.. great help (y). – Sheldon Alag Apr 15 '16 at 5:03
  • Either way, I was trying to use get_template_directory_uri() and that was wrong ;) Thanks for this! – Tessa Apr 2 '18 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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