How to replace settings in WordPress plugin from a theme

You are already pretty close. Extending your example: In your plugin put this: $default_settings = array( ‘key_one’ => 0, ‘key_two’ => 1 ); $settings = apply_filters( ‘example_filter’, $default_settings); Then a theme can change it like this: add_filter(‘example_filter’,’my_function’); function my_function($settings){ $settings[‘key_one’] = 1; return $settings; }

If $var is empty, return 404.

I have run into the same issue that using $wp_query->set_404(); would properly adjust the global object but not return the 404 template. So in the plugin I was writing I used the tried and tested method: add_filter( ‘template_include’, ‘wp_139917_force_404’ ); function wp_139917_force_404(){ global $wp_query; $wp_query->set_404(); status_header(404); include get_404_template(); exit; } ~ modified from source: https://github.com/codearachnid/woocommerce-product-permalink/blob/master/inc/product-permalinks.php#L56 … Read more

Get section of input passed to the sanitize_callback

Unfortunately if you look at the source the only thing that happens there is: add_filter( “sanitize_option_{$option_name}”, $sanitize_callback ); There is no opportunity to pass along or retrieve any extra information. On a larger scale I would say if you need section-specific sanitize then use section-specific callbacks. Even if considerable parts are shared with class based … Read more

Clean way to initialize plugin in newly-added site when plugin has been network activated?

After looking through Make WordPress, it looks like this is a known issue that won’t be fixed due to scalability concerns with large networks. That same issue linked above recommends doing exactly the solution discussed above. Specifically, that would look something like this: public static function 145561_activate_new_blog($blog_id) { if (is_plugin_active_for_network(plugin_basename(__FILE__))) { // do activation for … Read more

How to set the default value as the saved value once a form of widget is saved?

You need a corresponding function UPDATE to complete this. See the Widgets API on the codex: http://codex.wordpress.org/Widgets_API Below is their example. /** * Adds Foo_Widget widget. */ class Foo_Widget extends WP_Widget { /** * Register widget with WordPress. */ function __construct() { parent::__construct( ‘foo_widget’, // Base ID __(‘Widget Title’, ‘text_domain’), // Name array( ‘description’ => … Read more

Extending a theme: build new features as plugin or core modules?

If you’re going to want to mix-and-match these modules, I would suggest developing them as plugins or using a plugin such as Code Snippets to separate your modules, making them easier to work on, expand upon, and deactivate/activate. Either way, separating your modules from your theme will make customizing deployments faster while increasing the organization … Read more