Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

WordPress/PHP has me stuck with no logging...

When I check the debug.log, I see:
Step 1
Step 2

Here is the code:
error_log("Step 1", 0);


add_action( 'save_post', 'ajax_test' );
function ajax_test() {   
    error_log( "ajax_test entered", 0);
}
error_log("Step 2", 0);

Open in new window

So, this means EITHER:
1) error_log() does not work in an action
2) The code does not enter the function, ajax_test()

Is there a plug-in needed for logging inside of an action? Will this one help?
https://wordpress.org/plugins/aryo-activity-log/

Is there another way to log data, besides error_log()? Please let me know...

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of curiouswebster

ASKER

Thanks. But I still have a problem. I created that function, but still can not assert my Action function is getting entered...

unction write_log($log) {
    if (true === WP_DEBUG) {
        if (is_array($log) || is_object($log)) {
            error_log(print_r($log, true));
        } else {
            error_log($log);
        }
    }
}

write_log('About to Enter ajax_signup_insert_contact()');

add_action( 'save_post', 'ajax_signup_insert_contact' );
function ajax_signup_insert_contact() {  
    die('INSIDE THE FUNCTION'); 
}

write_log('About to Exit PHP script');

Open in new window


The output from the debug.log looks thusly, and shows the function was not entered...

[17-Jun-2021 20:26:30 UTC] Typical logging
[17-Jun-2021 20:26:30 UTC] About to Enter ajax_signup_insert_contact()
[17-Jun-2021 20:26:30 UTC] About to Exit PHP script

Open in new window


The die() function, I assume, would have meant the final "Exit PHP script" message not to show. I will close this question and open another one on the add_action() problem. Thanks.
1) How do I know if the logging is Failing or the function is Not getting entered?

Based on your debug.log file contents, logging appears to be working.

2) What is required to have an action?

save_post only files when you save or update a post, not on page visits.

3) Is there something key missing aside from just calling add_action()?

Your code looks correct.

4) Is the Action WordPress?

Unsure exactly what this might mean.

Yes... you're running inside WordPress, so the "Action" is "WordPress".

5) Does it have requirements for a function declared as 'save_post'?

No.

Your code above looks correct, just make sure you're actually creating a new post or saving to an existing post after an edit.
The following is the insert statement. Is this a post?

add_action( 'save_post', 'ajax_signup_insert_contact' );
function ajax_signup_insert_contact() {  
    die('INSIDE THE FUNCTION'); 
    write_log('Entered ajax_signup_insert_contact()');
    global $wp_query;
    $page_id = $wp_query->post->ID;
    $page_name = $wp_query->post->post_title;    
    
    /*if(isset($page_title) === false || $page_title === '') {
        // The title is empty, so Throw a Fatal Error()   
    }*/
    error_log( "Step 20", 0);
    if(is_page()){ //Check if we are viewing sign up page
        write_log( "Step 30");
        if($page_id === 37){  
            write_log("Step 40");
            global $wpdb;
            
            $fname = 'C';
            $lname = 'D';

            $table_name = "ps_contacts";

            if ( $wpdb->insert(
                $table_name,
                array(
                    'fname' => $fname,
                    'lname' => $lname,
                )                
            ) == false)
            {
                write_log( "FAILED");
            }
        }
    }
}

Open in new window