if you use any hooks, filters or shortcodes with class methods, you have three options.
either declare it in the context of an instance, in which case, you can refer to the instance of the class.
class myClass {
function __construct()
{
add_shortcode('my_shortcode_name', [ $this, 'shortcode' ]);
}
function shortcode()
{
//your code here
//properties of the instance are available
return 'a string';
}
}
or you declare it outside of an instance, then you must use a public static method.
add_shortcode('my_shortcode_name', [myClass::class, 'shortcode']);
class myClass {
public static function shortcode()
{
//your code here
//you only have access to static properties
return 'a string';
}
}
Related Posts:
- PHP Helper Class to create shortcodes
- How to manually fix the WordPress gallery code using PHP in functions.php?
- add_action in namespace not working
- Encode text string being appended as query to URL [closed]
- Display a text message if the shortcode is not found?
- How to return a string that has a variable inside in a shortcode?
- Run shortcode at certain resolution
- Passing a shortcode attribute to a sub-function
- post meta value as shortcode parameter
- How to protect own PHP code from WordPress updates
- the_title() & the_content() are retrieving Post details inside a Page, after calling a shortcode
- Loading page template into shortcode
- How to add Shortcode (font awesome) in widget title?
- How to do conditional publishing?
- Placing the_content inside shortcode not working
- Autogenerate shortcodes from an array of strings
- How to Insert Shortcodes into Theme?
- Template part inside shortcode, unexpected reult
- Shortcode API: How to get name that matched shortcode RegEx?
- Shortcode content filter?
- Custom Theme, Custom shortcode not working
- Why is file_get_contents returning page source?
- Plugin CSS not enqueing
- Showing content from one page on another
- How to display posts by current user in a drop down
- Create own WordPress shortcode gallery
- Get value from shortcode to do something
- How to prevent XSS alter custom global javascript object & methods in WordPress
- Colon is Missing In My Website Url in WordPress
- Custom shortcode outputs plain text instead of HTML at top of post
- How to use wp_add_inline_style for custom CSS added via shortcode?
- wordpress 4.4 self hosted video (html5) doesn’t show
- Shortcode returns values in the wrong order
- Embedding PHP in shortcode $content
- Shortcode with PHP issue “Undefined index”
- Excecuting php function in shortcode
- Returning data instead of echoing/printing
- get_posts works in the page template but not in a shortcode
- ::before on open/close function [closed]
- Is There A Way To Make Theme Files Accept Shortcodes?
- Is it possible to define variables in a wordpress shortcode, and then call the shortcode using a specific variable?
- Create a WordPress shortcode using PHP [duplicate]
- do_action(), iFrame Gets Displayed Despite Password Protection [closed]
- Using Shortcode to Grab Archive Listing, Separate by Year
- Unable to process php via shortcode
- Help with WordPress function inside a shortcode
- calling a custom field value inside functions.php
- Using ob_start and ob_get_clean with wordpress shortcode
- WordPress shortcode returns the data before
- List all blogs, but exclude the main site
- Cant display an image via PHP in wordpress
- Check class_exists before class definition / Doxygen problem
- PHP code inside Shortcode
- Action Hook Inside WordPress Plugin Shortcode
- How can I include the user id inside of a shortcode php output?
- help with custom shortcode that stopped working
- Shortcode for a Series of Elevating Image Files, Excluding a Range?
- Shortcode won’t execute
- how to get sentence values from wordpress shortcode parameter?
- OOP PHP class extends TwentyTwenty_Customize in Child Theme
- Updating Metadata with Shortcode
- Create shortcode for metabox gallery
- Use PHP Class in WordPress functions
- couldnt show short code atts in category
- Time Stamp In A WordPress Post Title That Does Not Keep Refreshing
- can I extend the WP_Query class to deal with ‘duplicate’ posts created by joining to wp_posts?
- How can I edit the final HTML structure of the whole web page
- I am having an issue with this shortcode plugin.. Warning: Illegal string offset ‘title’
- Advanced Custom Forms PHP formatting for do_shortcode
- overwrite wordpress gallery with custom gallery shortcode
- How can I create a shortcode from an html and php code written together
- Multiple Arrays for Custom Post Query using Boostrap accordions and ACF repeater fields
- CPT in a shortcode
- PHP/CSS: Shortcode won’t display correctly, and only displays in the head (before content)
- Open/closed function [closed]
- What is wrong with this shortcode? to include external php file in WordPress posts
- Need to Echo A Url path to show on a wordpress page
- WordPress Shortcode based on other shortcode
- Force ‘permanent’ post cache of shortcode results
- Wrapping shortcode content in a span or link
- Formatting Shortcode to Display in Visual Mode
- Use html tags in shortcode_atts, is it real?
- What is wrong with assigning values to a WP_List_Table class?
- How to pass parameter that ends up being part of a class name with wordpress shortcode
- calling a function from a class in your template
- How to have Function of a plugin using global vars into a shortcode?
- Overwriting a Shortcode
- How can I put a custom field as the link of a button shortcode?
- Where to place PHP for shortcodes
- How to wrap a text with shortcode into php file?
- PHP Widget and do_shortcode
- Problem using explode inside add_shortcode() callback funciton [closed]
- Shortcode not passing variable to included file
- A next page function with shortcode?
- using a shortcode in a hyperlink
- $_html is empty when var dumped
- WP Custom tables query
- Display total count of products in orders of a specific order status
- HTTP 500 Error after Creating Shortcode
- How to edit a widget code?