Add multiple shipping rates from add_rate function with custom ID

to understand how to store choosen method id or else try using: // Make a shipping rate for each result foreach ($quote->shipping_options as $option) { $rate = array( ‘id’ => ‘my_method_’.$option->id, ‘label’ => $methodNames[$option->name], ‘cost’ => $option->price, ‘meta_data’ => array(), ); // Register the rate $this->add_rate($rate); } in ‘meta_data’ you can store whatever you wantin … Read more

Performance of several get_option() calls

If you check the implementation of get_option() you’ll see that in line 168 it calls wp_cache_get() like so: $value = wp_cache_get( $option, ‘options’ ); This means multiple calls to get_option(“style_settings”) within the same PHP execution will be served from cache. There is no need to re-invent this, as you might break other functionality with it. … Read more

Passing array of strings to a SQL statement in a WordPress plugin

$wpdb->prepare() accepts a single array of arguments as the second parameter, e.g. $wpdb->prepare( “SELECT * FROM table WHERE field1 = %s AND field2 = %d AND field3 = %s”, array( ‘foo-bar’, 123, ‘[email protected]’ ) ) Therefore you can use an array of placeholders with the SQL command (the 1st parameter), like so: // Create an … Read more

Custom rewrite rule serves content, but returns 404 error code

This has been an issue in core for a while, http://core.trac.wordpress.org/ticket/10722. The simplest solution is to just overwrite the headers on ‘template_redirect’. You can replace them as long as you haven’t started any output yet, which you shouldn’t have at this point. Just call status_header( 200 ); No cache headers are also sent when the … Read more

WordPress plugin options and jQuery

Variable can be passed to jQuery or any javascript using wp_localize_script. Example: add_action( ‘template_redirect’, ‘prefix_enqueue_vars’ ); function prefix_enqueue_vars() { $script_vars = array( ‘setting_1’ => get_option( ‘some_option’ ), ‘setting_2’ => get_option( ‘another_option’ ), ); wp_enqueue_script( ‘my_script’, ‘path/to/script.js’, array( ‘jquery’ ), true); wp_localize_script( ‘my_script’, ‘unique_name’, $script_vars ); } How to get the variables in your javascript: jQuery(document).ready(function($) … Read more