I am currently stuck on a little problem.

I'm using a localStored value to differentiate the content that user sees depending on their choice.

<div class="form-group form-inline hidden-print">
 <div id="year_selection_area " class="controls text-center">
  <select class="form-control " name="Legislation year" id="dueYear" >
   <OPTION VALUE="2015">2015</OPTION>
   <OPTION VALUE="2014">2014</OPTION>

<script type="text/javascript">
 jQuery(document).ready(function($) {
  $( "#dueYear" ).change(function() {
   var selectedValue = $("#dueYear").val();
   localStorage['myKey'] = selectedValue;

My issue is on the admin side. To display exact content being edited i need to add a style sheet that only fits the current selection.

add_editor_style('style-2016.css' );

But my issue is that i cant use localStorage['myKey'] in functions.php. Also i would need to change the value being used on select box change.

Any suggestions would be appreciated.

  • Why would you use LocalStorge in the first place? Remember that LocalStorage doesn't send back any value to the server so LocalStorage might not be the best option here. There is at least 5 other ways I can think of doing this without LocalStorage use, but I would need to get more context to give you the best answer. Do you have any specific requirement/specifications/limitations that need to be taken into consideration? What is your select box used for exactly? It seems to be modifying the TinyMCE depending on select choice but how often does the user need to change that option? – bynicolas Sep 12 '16 at 14:59
  • User can change which version of the content he wishes to see (choosing 2014 will not show content removed before 2014 and will not show any content added after 2014, it will also highlight content added and removed in 2014). Having it stored in local session user going to any other page (over 25000 of them on the site) will see the same year. All different years are stored in the same file for simaltanious editing. Select box is currently not affecting TinyMce. And final part user can change their choise upto 10 times per page without a need to reload. – Artem Ankudovich Sep 12 '16 at 15:06
  • Then if you have logged in users why not save the option to the DB to make it persistent over page change? This will even remember on next login. – bynicolas Sep 12 '16 at 15:12
  • We found over the years users prefer starting at current year as each page contains legal acts meaning new day most likely means new client. – Artem Ankudovich Sep 12 '16 at 15:29
  • Ok, but if you need to access a localStorage parameter in your PHP script. It just isn't possible by design. So keeping track of a persistent setting in the DB would still be a better option. You could still reset the filter to the current year on each new logon (and each select box change) to satisfy your users preference. Better yet, you could even add some option to a setting page/profile page so each user could chose is/her preference. – bynicolas Sep 12 '16 at 15:43

To store the user defined value into the DB you can use the update_user_meta() function. And retrieve back the info with get_user_meta(). You could update the the user meta key with an AJAX call on your select change or put your select in a form and update on form submit either with $_GET or $_POST method. Your choice here would influence if you need to build a function that listens on a $_POST, $_GET or AJAX request to update your user metadata.

You would also need to use wp_get_current_user to know the ID of the user to be supplied to the update_user_meta() and get_user_meta() functions.

Finally, using wp_login action hook, you could reinitialise to the default value of the current year by calling update_user_meta() again. Alternatively, you could do some actions on the wp_logout hook

Note that it is ok to use update_user_meta even if the key doesn't exists, as the function will create it, if it doesn't find a matching key for that user.

Check the wp_ajax_(action) hook to learn how to create your handler for your AJAX requests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.