Okay
  Public Ticket #2658109
awareness of logged in user
Closed

Comments

  •  1
    Brian Shaughnessy started the conversation

    Short explanation --

    I'm building a custom data source which I intended to do via either json or php and then feed into a table. When I access my endpoint directly it is aware of the logged in user. But when the endpoint is accessed through the datatable (i.e. I view the page that has the table shortcode), there is no awareness of the logged in WP user. Consequently I can't filter my data properly. I read that it's possible to filter by the logged in user when using the MySQL data source, but my preference is to handle this with a script that lives in my website.

    Is there any way to enable logged-in-user awareness? I'm using the lite version for now. If this is a feature available in the premium version, please let me know.

  •  2,572
    Aleksandar replied

    Hello Brian

    Thank you for your interest in wpDataTables.

    "Users see and edit only own data" is a feature which is available only for editable tables, and that is only available in the full version of the plugin.

    However, if you have a User ID column, you can use our placeholder %CURRENT_USER_ID% as a predefined filtering value for that column, and the table will always be filtered per ID of a currently logged-in user. Since your users would be able to modify the filtering value, my advice would be to enable "Filters in a form" in Sorting & Filtering tab above the table and then hide that filter with custom CSS. I can help you with hiding that filter once you have the page up.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  •  1
    Brian Shaughnessy replied

    Ok -- I was aware of the option to restrict to the user when using MySQL as the datasource, but I'd rather not use that option. I don't think I was clear enough in my original ticket -- I am seeking to either use "JSON file" or "Serialized PHP array" as the data source. In either case, the end point is a link within my site.

    The problem is that when the endpoint is hit, it has no awareness of the logged in user. I suspect you're using wget or file_get_contents to retrieve the data from the endpoint URL and so it is basically accessed anonymously.

    Is there anyway to build the URL to the endpoint and pass the User ID? Will the placeholder you referenced work in that way?

  •  1
    Brian Shaughnessy replied

    I tried using that placeholder but it only seems to be used with the MySQL data source.

    If the json/php url data source is not inherently aware of the logged in user, what I'm basically looking for is something similar to the MySQL placeholder -- a variable that I can include with the endpoint URL to pass the logged in user's ID so that I can return data filtered to the user.

  • michael brians replied

    I'm having this same problem. I tried using get_current_user_id() and it always returns 0

    When I access the endpoint from my browser while logged in, I get the correct user ID. When wp data tables access the endpoint using the PHP serialized array option, the user ID is always 0.

    So as a work around I added the  %CURRENT_USER_ID% placeholder to the URL that I entered in the 'Input file path or URL' field.

    So it looks like this now 

    https://domain.com/folder/custom-php-file.php?user_id=%CURRENT_USER_ID% 

    My php script looks like this. 

    <?php
    // Initializing the WordPress engine in a stanalone PHP file
    require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php';
    header("HTTP/1.1 200 OK"); // Forcing the 200 OK header as WP can return 404 otherwise
    function wpdt_get_the_user_id()
    {
        if (isset($_GET['user_id']))
        {
            $current_user = $_GET['user_id'];
          
            //You now have the current user id 
        }
    }
    

    I know this way is unsafe and I would prefer to just use get_current_user_id() instead. 

    Can anybody help me get the user id without using the  %CURRENT_USER_ID% placeholder in the URL?

  •  2,572
    Aleksandar replied

    Hello Brian and Michael.

    Thanks for the code, I'm sure it'll be of use to others as well.

    The plugin can be filtered with the placeholder if there's a column with the Logged-in user's ID. So, basically you can add %CURRENT_USER_ID% in predefined filtering value in the User ID column, and the table will be filtered with it.

    I forwarded your question to one of our developers, and as soon as I receive a response from him I will let you know.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  •  2,572
    Aleksandar replied

    Hello again guys.

    There is a hook available in the Premium version, but not in Lite:

    You need to add one more hook to filter that URL of PHP file.

    In file wp-content/plugins/wpdatatables/source/class.wpdatatable.php: around line 1875

    you will find this

    case 'serialized':

    and under it add this

    $url =  apply_filters('wpdatatables_filter_url_php_array', $tableData->content, $this->_wpId);

    so it looks like this

    7837078816.png

    In hook you will do something like this

    function test ($url,$id) {
    // all data from current user
    // you can fetch only what you need
    $userData = wp_get_current_user()->data;
       if(isset($userData)){
           $url .= '?' . http_build_query($userData);
        }
    return $url;
    }
    add_filter('wpdatatables_filter_url_php_array', 'test', 2 , 10

    In the PHP file, you will use $_GET method to take the values from the URL which is filtered with the hook

    // you can check then for ID or user_login or user_pass or user_nicename or user_email ...
    if(isset($_GET['user_login'])){
          //do something
    }

    I try to be detail as I can, so you can do this very easily.

    Let me know is it working as you need

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • michael brians replied

    Ok this worked great for me. I did not need to add the extra code to wp-content/plugins/wpdatatables/source/class.wpdatatable.php

    The code was already added at line 3014

    $url =  apply_filters('wpdatatables_filter_url_php_array', WDTTools::applyPlaceholders($tableData->content), $this->_wpId);
    

    Thanks for the help

  •  2,572
    Aleksandar replied

    Great news, Michael.

    Thank you for letting me know.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables