reduxframework / redux-framework

Redux is a simple, truly extensible options framework for WordPress themes and plugins!
http://redux.io
Other
1.74k stars 583 forks source link

Can't use custom taxonomies when building sections via filter #1191

Closed derekashauer closed 10 years ago

derekashauer commented 10 years ago

Custom taxonomies need to be set in an "init" action. Redux appears to run before "init". So the custom taxonomies are not yet set and thus not available to me to help build sections.

In short, this code is trying to create a text field for each "sunshine-product-price-level" taxonomy. I get "Invalid taxonomy" errors from the get_terms() call.

add_filter('redux/options/sunshine_options/sections', 'sunshine_digital_downloads_options');
function sunshine_digital_downloads_options($sections) {

    $fields = array();
    $fields[] = array(
        'id'        => 'price-level-header',
        'type'      => 'section',
        'title'     => __('Gallery Download Pricing','sunshine'),
        'subtitle'      => __('Cost for digital download of an entire gallery for each price level','sunshine')
    );
    $price_levels = get_terms('sunshine-product-price-level', array('hide_empty' => false));
    foreach ($price_levels as $price_level) {
        $fields[] = array(
            'id'        => 'gallery_download_price_'.$price_level->term_id,
            'type'      => 'text',
            'title'     => $price_level->name,
        );
    }

    $sections[] = array(
        'icon'      => 'el-icon-download',
        'title'     => __('Digital Downloads', 'sunshine'),
        'fields'    => $fields
    );
    return $sections;
}
dovy commented 10 years ago

Remove the init. You can run custom taxonomies anywhere. It's just suggest to run on Init. You won't get blocked running them earlier. Redux runs before init, that's why you can access your variables inside functions.php. ;)

derekashauer commented 10 years ago

Trying to do it outside of an init hook and it causes even more errors. Everything I read says you must register post types and taxonomies in the init.

derekashauer commented 10 years ago

http://screencast.com/t/5zejNRkML - Nacin, lead WordPress developer. Doesn't get much more official than that. (from https://core.trac.wordpress.org/ticket/15568)

I don't understand the need for Redux to run before init. So you can get a simple global variable set with the options? A theme/plugin developer can simply use get_option() and define it themselves whenever they need and Redux becomes more flexible to work with custom post types and taxonomies.

WordPress gives errors if you try to use it before init. So... ya.

dovy commented 10 years ago

Umm, not true... What type of errors do you get? I use it elsewhere all the time...

derekashauer commented 10 years ago

I created a test plugin, here is the entire thing:

<?php
/*
Plugin Name: CTP Test
Description: CPT test 
Version: 1.0
Author: Sunshine Photo Cart
Author URI: http://www.sunshinephotocart.com
*/

$labels = array(
    'name' => _x('Galleries', 'post type general name'),
    'singular_name' => _x('Gallery', 'post type singular name'),
    'add_new' => _x('Add New', 'gallery'),
    'add_new_item' => __('Add New Gallery'),
    'edit_item' => __('Edit Gallery'),
    'new_item' => __('New Gallery'),
    'all_items' => __('All Galleries'),
    'view_item' => __('View Gallery'),
    'search_items' => __('Search Galleries'),
    'not_found' =>  __('No galleries found'),
    'not_found_in_trash' => __('No galleries found in trash'), 
    'parent_item_colon' => '',
    'menu_name' => __('Galleries')
);
$args = array(
    'labels' => $labels,
    'public' => true,
    'exclude_from_search' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_nav_menus' => false,
    'show_in_menu' => false, 
    'query_var' => true,
    'capability_type' => 'post',
    'has_archive' => false, 
    'hierarchical' => true,
    'map_meta_cap' => true,
    'supports' => array( 'title', 'editor', 'page-attributes', 'thumbnail' )
); 
register_post_type('sunshine-gallery',$args);
?>

As a result, I get an error:

[19-May-2014 14:46:26] PHP Fatal error: Call to a member function add_rewrite_tag() on a non-object in /Users/derekashauer/Dropbox/sunshine/sunshinepro.dev/wp-includes/rewrite.php on line 51

In researching this error, every single post I find says the solution is to put the register_post_type on an init action. How are you registering post types and taxonomies outside init without errors?

dovy commented 10 years ago

That's probably because you're running your plugin code strait up. I wrap my plugin code in before_setup_theme.

Give that a try.

derekashauer commented 10 years ago

That action doesn't even exist, nor does before_theme_setup (I know I sometimes get hooks backwards) - http://codex.wordpress.org/Plugin_API/Action_Reference

dovy commented 10 years ago

Excuse me, just setup_theme. ;)

derekashauer commented 10 years ago

I realized after I posted that was the one you likely mentioned. The docs say that action is for loading theme settings, great. Redux should be using that to load the settings. I don't see why it loads everything at that time - that part makes no sense to me. Everything else should be loading on init so things like custom post types and taxonomies which WordPress docs and lead developers clearly state are supposed to go. I feel like Redux should follow WordPress rules and not make developers violate those rules. Violating those rules means something else might break down the road.

dovy commented 10 years ago

Haha, dude, dude. We're not violating rules. There are tons of WordPress themes on WP.org based on Redux. If we were violating rules we wouldn't get in.

My issue is the WordPress data is not being called until admin_init which runs AFTER init. Again, our WordPress data is not grabbed until AFTER init is run. So right now yours is a special case. No one out there is reporting a big concern.

How about you work with us to resolve issues instead of just saying we're violating standards?

I'll give your code a try, but I haven't seemed to have a problem before.

kprovance commented 10 years ago

Not to mention Envato. Just sayin', is all.

derekashauer commented 10 years ago

I am trying to work with you guys to find a solution, I first need to understand why things are done they way they are to work through this with you. I just don't understand why all of Redux is loaded before init when every bit of documentation would indicate that is the best time to do so.

I moved my custom post type and custom taxonomies to a setup_theme action. I am still getting the exact same error as before - invalid taxonomy.

The following code are pertinent snippets from throughout my plugin - quite a bit has been excluded because they are not necessary:

add_action('setup_theme', array($this, 'setup_theme'), 5);
function setup_theme() {
    $this->post_types();
    ...
}
private function post_types() {
    ...
    $labels = array(
            'name'             => __( 'Price Level', 'sunshine'),
            'singular_name'    => __( 'Price Level', 'sunshine'),
            'search_items'     =>  __( 'Search Price Levels', 'sunshine'),
            'all_items'        => __( 'All Price Levels', 'sunshine'),
            'parent_item'      => __( 'Parent Price Level', 'sunshine'),
            'parent_item_colon'=> __( 'Parent Price Level:', 'sunshine'),
            'edit_item'        => __( 'Edit Price Level', 'sunshine'),
            'update_item'      => __( 'Update Price Level', 'sunshine'),
            'add_new_item'     => __( 'Add New Price Level', 'sunshine'),
            'new_item_name'    => __( 'New Price Level', 'sunshine')
        );
    $args = array(
            'label' => 'Price Level',
            'labels' => $labels,
            'public' => false,
            'hierarchical' => false,
            'show_ui'  => false,
            'query_var'=> true,
            'show_in_nav_menus' => false
        );
    register_taxonomy('sunshine-product-price-level', 'sunshine-product',$args);
    ...
}
add_filter('redux/options/sunshine_options/sections', 'sunshine_digital_downloads_options');
function sunshine_digital_downloads_options($sections) {
...
    $price_levels = get_terms('sunshine-product-price-level', array('hide_empty' => false));
    if (is_wp_error($price_levels)) {
        wp_die($price_levels->get_error_message());
    }
...
}

This results in an "Invalid taxonomy" error.

derekashauer commented 10 years ago

I also moved the last piece of code that uses get_terms into the initial setup of the $sections variable, instead of the filter, and still got the same error.

dovy commented 10 years ago

PARTS of Redux load when you load it. The main reason is so you can access the global variable from within functions.php. Delaying everything made the data inaccessible until the init hook. That's why.

I'll tinker with this later this week but again it is a special issue you're facing and I am unsure why it's happening.

dovy commented 10 years ago

Well, a quick fix for this is not to load your config until after you'e created the custom post type. So hook your redux code in there after you've declared the taxonomy and it should work.

derekashauer commented 10 years ago
add_action('setup_theme', 'sunshine_load_redux', 10);
function sunshine_load_redux() {
    if (is_admin())
        require_once('admin/sunshine-options.php');
}

Results in "You do not have sufficient permissions to access this page" errors. So trying to do that doesn't appear to work (remember, I have the loading of my post types at level 5 for the setup_theme action, so "sunshine_load_redux" is loading after the post types have been created)

dovy commented 10 years ago

How about running that on it's own init hook and wrapping the config in admin_init, 0?

dovy commented 10 years ago

Sheesh dude. I am unsure. That's some complex inner workings WordPress stuff, hah. I'll try later this week.

elcrespo commented 8 years ago

¿Sorry for write in an old post, but I'm having the same problem and I haven't found more info. did You find the solution? (sorry about my English)

ghost commented 8 years ago

Hi, I've the same problem too: "Invalid taxonomy". Did you find the solution? Thank you