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

wp.media update options and force render on uploader

Solved! Not sure if this is the correct method, but after hours of console logging I discovered that this code: acf.media.content.get().options.selection.multiple = false will update the multiple option and therefore change how many images can be selected in the new uploader. If someone finds a nicer way, I’d love to hear it

How to implement color picker from wordpress in my plugin?

Yes, there is: https://make.wordpress.org/core/2012/11/30/new-color-picker-in-wp-3-5/ 1. You should enqueue scripts and styles… add_action( ‘admin_enqueue_scripts’, ‘mw_enqueue_color_picker’ ); function mw_enqueue_color_picker( $hook_suffix ) { // first check that $hook_suffix is appropriate for your admin page wp_enqueue_style( ‘wp-color-picker’ ); wp_enqueue_script( ‘my-script-handle’, plugins_url(‘my-script.js’, __FILE__ ), array( ‘wp-color-picker’ ), false, true ); } 2. … add an input… <input type=”text” value=”#bada55″ class=”my-color-field” … Read more

Remove profile picture option (and other things) from profile.php (in admin)

We can use the show_avatars option to remove the Profile Picture section. We can visit /wp-admin/options.php to turn it off manually. Or update it with: update_option( ‘show_avatars’, 0 ); Or modify it through a filter: add_filter( ‘option_show_avatars’, ‘__return_false’ ); We could also restrict it to the profile.php page: add_action( ‘load-profile.php’, function() { add_filter( ‘option_show_avatars’, ‘__return_false’ … Read more

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