• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

How do I use Genesis functions in a plugin?

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › How do I use Genesis functions in a plugin?

This topic is: not resolved

Tagged: create, functions, plugin, write

  • This topic has 5 replies, 3 voices, and was last updated 8 years, 2 months ago by Carlo.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • December 13, 2014 at 1:50 pm #134472
    dev
    Participant

    I'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 #134488
    Brad Dalton
    Participant

    Try this https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate


    Tutorials for StudioPress Themes & WooCommerce.

    December 15, 2014 at 11:49 am #134604
    dev
    Participant

    Thanks 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 #134652
    Carlo
    Member

    So 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.


    Comprehensive, easy to follow Genesis documentation

    December 16, 2014 at 1:19 am #134653
    dev
    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.


    @Brad

    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 #142794
    Carlo
    Member

    I recently published a tutorial on this topic:
    http://carlomanf.id.au/genesis-functions-plugin/


    Comprehensive, easy to follow Genesis documentation

  • Author
    Posts
Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble