Here is a basic class which you can create once:
if ( ! class_exists('my_class') ) :
class my_class {
public $my_var;
// This variable is for a single instance
static $instance;
function __construct () {
// Call other methods like this
$this->initialize();
}
function initialize () {
// Populate your variables here
$this->my_var="any value you want";
}
static function get_instance () {
// Static variables are persistant
// Notice the 'self::' prefix to access them
if ( empty(self::$instance) )
self::$instance = new my_class();
return self::$instance;
}
}
endif;
Now in your template files you can access the class like this:
<?php $var = my_class::get_instance()->my_var; ?>
Related Posts:
- Are WordPress Heartbeat API ‘beats’ staggered or do they occur simultaneously for all users?
- Checking for new message using AJAX and PHP. Server overload?
- Limit Search Queries per IP per Day
- Automatically load WordPress page on server every XX hours
- WordPress article with 50 000 words loading slow – 100% cpu
- What is the correct way to use WordPress functions outside WordPress files?
- dbDelta support for FOREIGN KEY
- How can I save a multiple select array with the settings API for a plug-in options page?
- WP-CLI – Selecting PHP version
- Inject post (from specific category) between posts in Loop
- Place page title in header?
- Shortcode to insert
- Gutenberg Blocks doesn’t render correctly when using do_blocks
- WordPress new user / forgot password emails are broken
- Initialize WordPress environment to use in a real cron script
- How to get year, month and hour in WordPress?
- class=”parent” for wp_list_pages?
- PHP Script within wordpress theme
- Conditional statement for parent, child and grandchild pages
- Why use %d in php when in function
- How to display product cropped thumbnail (150×150) for WooCommerce product categories
- Add to checkout total lines an additional line below shipping in WooCommerce
- wp_embed_register_handler to embed html files
- When is is_admin() available?
- Creating new menu item
- Processing a subscription form with POST method?
- Get posts with at least one category in common with current post?
- How do i use this jquery script on a wordpress site?
- Variable global scope [closed]
- If post ID has_term?
- Apache /Ubuntu server not running WordPress installation, outputting PHP code like HTML
- How to give new users two specific user role options upon WordPress user registration
- Add custom url to featured image
- How to add date to navigation bar
- Filter Select results based on selection
- creating a second image attachment template?
- add element with add_action to posts from a specific category ID
- developing a wordpress plugin, have a few PHP Woocommerce related coding questions
- How to pick the default selected value in wordpress dropdown?
- How to target grandchild of post_parent using wp_list_pages
- remove post that has no content
- How to show only subcategories in parent category not parent category?
- Get post id within comments loop
- Woocommerce change prices for a certain country [closed]
- Find a way to retrive data updated through metabox plugin to web page
- WordPress Environment: Dynamic Page using shortcode – how to change the page name for sharing
- Should i use the wordpress Options table or to create database table..?
- Where are the src and srcset sizes coming from?
- wp_remote_post empty $_POST
- How can I spin up a new website for a registered user automatically?
- How to show every second user different types of banners?
- Woocommerce – get_items() returns empty array
- Need help with pagination
- get_comments_number() returns 0
- Using wordpress function to retrieve data
- Creating a child theme after numerous edits to parent theme
- Adding Additional Variables on Menus Page
- Querying for specific tags
- Why the dynamic_sidebar() call is putted into an ul tag?
- How to correctly escape an echo
- Timber right sidebar in WordPress theme
- Build A Custom SQL Query for WordPress Search
- Get main parent category for post (WordPress/Advanced Custom Fields)
- How to run complex query using PHP
- Store ajax data in PHP variable
- comment_post (if comment is approved OR $comment_approved === 1) not working?
- Configure WordPress to Use Responsive Image Rendering
- How to edit post meta data before publishing the post it self wordpress?
- How can i create a function tag in my plugin
- Why the responseText containing those two arrow signs at the beginning
- Call jQuery function from PHP?
- How to Get Linked Elements for a specific ml_source_elementid
- remove_action() not working in page template – Genesis
- Adding action item to admin users table and sending email
- Metabox – Displaying the Value of a Metabox
- Using get_the_excerpt in edit-post
- ajax form with multiple submit buttons and values
- Best way to define a database with product codes and back-end support?
- WordPress – custom navigation item classes
- Is there a way to randomize and connect a background and header image?
- Post Thumbnail on Single (if elseif else)
- Display pages from specific page template
- dynamically filter by category via sub-menu
- Sidebar doesnt get updated?
- Edit the Publish Widget Options
- How can I print out a single stylesheet or javascript link?
- WordPress developer
- If the_content’s string length is greater than 0, show post otherwise dont
- WordPress post pagination on custom template not working
- get_option(‘admin_email’) not working in wordpress when using ajax call
- wordpress featured image
- How do I update product shipping using PHP in WooCommerce?
- How can I edit the WooCommerce Mini DropDown Cart to read as follows
- wp_enqueue_scripts with JS script as a string
- Why can’t I enter the wordpress admin interface?
- base64_encode conflict with convert_smilies in wordpress
- Disable Typed Parameters for JavaScript in PhpStorm? [closed]
- How to add Paragraph Tags in WordPress using Visual Composer?
- PHP – Multiple variables in insert post array
- Search custom post type posts only by meta fields?