Support » Developing with WordPress » help with beginner php in child theme functions.php

  • Resolved aricci

    (@aricci)


    I’m a noob with php but I’m think I’m half way to my solution. I want to add an additional header (html image) above the featured image on blog post pages and on the blog archive page. I’ve set up a staging site and installed a child theme. On its functions.php I enqueued the styles and then added my code:

    // Add content header above post featured image 
    add_action( 'astra_entry_top', 'add_blog_header' ); 
    function add_blog_header() { 
    // If we're on a single post or the blog archive page 
    if ( is_single() OR is_home() ):  
    // Echo the html 
    ?> <p class="blog-header"><img class="aligncenter size-full wp-image-18927" src="https://www.annabelfrage.com/wp-content/uploads/2020/12/blog-header-stolen-moments.png" alt="Stolen Moments ~ Welcome to my blog" /></p> 
    <?php endif; ?>

    Something is not right though. Wp won’t save it and the message from WP is: Scrape nonce check failed. Please try again.
    Any suggestions?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter aricci

    (@aricci)

    Follow up: I just googled Scrape nonce check failed and tried some solutions, none worked:

    • disabled SG Optimizer
    • edited the URL of the image to the staging site I’m working from rather than the live site
    • emptied the cache
    • Thanks for any pointers.

    Moderator bcworkz

    (@bcworkz)

    I’ve not encountered that error before, but it appears to be related to security measures used when using the built-in theme editor. It might not reflect any issues with your code. None the less, your code has a syntax error, so it’s good it wasn’t saved. If you are editing your active theme, you would have been locked out had your code been saved successfully.

    You need a terminal } to demarcate the end of your function declaration (after endif;).

    Assuming this code is at the very end of functions.php, don’t include the final ?>. Including it can introduce hard to locate errors. Dropping it does not cause any syntax error despite the earlier <?php not being closed. Including it can cause stray whitespace to be output and if that happens, PHP will throw “headers already sent” errors, which is misleading and doesn’t tell you the actual root cause.

    To be safe, switch to a different theme while you edit your theme’s files. After saving, you can switch back. If you’ve introduced any serious errors that got past the code linter, the theme will fail to activate.

    Thread Starter aricci

    (@aricci)

    Brilliant!

    On the staging site I fixed the syntax as you suggested, disabled all the plugins, emptied the cache and it still wouldn’t save but I switched themes to twenty twenty-one and that did the trick. There were no issues when I re-enabled the plugins or theme and the new header displayed just as I wanted it to.

    Knowing I could push the staging site to live if necessary I tried a shortcut and copied the saved functions.php to the live site child theme and it worked like a charm. You fixed two issues at once, thanks ever so much.

    Thread Starter aricci

    (@aricci)

    All fixed

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘help with beginner php in child theme functions.php’ is closed to new replies.