Getting user roles in plugin files

Figured my comment would actually make a better answer, so here we go. You can hook other methods within the plugin class onto admin_init (for the admin side) within the constructor, like so: class Plugin_Class { public function __construct() { add_action( ‘admin_init’, array( $this, ‘some_other_method’ ) ); } public function some_other_method() { // do something … Read more

Calling custom plugin class methods from a template

You can just instantiate an object from class CustomPlugin like $obj = new CustomPlugin(‘id’,’name’,’desc’); in CustomPluginTemplate.php and call any method from the class within the CustomPluginTemplate.php file as $obj->query_vars(); Before instantiating make sure you class is included.

How to Create database table when Plugin installed/activated, and delete database when Plugin deleted

Use this code instead- class hidemysite_security{ public function __construct() { if (is_admin()) { register_activation_hook(__FILE__, array( $this, ‘activate’)); register_deactivation_hook( __FILE__, array( $this, ‘my_plugin_remove_database’ ) ); } } public function activate() { global $wpdb; $table = $wpdb->prefix . ‘md_things’; $charset = $wpdb->get_charset_collate(); $charset_collate = $wpdb->get_charset_collate(); $sql = “CREATE TABLE $table ( id mediumint(9) NOT NULL AUTO_INCREMENT, time … Read more

How to properly setup an activation hook

You first example looks corrent. There is however a typo, hence class ‘MyPluginAdmin’ not found. You include myplugin-admin.php, but then your question seems to suggest the page holding the class is in fact my-plugin-admin.php (with a hypen). If I correct the typo and run the code there are no warnings. Edit (this works correctly): my-plugin.php: … Read more

restore_current_blog required after switch_to_blog, if I use that in a function?

switch_to_blog changes values of global variables (as you can see here: https://core.trac.wordpress.org/browser/tags/5.0.3/src/wp-includes/ms-blogs.php#L801). It doesn’t matter if you call it in function or not – these variables will get changed. So yes – you always have to call restore_current_blog when you’ve done all you wanted with the switched blog. Otherwise these variables will stay changed (so … Read more

Shortcode Inside Class Not Working

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 … Read more

Override WP Class Private Function

As @jacob suggest, you can add subscriber role into that array using add_filter . add below code in active theme’s functions.php file. add_filter( ‘wf_user_permission_roles’, ‘wf_user_permission_roles_callback’ ) ; function wf_user_permission_roles_callback( $roles ) { $roles[] = ‘subscriber’; return $roles; }

problem with implementing widget via the_content()

Its extremely difficult to remove a filter that’s added as part of an object. You have get the reference to the original object and pass that as part of the function to remove. Luckily the filter was added at an unusual priority, so you will probably be safe removing all filters at priority 100: remove_all_filters( … Read more

Conditional tags inside a class

Your real problem is the god class. You put rendering, sanitation, validation, saving and fetching data, and registration into one class. Separate your tasks to make it it OOP, and let the main class be a very simple front controller that handles the initial registration only. Here is a rough start, illustrating how I would … Read more

Extend a class of a plugin

Woocommerce support sent me the solution of my problem: function unregister_parent_hook() { if ( function_exists( ‘wc_dve’ ) ) { $instance = wc_dve(); remove_action( ‘woocommerce_after_order_notes’, array( $instance, ‘exemption_field’ ) ); } }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)