Search code examples
phpwordpresserror-handlingplugins

Using error_log("some message") not being written to error_log


I'm developing a plugin and trying to debug it. I've placed error_log statement is the parts of the code I suspect are causing the problem. But nothing is being written to the error_log file. I am seeing some entries cause by other parts of WordPress and a couple of other plugins, but not from my plugin.

I know the code is running because I've added some echo statement in the same areas, and they appear. I'm trying to debug an AJAX response handler. But none of the error_log statements add to the error_log. I have modified wp_config.php and set define( 'WP_DEBUG', true );. This does cause entries to be added to the log, but from other unrelated code.

I have also tried setting the related ini parameters, but they seem to be ignored.

Is this something WordPress is designed to prevent?

Here's the part of the code I'm working on:


function register_page_categories() {
    // Register the 'page_category' taxonomy
    register_taxonomy(
        'page_category',  // Taxonomy name
        'page',        // Object type (post type)
        array(
            'label'     => __('Page Categories'),  // Display name for the taxonomy
            'rewrite'     => array('slug' => 'page-category'),  // Custom slug for the taxonomy URL
            'hierarchical' => true,  // Enable hierarchical terms
        )
    );
}
add_action('init', 'register_page_categories');

// Register REST API routes
function spp_register_rest_routes() {
    register_rest_route('spp/v1', '/save-order-and-category', array(
        'methods'  => 'POST',
        'callback' => 'spp_saveOrderAndCategory',
        'permission_callback' => function () {
            return current_user_can('edit_pages');
        },
    ));
}
add_action('rest_api_init', 'spp_register_rest_routes');

// Save order and category via REST API
function spp_saveOrderAndCategory(WP_REST_Request $request) {
/*
    ini_set('display_errors', 1); 
    ini_set('display_startup_errors', 1); 
    error_reporting(E_ALL);
*/
    $params = $request->get_json_params();
    $order = $params['order'];
    $categories = $params['categories'];

    error_log("spp_saveOrderAndCategory - Order: " . print_r($order, true));
    error_log("spp_saveOrderAndCategory - Categories: " . print_r($categories, true));

    foreach ($order as $index => $pageId) {
        wp_update_post(array(
            'ID' => $pageId,
            'menu_order' => $index + 1,
        ));

        if (isset($categories[$pageId])) {
            assign_category_to_page($pageId, $categories[$pageId]);
        }
    }

    return new WP_REST_Response(array('status' => 'success'), 200);
}


The purpose of the code is to save the category assignment for a page(s).

But data is not being stored in the tables.

The AJAX call returns no error the browser. I haven't seen this problem before in PHP. Usually, I have used this in a PHP program to turn error logging on.


ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL);

But that hasn't had any effect on logging.


Solution

  • error_log can fail if php's user does not have write permission on log file or for many other reasons.

    if you are developing on a local machine you can invest some time in setting up the server correctly

    however, if your development environment is on a shared host you could replace error_log with file_put_contents es:

    //where PATH_WHRE_YOU_CAN_WRITE could be for instance: your_plugin_folder/logs <== NOT THE BEST SOLUTION
    //BE CAREFULL about where you place your logs if you don't want show them to world wide
    file_put_contents('PATH_WHRE_YOU_CAN_WRITE/php.log',  print_r(array("time"=>date("Y-m-d h:i:s"),"data"=>$DATA_YOU_WANT_LOG,),true) . "\n\n",FILE_APPEND);