Support

Account

Home Forums Backend Issues (wp-admin) ACF Save Post Failing

Solving

ACF Save Post Failing

  • I have a basic function hooked to the ACF “save_post” action, and every time that I actually go to save a page, it fails out with 2 JS errors:

    [Error] TypeError: 'null' is not an object (evaluating 'acf.o.post_id')
    	(anonymous function) (input.min.js, line 13)
    	c (load-scripts.php, line 3)
    	fireWith (load-scripts.php, line 3)
    	ready (load-scripts.php, line 3)
    	q (load-scripts.php, line 3)

    and

    [Error] ReferenceError: Can't find variable: wp
    	init (input.min.js, line 13)
    	(anonymous function) (input.min.js, line 13)
    	dispatch (load-scripts.php, line 4)
    	handle (load-scripts.php, line 4)

    My code is a simple snippet that compiles my PHP CSS files to pure CSS which I then include in my document. Here’s my code:

    function requireToVar($file){
                    ob_start();
                    require($file);
                    return ob_get_clean();
            }
    
    function on_acf_page_save( $post_id ){
            $main_compiled_contents = requireToVar('css/source/style.php');
            $main_css_file = fopen(get_template_directory().'/css/compiled/style.css', 'w+');
            fwrite($main_css_file, $main_compiled_contents);
            fclose($main_css_file);
    
            $laptop_compiled_contents = requireToVar('css/source/laptop.php');
            $laptop_css_file = fopen(get_template_directory().'/css/compiled/laptop.css', 'w+');
            fwrite($laptop_css_file, $laptop_compiled_contents);
            fclose($laptop_css_file);
    
            $tablet_compiled_contents = requireToVar('css/source/tablet.php');
            $tablet_css_file = fopen(get_template_directory().'/css/compiled/tablet.css', 'w+');
            fwrite($tablet_css_file, $tablet_compiled_contents);
            fclose($tablet_css_file);
    
            $mobile_compiled_contents = requireToVar('css/source/mobile.php');
            $mobile_css_file = fopen(get_template_directory().'/css/compiled/mobile.css', 'w+');
            fwrite($mobile_css_file, $mobile_compiled_contents);
            fclose($mobile_css_file);
    
    }
    
    add_action('acf/save_post', 'on_acf_page_save', 20);

    Any insight as to what would be causing this issue greatly appreciated.

  • Hi @thoughtspacewebsites

    Does the above code create the files?
    It is not possible for the above code to effect any javascript on the edit screen, so perhaps there is no issue here at all?

  • My code that I pasted runs through style.php files and compiles them into static style.css. The CSS files are what’s being called in my document. My goal is to have the CSS files be updated with new values every time an options page is saved. I’ve successfully implemented this on another client site, but for some reason it’s not working now. I thought it might have been an issue with the plugin.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘ACF Save Post Failing’ is closed to new replies.