Title: Object Programming in snippets
Last modified: July 9, 2018

---

# Object Programming in snippets

 *  [codeyschoettle](https://wordpress.org/support/users/codeyschoettle/)
 * (@codeyschoettle)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/object-programming-in-snippets/)
 * I’m using object design approach in snippets and am using private variables with
   public functions. I use the old insert php method to make a “new” object of a
   class I created, however, I can’t call any functions from the class I created.
   Any idea how to call functions from classes within the new shortcode?

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

 *  Plugin Author [webcraftic](https://wordpress.org/support/users/webcraftic/)
 * (@webcraftic)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/object-programming-in-snippets/#post-10477774)
 * Hi,
    1. If you want to place a class in a snippet and use it in any shortcode.
   Create a snippet with the class, the class must be global. Set in the snippet
   settings “run everywhere”. [http://joxi.ru/brRzEXgTJL95Yr](http://joxi.ru/brRzEXgTJL95Yr)
 *     ```
       global $wbcr_my_public_class;
   
       class Wbcr_PublicClass {	
         private $product_name = 'book';
   
         public function show_my_method_text() {
           return 'Return My value';
         } 
         public function show_product_name() {
           return $this->product_name;
         }
       }
   
       $wbcr_my_public_class = new Wbcr_PublicClass();
       ```
   
 * 2. Create a snippet with a shortcode and call your class, so
    [http://joxi.ru/ZrJzOLBT9b4qZr](http://joxi.ru/ZrJzOLBT9b4qZr)
 *     ```
       global $wbcr_my_public_class;
   
       if(!empty($wbcr_my_public_class) && $wbcr_my_public_class instanceof Wbcr_PublicClass) {
        	echo $wbcr_my_public_class->show_my_method_text(); 
         	echo $wbcr_my_public_class->show_product_name();    
       }
       ```
   
 * Best regards, Alex
 *  Thread Starter [codeyschoettle](https://wordpress.org/support/users/codeyschoettle/)
 * (@codeyschoettle)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/object-programming-in-snippets/#post-10478385)
 * My goal is to call the function from the webpage itself. I want to make a function
   let’s say called “displayName();” within a snippet. I want to be able to call
   the function by passing POST data into the shortcode found on the page, like “[
   wbcr_php_snippet id=”249” displayName(“name”)]”
 *  Plugin Author [webcraftic](https://wordpress.org/support/users/webcraftic/)
 * (@webcraftic)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/object-programming-in-snippets/#post-10491222)
 * Hi,
    You do not need to pass data through the shortcode, you can get all the 
   post data via the global variable $_POST inside the snippet.
 * This code prints all the data sent by the post method on the page
 *     ```
       $post = $_POST;
       print_r($post);
       ```
   
 * 1. Create a snippet with a function, this snippet should be run everywhere
 *     ```
       function displayName($name) {
         echo $name;
       }
       ```
   
 * 2. Create a second snippet shortcode
 *     ```
       //$attr_name - the value of the variable is passed from the shortcode
       displayName($attr_name);
       ```
   
 * Call the snippet using the shortcode, passing the value inside the function. 
   You can pass data through a shortcode, so
    `[wbcr_php_snippet id="249" attr_name
   ="name"]`
 * Best regards, Alex

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

The topic ‘Object Programming in snippets’ is closed to new replies.

 * ![](https://ps.w.org/insert-php/assets/icon-256x256.gif?rev=3523853)
 * [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/)

 * 3 replies
 * 2 participants
 * Last reply from: [webcraftic](https://wordpress.org/support/users/webcraftic/)
 * Last activity: [7 years, 11 months ago](https://wordpress.org/support/topic/object-programming-in-snippets/#post-10491222)
 * Status: not resolved