ss_dom_before_save

How to use: add_filter()

Included in: Simply Static Pro 1.5.+


Sometimes you need to change something within your static files and this filter will help you doing just that. It can modify the parsed HTML from our crawler before we saving it to the disk.


A common example are JSON strings inside of scripts tags like in the Yoast SEO plugin where the schema is outputted that way.

If you want to change the URL inside of it, you can use the following code snippet to handle it:

add_action( 'ss_dom_before_save', function ( $dom, $url ) {
    $options = Simply_Static\Options::instance();

    foreach ( $dom->find( 'script.yoast-schema-graph' ) as $script ) {
       $decoded_text      = html_entity_decode( $script->outertext, ENT_NOQUOTES );
       $text              = preg_replace( '/(https?:)?\/\/' . addcslashes( Simply_Static\Util::origin_host(), '/' ) . '/i', $options->get_destination_url(), $decoded_text );
       $script->outertext = $text;
    }

    return $dom;
}, 10, 2 );

You can add this to the functions.php of your child theme or use the Code Snippets plugin to add it.