plugins - How To Toggle User_Meta in frontend in a form using PHP

I am trying to make a small form, that just has a submit button, and on submission, it should toggle user meta for the W

I am trying to make a small form, that just has a submit button, and on submission, it should toggle user meta for the WordPress user. The button exists on frontend after content of post, and has a action too.

<form action="" method="POST">
<input type="submit" name="test" id="test" onclick=""/><br/> </form>

The above was the html form code, i can assure that it echoes properly. Then i add the below line to trigger it. This is php

if (isset($_GET['dark'])){ 
testfun(); 
}

This calls a function called testfun, the function testfun is below:

function testfun(){
$user_id = get_current_user_id();
$theme= get_user_meta($user_id, 'theme', true);
if ($theme == 'dark') {
   update_user_meta($user_id, 'theme', 'light');
   }
   else{
   update_user_meta($user_id, 'theme', 'dark');
   }
}

And obviously this code doesn't work, and i fail to figure our why. I have an intuition that the way i gey user id, can be wrong, because rest all seems to be fine. Anyhow please help me with this code.

I basically want to create a button that for the current logged in user, updated the user meta key 'theme' and toggles it between values 'light' and 'dark'

And as far as I am concerned i couldn't find anything wrong in it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745104100a4611453.html

相关推荐

  • plugins - How To Toggle User_Meta in frontend in a form using PHP

    I am trying to make a small form, that just has a submit button, and on submission, it should toggle user meta for the W

    16小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信