dtf1111 2015-10-21 06:29
浏览 82

使用redux框架获取WP插件中的当前用户ID

I'am working on a sorter plugin for WordPress, which have Redux Framework installed to manage the options of every section. The plugin uses AJAX to get the ids of all the sections in the homepage of the website, then passes those values to the plugin main file to process in a function that stores the values in the current user meta. That works well, no problem here. The function looks like this:

add_action( 'wp_ajax_save_sections_ids', 'save_sections_ids_sorting_sections' );      
function save_sections_ids_sorting_sections() {  
    //stuff here...
    $user_ide = get_current_user_id(); //it works because it is inside a hook
    update_user_meta($user_ide, "set-sections", $sections_ids);
    die();       
}

Then I have to get the stored values in user_meta to pass them to the Redux Field, so I wrote other function in the main file of the plugin. The function is this:

function get_the_db_sections_ids() { 
        $user_ide = 1; //This should be get_current_user_id() or something similar, but nothing works, not even globalizing $current_user and using get_currentuserinfo();
        $sections_ids = get_user_meta($user_ide, "set-sections", true);
        $sorter_field = array(
            "section_id" => "basic",
            'id'       => 'homepage-sections',
            'type'     => 'sorter',
            'title'    => 'Control de secciones',
            'desc'     => 'Arrastra y suelta los bloques con los nombres de las secciones, tanto para ordenarlas verticalmente como para desactivarlas o activarlas.',
            'compiler' => 'true',
            'options'  => array(
                'enabled' => array(
                ),
                'disabled'  => $sections_ids
            ),
        );
        return $sorter_field;     

}  

As you notice in the comment in the function above, I have tried several ways, also require_once("/../../../wp-load.php"), but nothing happens. I tried do_action and add_actions, to create a hook, but those also use global variables, and for what I understand, the global variables do not work in functions with no hooks in plugins.

But I havent finished yet. The really tricky part is, I am calling an instance of Redux class inside the Redux config file (sample-config.php for the demo, I have a custom file, but it is the same).

The instance is Redux::setField($opt_name, get_the_db_sections_ids());

The problem this does not print anything if I call it from a function, or the function linked to the AJAX call.

As you can see, the second parameter of the instance is the function I wrote above that, and it works perfectly if I set $user_ide to 1, but I want the data stores in all admins user_meta, in case user 1 is erased, or whatever.

Is there a way to achieve what I want, or to store the data somewhere else and get it from there. I was thinking in creating a custom table in db, and use wpdb to retrieve the data, but I think I can't use wpdb either, because it will be the same problem.

I get the feeling I'm missing something very basic, but I can't get it. Please help.

  • 写回答

2条回答 默认 最新

  • doupu1727 2015-10-21 06:31
    关注

    This should help you out

    global $current_user;
    get_currentuserinfo();
    $user_ide =  $current_user->ID;
    

    You'll have to declare a global variable $current_user, in order to use it.

    评论

报告相同问题?