Title: Improve code and logic
Last modified: July 16, 2020

---

# Improve code and logic

 *  [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/improve-code-and-logic/)
 * Right now, you put PHP code in the DB (PHP code snippet), which means that before
   executing the page, you ask the DB to load more PHP stuff. Your DB is a real 
   bottle neck and is on its knees once using code snippet together with all other
   plugins a site use or needs.
 * Why can’t you load the code as a plugin code instead of from the db?

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/improve-code-and-logic/#post-13139276)
 * Hi [@alriksson](https://wordpress.org/support/users/alriksson/),
 * It’s true that executing a database query will have an impact on the load time
   of your website. However, it’s important to keep in mind that there are already
   a number of database queries that are run simply as a part of having WordPress
   installed, and this is just one additional query.
 * If you’re really concerned that Code Snippets is having a performance impact 
   on your site, I recommend the [Query Monitor](https://wordpress.org/plugins/query-monitor/)
   plugin for actually measuring how long that query takes. On my site it takes 
   0.0003 seconds.
 *  Thread Starter [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/improve-code-and-logic/#post-13143849)
 * True but it also an issue of execution order and what I know we should not need
   to involve the db to execute php.
 * Not a developer but I think there could be room for improvements, on your site
   is it heavy or light or just a simple site you spinned up now to check?
    Please
   show me where in QM I can find code snippets queries.
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/improve-code-and-logic/#post-13143944)
 * The other option would be to store snippet code as files on the filesystem. This
   is something we can look into adding in the future, but my concern would be that
   gaining filesystem access can be tricky to do effectively on many hosts, due 
   to inconsistent permissions or particular server configurations, making it less
   of a reliable method.
 * The site is my personal website where I use the plugin – not a testing site or
   one I made on the fly. The time will of course change depending on the server
   and how many snippets you have.
 * If you have Query Monitor installed and the panel open (by clicking on the item
   in the admin bar), you’ll find the snippets database query by looking under the‘
   Queries’ section and scrolling down until you see a query that starts like this:
 *     ```
       SELECT id, code, scope
       FROM wp_snippets
       ```
   
 *  Thread Starter [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/improve-code-and-logic/#post-13146831)
 * > The other option would be to store snippet code as files on the filesystem.
   > This is something we can look into adding in the future, but my concern would
   > be that gaining filesystem access can be tricky to do effectively on many hosts,
   > due to inconsistent permissions or particular server configurations, making
   > it less of a reliable method.
 * Most performance and cache plugin require don’t think this would be a big issue
   but maybe less reliable. But why not add options to choose and by default use
   sql and db if possible? And once enable the file function ask one more time and
   maybe add a function to check if your plugin is able to write in the filesystem,
   as many other plugins do by checking and if not link to a documentation page 
   for further information to debug and last outcome go back to sql/db for the user.
   As you say some may not allow due to firewalls etc but still think most will 
   have a hassle free experience consider the norm of use of cache plugin which 
   normally need to have access.
 * Just suggesting here 🙂
 * > If you have Query Monitor installed and the panel open (by clicking on the 
   > item in the admin bar), you’ll find the snippets database query by looking 
   > under the ‘Queries’ section and scrolling down until you see a query that starts
   > like this:
   > SELECT id, code, scope
   >  FROM wp_snippets
 * Thanks, looks ok as you said so you have a point about it not being slow. But
   to me it sounds and what I heard from other using code snippet. Why in the world
   query sql to db to execute php if you understand.
 * And my main issue is also the other ticket where it is executed too late. But
   I love the UI so would like to keep the plugin
 *  Thread Starter [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/improve-code-and-logic/#post-13261144)
 * I would use taxonomy terms as wordpress cache this rather than involving the 
   db and write to files and then parse them for data as it’s faster. Maybe something
   you could look into.
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/improve-code-and-logic/#post-13267619)
 * I’m a little unsure how storing data as taxonomy terms would speed things up.
   As far as I can tell, the current method of using a custom table incurs the least
   overhead because there’s no need to store unnecessary metadata, and the important
   fields can be indexed.
 * I agree it would be beneficial to write to files for execution, and this is definitely
   something we are considering.
 *  Thread Starter [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/improve-code-and-logic/#post-13273678)
 * Wordpress cache taxonomy terms by default.
 * Write to files may not be faster than taxonomy terms would it? I’m just elaborating
   on improvements but as far as I know taxonomy terms would be faster but correct
   me if I’m wrong.
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/improve-code-and-logic/#post-13280726)
 * Taxonomy terms would not be faster than the current solution. It would still 
   involve reading/writing from the database, and with additional overhead.
 * Writing to files would need to happen at the point when a snippet is saved, and
   then that would (likely) speed up loading the snippets for execution. It does
   have a higher potential to create issues on certain hosts, however, which is 
   why it is not currently the method we use.
 *  Thread Starter [Rookie](https://wordpress.org/support/users/alriksson/)
 * (@alriksson)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/improve-code-and-logic/#post-13287806)
 * I might be wrong, suggesting her,e but wordpress taxonomty terms are cached by
   default in wordpress. So no queries is needed.
 * Feels like it’s faster than what’s today but maybe not faster than writing to
   files.
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/improve-code-and-logic/#post-13287859)
 * I’m not sure what you mean by ‘cached by default’. All WordPress data is stored
   in the database, and so it must be fetched from the database at least once every
   page load, unless you have some sort of persistent caching software installed.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Improve code and logic’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 10 replies
 * 2 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [5 years, 9 months ago](https://wordpress.org/support/topic/improve-code-and-logic/#post-13287859)
 * Status: not a support question