Using error_log(“some message”) not being written to error_log

  Kiến thức lập trình

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.

LEAVE A COMMENT