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

settings_fields() doesn’t work for other roles except admin

To change the capability required to save settings you can use the option_page_capability_{$option_page} filter, where {$option_page} is the value passed to settings_field() on your settings page. So if you have this on your settings page: <?php settings_fields( ‘wpse_294802_options’ ); ?> You can change the required capability to save with the option_page_capability_wpse_294802_options filter: function wpse_294802_option_page_capability() { … Read more

Is it possible to enqueue CSS files from plugin before theme’s CSS files?

It’s easy to add your plugin stylesheet after theme stylesheet. If you’re sure that your theme styles and plugin styles have the same selector weight (theme has this style .site-header { background-color: #ccc; } and your plugin has this .site-header { background-color: #f1f1f1; }) then enqueuing plugin stylesheet after theme stylesheet will work. If you’re … Read more

Is it safe to call do_action and apply_filters for built-in hooks?

I think there is a difference here between do_action and apply_filters. do_action usually triggers a global state change, either output, enqueuing of resource, initialization of modules, and such. This, like anything which infuance global state can result in surprising side effect when used out of context. apply_filters OTOH behaves much more like a function call, … Read more

Add custom element ID depending where the widget place is, sidebar or footer

Solved it by following the advice from @mmm. Using $args[“id”] did the trick. Using $args[“id”] will return the following: post-sidebar footer-first With a little change, my final code is: public function widget( $args, $instance ) { extract($args); // … <div id=’instafeed-<?php echo explode(“-“, $args[“id”])[0] ;?>’ class=”c-widget-instagram”></div> <?php echo $after_widget; } The result is: <div id=’instafeed-post’ … Read more

Custom plugin – $post_id in wp_head

Use get_queried_object_id() to get the post ID of the current page/post when viewing a single page or post, regardless of whether or not you’re in the loop, or what the global $post variable happens to be at that moment: if ( is_page() || is_single() ) { $post_id = get_queried_object_id(); $meta_alternate = get_post_meta( $post_id, ‘extra_meta_alternate’, true … Read more