Completely remove WP_Admin_Bar for specific user roles

Add the following to your functions.php file as detailed here. Disable Admin Bar for everyone: // Disable Admin Bar for everyone if (!function_exists(‘disable_admin_bar’)) { function disable_admin_bar() { // for the admin page remove_action(‘admin_footer’, ‘wp_admin_bar_render’, 1000); // for the front-end remove_action(‘wp_footer’, ‘wp_admin_bar_render’, 1000); // css override for the admin page function remove_admin_bar_style_backend() { echo ‘<style>body.admin-bar #wpcontent, … Read more

WordPress 4.5 deprecated get_currentuserinfo()

The answer lies within the wp_get_current_user() pluggable function. I simply changed the function name get_currentuserinfo() to wp_get_current_uesr() then made sure the returns were not boolean but return $current_user. This seems to be working well, including caching, etc. Hopefully this helps others. if ( ! function_exists( ‘wp_get_current_user’ ) ) : /** * This replacement function will … Read more

Strategies to implement selective loading of plugins

Filter option_active_plugins. You can change the result of get_option() here without actually changing the database. if ( defined( ‘DOING_AJAX’ ) && DOING_AJAX ) add_filter( ‘option_active_plugins’, ‘disable_plugins_temporary’ ); function disable_plugins_temporary( $plugins ) { // unset plugins you don’t need, then return $plugins; } Background wp_get_active_and_valid_plugins() calls get_option( ‘active_plugins’, array() ) to get the active plugins. In … Read more

Add async script

The script_loader_tag filter, added in version 4.1, addresses this issue. To add an async attribute to an enqueued script you can do: /** * Add an aysnc attribute to an enqueued script * * @param string $tag Tag for the enqueued script. * @param string $handle The script’s registered handle. * @return string Script tag … Read more

Accessing plugin settings in gutenberg

The WordPress way to access PHP variables with JavaScript is to use wp_localize_script(). function wpse_enqueue_scripts(){ wp_enqueue_script( ‘wpse’, PATH_TO . ‘script.js’ ); wp_localize_script( ‘wpse’, ‘credentials’, $credentials ); } add_action( ‘wp_enqueue_scripts’, ‘wpse_enqueue_scripts’ ); Then in your JavaScript, you can access the credentials like console.log( credentials );

How can I run AJAX on a button click event?

Start from a working code base.. add_action(‘in_admin_header’, ‘my_ajax_button’); function my_ajax_button() { echo ‘<a href=”#ajaxthing” class=”myajax”>Test</a>’; } add_action(‘admin_head’, ‘my_action_javascript’); function my_action_javascript() { ?> <script type=”text/javascript” > jQuery(document).ready(function($) { $(‘.myajax’).click(function(){ var data = { action: ‘my_action’, whatever: 1234 }; // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php $.post(ajaxurl, data, function(response) … Read more

Optimize shortcode callbacks

There is a very important programming principle: DRY – Don’t repeat yourself. Whenever you realize you are repeating almost the same job try to write an abstraction. For your pad* shortcodes this means: function get_padding( $atts ) { $args = shortcode_atts( array( ‘num’ => 10 ), $atts ); return str_repeat( ‘ ‘, (int) $args[‘num’] ); … Read more

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