Categories
Uncategorised

Use Genesis Functions in a Plugin

Are you an advanced user of WordPress + Genesis and trying to use Genesis functions in a plugin?

Usually, you put your custom code for Genesis into your theme functions, but there are arguments for creating a custom plugin instead.

However, complications can arise from using Genesis code in a plugin. This is because plugin code is run before Genesis core code is run.

Any code that uses Genesis hooks (i.e. add_action or add_filter), like the below example, is OK to go in a plugin.

<?php
//* Plugin Name: My Custom Plugin for WordPress + Genesis

add_filter( 'genesis_search_text', 'cm_search_input_text' );
function cm_search_input_text( $text ) {
	return esc_attr( 'Search my website' );
}

This is because the way that hooks work is that the code doesn’t get run until later on, when Genesis has been setup.

What you can’t do in a plugin is call up a Genesis function without a hook, because your code will be run immediately upon your plugin being processed, which is before Genesis has been setup.

<?php
//* Plugin Name: My Custom Plugin for WordPress + Genesis

add_theme_support( 'genesis-footer-widgets', 3 );

The solution is to wrap your function call in one of the appropriate actions or filters. The action after_setup_theme works well.

Example:

<?php
//* Plugin Name: My Custom Plugin for WordPress + Genesis

add_action( 'after_setup_theme', function() {
	add_theme_support( 'genesis-footer-widgets', 3 );
} );

That way your code won’t be run immediately, but will be saved until after Genesis is setup.

Still having trouble?

Contact me and I’ll be glad to help you out.