I needed to develop a technique to delete some data from the database.

The technique will be part of a plugin at some point.

For immediate results, I was editing functions.php on my local dev site, placing the necessary commands at the bottom of the file and reloading the page upon saving.

Is this considered bad form?

If so, what's the recommended method for running small bits of test code as described?

Fwiw, here is a link to the code in question: https://wordpress.stackexchange.com/a/267886/60200

  • why not make a button and then check isset? as part of the function? – rudtek May 24 '17 at 19:06
  • I think this is a good question, but it's also one that will draw opinionated answers, so it's probably not a good fit for this site. That said, what you're doing will get r' done, but it would be tidier to add this code to a plugin. The code should be wrapped in a function called by a hook too, e.g. add_action( 'init', 'my_debug_function' ); function my_debug_function() { ...debug code }. The plugin approach will let you easily enable and disable the triggering of your debug code. Adding a plugin options page to fire the code conditionally would be another step up. – Dave Romsey May 24 '17 at 19:07
  • Or an admin dashboard widget to fire it on submit. I agree a plugin feels more "the WP way" than a theme's functions.php for snippet testing. – WebElaine May 24 '17 at 19:09
  • Thanks for the comments. Very helpful. Agree on the concern re: discussion. I tried to formulate a question that would get more facts and less opinion :-) So far, the answers have been helpful and on point (whew). – Mike May 24 '17 at 20:09
up vote 1 down vote accepted

There isn't really a prescribed way to do this, but it's probably safer to hook an action like init to be sure everything you need is loaded, and then maybe check for some sort of flag so you can control which page loads run your code.

function my_test_func(){
    if( isset( $_GET['do_my_test_thing'] ) ){
        // your code
    }
}
add_action( 'init', 'my_test_func' );

Then add ?do_my_test_thing=true to the URL to explicitly run your code on that page load.

There is no place in WP that is considered to be purposefully meant for developing and debugging of code.

People can get opinionated on appropriate ways to distribute code (mostly theme vs plugin).

But for private development you can go with pretty much any workflow and code placement that makes sense to you. Personally I use a mix of empty theme and must use plugins.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.