External popup onclick to open only once per sessi

2019-07-27 23:15发布

I was able to duplicate the functional popup (coding is hell for me). The problem is that every time you click on the page. What's annoying. Is there a possibility for me as a beginner, so that I can only open the window once for the site? It's for a wordpress page. The code I inserted into the body. Well thank you Here is the code I wrote

<javascript:void(0);" onClick="window.open('<?php echo "https://prehrajto.cz/hledej/" .  $post->post_title ."?cc=uga3bwln" ?>','prehrajto','resizable=yes,scrollbars=yes,');">    

I try this code , its open once per session , but its blocked browser . It would not mind, it's not the onclick function.

<script type="text/javascript">
onClick=window.open('<?php echo "https://prehrajto.cz/hledej/".get_the_title()."?cc=uga3bwln" ?>','prehrajto','resizable=yes,scrollbars=yes,');
</script>

And my programming experience is zero. I have a problem to combine code and to work with cookies . Im lost .

3条回答
倾城 Initia
2楼-- · 2019-07-27 23:45

you can do with partial javascript partial php,

1) Php Based Cookie - Session session check

2) Javascript : Cookie - LocalStorage based session check

查看更多
放我归山
3楼-- · 2019-07-27 23:52

yes, you can do that using cookies .

php has functions for dealing with cookies , mainly setcookie() and companions

**/ Set a Cookie /* 

    add_action( 'init', 'set_mycookie' );
    function set_mycookie() {
    setcookie( 'cookiename', 'cookievalue', time() + 3600, COOKIEPATH, COOKIE_DOMAIN   );
    }

**/ Get a Cookie /*

    add_action( 'wp_head', 'get_mycookie' );
    function get_mycookie() {
    $myvar= isset( $_COOKIE['cookiename'] ) ? $_COOKIE['cookiename'] : 'cookie not set';

    }

**/ Delete or Unset /* 

    add_action( 'init', 'unset_mycookie' );
    function unset_mycookie() {
    setcookie( 'cookiename', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN );
    }

now, of course these are the most basic general examples, but you could use it with any set of conditions, ( like @Bibberty comment/question ) for example by session, by user, by day, by page, etc . whatever you want .

Sidenote - wp has dedicated functions for using cookies, mainly for auth and session management ( which wp does not do by default ). see wp_set_auth_cookie() to learn more

sidenote 2 Also, regarding your code ... embading php inside JS ( or viceversa...) is a bad practice. wp has actually a very cool and elegant function to deal with that, and if you really want to learn php/wp - you better start using it now

查看更多
男人必须洒脱
4楼-- · 2019-07-27 23:56

Its done , I did a popup with a css class with a z-index, and I inserted a transparent image as a external link with _blank and onclick into body. Maybe unprofessional but functional

查看更多
登录 后发表回答