Title: Reusing code/snippets w/ variables
Last modified: June 21, 2018

---

# Reusing code/snippets w/ variables

 *  Resolved [MarioKnight](https://wordpress.org/support/users/marioknight/)
 * (@marioknight)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/reusing-code-snippets-w-variables/)
 * With the old system, I was using the following to generate my own gallery solution
   within pages as such:
 *     ```
       [insert_php]
       $aid = 1234;
       include('/path/to/script.php');
       [/insert_php]
       ```
   
 * This new system appears to want me to make a separate snipped for each possible
   one, and then change the above in pages to the new format. Already having to 
   change 100s of pages will be cumbersome (I do intend to be re-doing this site
   with CPTs and custom-made plugins at some point, but this will be a long process),
   and this looks to be doubled having two places to manage code instead of just
   one. Is there a way to have a single snippet created with the variable passed
   in the shortcode within the page? Or am I better off sticking with the old system
   as I work to re-do a lot of this site?

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

 *  Plugin Author [webcraftic](https://wordpress.org/support/users/webcraftic/)
 * (@webcraftic)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/reusing-code-snippets-w-variables/#post-10423902)
 * Hi,
 * I plan in the new version to make it possible to pass the value of the variable
   inside the shortcode so that you do not duplicate the snippets. Expect this decision,
   I think it has already appeared tomorrow.
 * Best regards, Alex
 *  Plugin Author [webcraftic](https://wordpress.org/support/users/webcraftic/)
 * (@webcraftic)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/reusing-code-snippets-w-variables/#post-10431784)
 * Hi,
 * In the new version of the plugin I made it possible to pass the value through
   shortcodes. You can get the values of the variables from the shortcode attributes.
   For example, if you set the my_type attribute for the shortcode [wbcr_php_snippet
   id=”2864″ my_type=”button”], you can get the value of the my_type attribute in
   the snippet by calling $my_type var.
 * Best regards, Alex
 *  Thread Starter [MarioKnight](https://wordpress.org/support/users/marioknight/)
 * (@marioknight)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/reusing-code-snippets-w-variables/#post-10438190)
 * Hello,
 * Thank you for that quick turnaround. I’ve tested this on my dev instance, and
   it works with no issues I can see. I’ll still need to decide if I want to do 
   this sort of switch or not given the scale and future dev work mentioned above,
   but at least this new system to your plugin works and is an option now, so I’ll
   mark this as resolved. Thank you!
 *  [sybok](https://wordpress.org/support/users/sybok/)
 * (@sybok)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/reusing-code-snippets-w-variables/#post-10442349)
 * This does not seem to be working for me. What does “calling $my_type var” mean?
 * In my snippet I do
    echo (“Price is {” . $thePrice . “}”); This yields an empty
   string in $thePrice. I.e., it prints Price is {}
 * The snippet is called by
    [wbcr_php_snippet id=”635″ thePrice=”32″]
 * I assume I need to do something else? I am running version 2.0.6 of PHP code 
   snippets.
 *  Plugin Author [webcraftic](https://wordpress.org/support/users/webcraftic/)
 * (@webcraftic)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/reusing-code-snippets-w-variables/#post-10442363)
 * [@sybok](https://wordpress.org/support/users/sybok/)
    Try to use variables without
   camelcase. It is better to separate the variable with a lower underscore.
 * `echo "Price is {" . $theprice . "}";`
 * `[wbcr_php_snippet id="2901" title="Echo test" theprice="32"]`
 * Best regards, Alex
 *  [sybok](https://wordpress.org/support/users/sybok/)
 * (@sybok)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/reusing-code-snippets-w-variables/#post-10442499)
 * OK, that seems to work. I don’t know why you would need to massage the attribute
   names, but OK. So underscores will work – do dashes?
 * You also seem to be doing some massaging of the value string. I am trying to 
   pass in a file name (image for a PayPal sales item). The file is something like
   abc.jpg. Took me awhile to realize you change the period to a dash, so the file
   becomes abc-jpg. Obnoxious that I have to write code to convert back to the correct
   file name. Is there a way to escape the period in the filename?
 *  [csx321](https://wordpress.org/support/users/csx321/)
 * (@csx321)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/reusing-code-snippets-w-variables/#post-10597021)
 * I found that the massaging of the values is happening because of the line:
    `
   $attr = array_map('sanitize_title', $attr);` in wp-content/plugins/insert-php/
   includes/shortcodes.php. You can comment that line out, and the custom attribute
   values will not be changed.

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

The topic ‘Reusing code/snippets w/ variables’ is closed to new replies.

 * ![](https://ps.w.org/insert-php/assets/icon-256x256.gif?rev=3475630)
 * [Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts](https://wordpress.org/plugins/insert-php/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/insert-php/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/insert-php/)
 * [Active Topics](https://wordpress.org/support/plugin/insert-php/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/insert-php/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/insert-php/reviews/)

 * 7 replies
 * 4 participants
 * Last reply from: [csx321](https://wordpress.org/support/users/csx321/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/reusing-code-snippets-w-variables/#post-10597021)
 * Status: resolved