-1

I am currently devising a plugin and my problem is that it works if I login as WP user but if I browse the page (not logged in), my plugin is not working. What am I missing here? What should I do to make Wordpress recognize my plugin at all times?

The plugin I'm currently working has something to do with cookies.

function my_cookie() {
    setcookie( 'test-cookie', 'hello world', time()+1209600, '/');
}
add_action( 'init', 'my_cookie' );

For output:

function output_test_cookie() {

if ( isset( $_COOKIE['test-cookie'] ) )
    echo $_COOKIE["test-cookie"];
}
add_action( 'wp', 'output_test_cookie' );

But it won't work unless I'll login as WP Admin.

I came across this question Why is PHP Cookie via plugin only set when logged in as Admin and not regular users? and other questions. We have the same problem but requesting to disable cache is not an option for me.

  • why down vote? I'm just asking for help. – silent_coder14 Apr 17 '18 at 22:16
  • @silent_code14 there's no information in your question, it's not clear what your plugin does, how it does it, and no code. It isn't even clear what's meant by it doesn't work. E.g. does it try to do something but fails when you're logged out? Or does it appear as if it doesn't even exist? Or does it give a WSOD? When you say recognise, what do you mean? Please edit your question with additional information so that an answer is possible. Remember, this is a Q&A site not a discussion forum – Tom J Nowell Apr 17 '18 at 22:47
  • When you say you cannot disable cache, why? With a cache enabled, your code will not run, the server will return a cached response – Tom J Nowell Apr 18 '18 at 1:02
  • Because it is plugin which is suppose to be distributed to identified clients. So by the idea disabling the cache in the server, it will only mean that we would request each of our clients to disable their cache which is not that ideal. – silent_coder14 Apr 18 '18 at 15:58
  • I don't follow, can you rephrase the last part of your previous comment? Note that what you're trying to do will never work with cached pages, it's one of those fundamental things. You need to bypass cache to set the cookie – Tom J Nowell Apr 18 '18 at 23:15
0

Although the RFC for cookies implies that dashes are OK in a cookie name, others have experienced issues with using dashes in a cookie name.

Try changing the name of the cookie to something without a dash. Only alphanumeric characters are allowed, but underline characters seem to be OK.

Reference this question/answer: https://stackoverflow.com/questions/18907771/can-http-cookie-name-contain-dashes/27235182 ; the accepted answer includes references to the RFC.

I personally try not to use dash characters in anything, unless I want to 'subtract'.

  • I've also use cookie name with out dashes but still won't work. Thanks for your input. – silent_coder14 Apr 18 '18 at 16:00

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.