Adding to a plugin options screen with an add-on


(Mark Senff) #1

Hey everyone,

I’m not sure if this falls under technical support, but it’s not the usual “hey I can’t get this thing to work specifically”, it’s more a question on what’s the best way to approach this…

So I have this plugin in the WP repo that makes something on your site sticky. Works all good, and I created a separate add-on for that, some simple additional functionality.

The only thing I can’t figure out is how to add an option to the main plugin’s options screen. I know I can build in something in the main plugin that has a routine like this:

if (add-on is active) {
   echo 'set something for the addon';
   echo '<input type="checkbox" name="whatever" value="meh">'; 
}

BUT that doesn’t seem right to me, cause now I’m just editing the main plugin and I don’t want to have to do that every time I create a new add-on.

So I’m wondering if there is a way to just add something to the main plugin options screen but from within the add-on code? In other words, can I add something to the options screen of the main plugin without actually touching/editing the main plugin?

Tried Googling but I keep ending up on generic plugin basics… Any help or pointers appreciated!


(Leland Fiegel) #2

Good question. Totally allowed, by the way. “Technical support” is fine, I’d just prefer to shy away from basic usage support (i.e. how do I publish a post?)

I’m by no means a plugin expert, but this seems like the perfect use of an action hook. You’ll have to add the action hook in the main plugin file once, then declare functions in add-ons that hook into that action.

So something like this in the main plugin:

do_action( 'yourplugin_addon_fields' );

Then this in the addon:

function yourplugin_addon_meh() {
   echo 'set something for the addon';
   echo '<input type="checkbox" name="whatever" value="meh">'; 
}
add_action( 'yourplugin_addon_fields', 'yourplugin_addon_meh' );

Not tested. And like I said, I’m not a plugin expert so if anyone sees anything wrong with the above, please feel free to correct it.


(Jeffrey Carandang) #3

Or you could use function_exists or class_exists :slight_smile: Hope this helps!

Cheers!


(Mark Senff) #4

Thanks @leland, I’ll look in that direction. I think the tricky part will be to add something to my options screen with a very specific position. We’ll see!


(Nate Wright) #5

A do_action as @leland recommends is a great way to add a slot for other options. However, depending on how much you expect to continue building out the plugin, you may want to invest in a more extensible architecture for your base plugin’s settings page. An easy way to do this is to define your fields in an array and then pass that array through a filter.

// Your base plugin's settings fields
$fields = array(
	'one' => 'print_field_one',
	'two' => 'print_field_two',
);

$fields = apply_filters( 'yourplugin_fields', $fields );

foreach( $fields as $field ) {
	call_user_func( $field );
}

function print_field_one() {
	echo '<input name="field1">';
}

function print_field_two() {
	echo '<input name="field2">';
}
// Your addon
add_filter( 'yourplugin_fields', 'addon_fields' );
function addon_fields( $fields ) {
	$fields['three'] => 'print_field_three';
}

function print_field_three() {
	echo '<input name="field3">';
}

This provides the added benefit that you can re-arrange settings fields, replace them and do more tailored interactions from your addons.

If you want to see a simple OO library for building settings pages like this, I built Simple Admin Pages.