• Help:
    I have been using code snippets for a variety of reasons.
    It has been a spectacular plugin!!!
    Until…
    I noticed that it stopped working with a snippet I use to update the options table.
    I have put together a minimalist snippet to test (see below).
    When I save it and Run Once, messages appear that ‘Snippet added and executed’
    However, the database was not updated.
    No errors are visible or written to the error log files.
    The remainder of the website is working as expected.
    I have tried:
    – setting WP_DEBUG to true
    – viewing errors and logs in QM Monitor
    – Turning off ALL plugins
    – Deactivating, Uninstalling and Reinstalling the Code Snippets plugin
    I would really, really appreciate your help in troubleshooting and resolving this.

    Bob
    ———————————————-
    // Minimal Snippet to Run Once (Note: reverse single quotes added in copy/paste

    $scc_bob = array(
    	 'first' => 'Redrock',
    	 'last' => 'Bob'
    );
    //Update entire array
    update_option('scc_bob', $scc_bob);
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hi Bob,

    It looks like the problem here is not that the snippet is not running, but that it is just having no effect.

    Calling functions like update_option does not work if it happens too early in the WordPress load process. At the time when snippets are run, the list of options have typically not yet been fetched from the database, so the process fails.

    The fix is simple – wrap functionality like this in a basic hook like init to ensure that it only runs once WordPress has been fully loaded:

    add_action( 'init', function () {
    	
    	$scc_bob = array(
    		 'first' => 'Redrock',
    		 'last' => 'Bob'
    	);
    	//Update entire array
    	update_option('scc_bob', $scc_bob);
    
    } );

    Apologies for the delay in figuring this out! The example really helped.

    Thread Starter redrockbob

    (@redrockbob)

    @bungeshea Hi Shea. Thanks for the suggestion. I had high hopes. I tried attaching it to init and wp_loaded hooks and then ran each ‘run once’. Neither attempt was successful. Any additional suggestion would be appreciated. Thanks again for investigating this for me. bob

    Plugin Author Shea Bunge

    (@bungeshea)

    How are you determining whether the snippet succeeded? I checked by looking on the wp-admin/options.php page.

    Thread Starter redrockbob

    (@redrockbob)

    Hi again Shea (@bungeshea)
    I use phpmyadmin to query the options table before and after.

    bob

    Plugin Author Shea Bunge

    (@bungeshea)

    @redrockbob

    If you try a snippet like the one below, do you see any output in the admin footer when the snippet runs?

    add_action( 'admin_footer_text', function () {
    	
    	update_option( 'code_snippets_test_option', 2 );
    
    	echo 'snippet completed';
    } );
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Run Once Snippets Stopped Working’ is closed to new replies.