Community Forums › Forums › Archived Forums › General Discussion › How do I use Genesis functions in a plugin?
- This topic has 5 replies, 3 voices, and was last updated 8 years, 2 months ago by
Carlo.
-
AuthorPosts
-
December 13, 2014 at 1:50 pm #134472
dev
ParticipantI'm writing my first plugin and I keep getting these errors upon activation:
PHP Fatal error: Call to undefined function genesis_register_layout() in ...
and
PHP Fatal error: Call to undefined function genesis_register_sidebar() in ...How do I use the Genesis functions in a plugin? Am I missing some kind of init code or pointer or something?
Thanks.
December 14, 2014 at 12:13 am #134488Brad Dalton
ParticipantDecember 15, 2014 at 11:49 am #134604dev
ParticipantThanks Brad. As always you are a fountain of great info. I took a look at it on github this morning and it is somewhat complex for the first-time writing a plugin.
I finally finished my plugin. It required that I use the genesis_register_layout() function. I really thought that because Genesis was loaded that WP would 'find' the function "by magic." Well, it didn't. So what I did was go into the Genesis code and copied the function and pasted the code into my plugin.php file and it worked fine.
So my question stands. What is the 'pro' way to incorporate Genesis hooks and filters, etc., in a plugin.
(And for anyone who needs to load a custom template file and you can't keep it in the theme folder (I'm using Dynamik which will wipe away anything upon update) I found some code to keep it in the plugin folder:
add_post_type_support( 'book', 'genesis-layouts' ); add_filter( "single_template", "ac_get_book_post_type_template" ) ; function ac_get_book_post_type_template($single_template) { global $post; $xdir = plugin_dir_path( __FILE__ ); if ($post->post_type == 'book') { $single_template =$xdir .'single-book.php'; } return $single_template; }
December 16, 2014 at 12:59 am #134652Carlo
MemberSo my question stands. What is the ‘pro’ way to incorporate Genesis hooks and filters, etc., in a plugin.
Great question. I recently encountered this problem myself.
To be clear, we actually can use Genesis hooks and filters in a plugin, because the way actions and filters work is that the code doesn't get run until later on.
What we can't use is Genesis functions, because plugin code is run before Genesis functions are defined.
The solution is to wrap your function call in one of the appropriate actions or filters. The action
after_setup_theme
worked for me.example:
add_action( 'after_setup_theme', function() { genesis_register_layout(); } );
That way the code does not get run until after Genesis is setup.
December 16, 2014 at 1:19 am #134653dev
Participant@Carlo
That's a great solution. What I didn't know is that Gen is loaded after plugin code. I would not have expected that... and I wonder why it is set up that way.I looked for over an hour for a work-around like you posted and didn't find one... and was really surprised. I'll give it a try next time I need a Gen function.
I like the Boilerplate Plugin. But there are zero docs to this new version and I'm not sure where to put my code... should I go an add functions to the main class or where? Or ust put it after run_plugin_name(); in the plugin-name.php file.
I'm probably in the minority here, but I think using object oriented methodology for a short plugin is sort of overkill and simply adds to the complexity of the project. However, for a large plugin like WooCommerce, I can see the advantages.
March 2, 2015 at 2:03 am #142794Carlo
MemberI recently published a tutorial on this topic:
http://carlomanf.id.au/genesis-functions-plugin/
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.