
Cookie Connector for (Beaver) Themer
Cookie Connector for Beaver Themer is an unofficial addon-plugin for the popular Beaver Themer plugin. It allows you to read cookie-values using the Beaver Themer Connector and use Conditional Logic to hide/show seperate nodes (modules, columns and/or rows) using cookie values.
Cookie Connector also allows you to create cookies using an AJAX call. For security measures you will need to write the AJAX yourself, but an example file on how to do that can be found here, in the example folder:
https://github.com/badabingbreda/cookie-connector-for-themer/
Using the Cookie Connector
You can display the Cookie Connector wherever you’d normally insert it as a string, using either the connect or insert button.
Using the Conditional Logic filter
Because cookies have a certain validity, it can’t return a value when the cookie isn’t there or has become invalid. The Conditional Logic filter has an extra parameter to set a default value for whenever it doesn’t return anything. Setting this parameter return that value whenever it doesn’t exist.
Writing cookies values
Cookies are written before any headers are written to the visitor’s browser. The downside to that is that cookies can only be read when the visitor’s sends a request, since they are sent over WITH the request. This means that you can’t write a cookie’s value and immediately read that cookies new value, within a single run of a page-call.
To work around that, cookies can be written using an AJAX call.
“Cookie Connector for Themer” enqueues a script that registers the cookieConnector object, which handles the security and sending of requests.
Let’s consider the following html-code that is used in a HTML module:
<p><a href="javascript:cookieConnector.set( 'setmycookie' , { cv: 'my cookie value' , valid: 3600 } );">Set cookie value</a></p> <p><a href="javascript:cookieConnector.set( 'unsetmycookie' );">Unset cookie value</a></p> The first parameter is the actionname that is going to be called in the PHP script, so in this example (see below) the part after ‘wp_ajax_’ and ‘wp_ajax_nopriv_’.
You can also add an extra, optional, third parameter debug that will add a console.log dump of the response, should you need to know what’s being answered:
<p><a href="javascript:cookieConnector.set( 'setmycookie' , { cv: 'my cookie value' , valid: 3600 } , true );">Set cookie value</a></p> <p><a href="javascript:cookieConnector.set( 'unsetmycookie', {} , true );">Unset cookie value</a></p> Adding the PHP code to create/change/delete the cookie
Clicking the link wil trigger an AJAX call that will set a cookie on the visitor’s device, if their browser allows it.
On the server-side, you will need to add one or two ajax callbacks, depending if the call can be made without being logged in.
<?php add_action( 'wp_ajax_setmycookie' , 'callback_setmycookie' ); add_action( 'wp_ajax_nopriv_setmycookie' , 'callback_setmycookie' ); add_action( 'wp_ajax_unsetmycookie' , 'callback_setmycookie' ); add_action( 'wp_ajax_nopriv_unsetmycookie' , 'callback_setmycookie' ); function callback_setmycookie() { // first check if this ajax call is // done using the script belonging to the installation // if( ! check_ajax_referer( 'cookie-security-nonce' , 'security' ) ) { wp_send_json_error( 'Invalid security token sent.' ); wp_die(); } // Check if visitor has accepted the cookies from Cookie Notice plugin // If you're using another plugin, you should write your own check here if ( !function_exists('cn_cookies_accepted') || !cn_cookies_accepted() ) { wp_die(); } if ( !defined( 'DOING_AJAX' ) ) define( 'DOING_AJAX' , TRUE ); // set your cookie name here $cookie_name = 'mylanguage'; $default_validity = 60 * 60; // = 60 minutes // try to get the cookie_value $cookie_value = isset( $_GET['cv'] ) ? $_GET['cv'] : false; // try to get the cookie validity period. If not default to default validity $cookie_valid = isset( $_GET['valid'] ) ? $_GET['valid'] : $default_validity; // if the action is setmycookie we need a cookie_value (cv) because else it will fail. Check for it and return an error if there isn't one. if ( ! $cookie_value && $_GET['action'] == 'setmycookie' ) { wp_send_json_error( array( 'success' => false, 'error' => '402', 'message' => 'cookie not set, no value given. ( cv )' ) ); } // check action parameter // UNSET mycookie if ( 'unsetmycookie' == $_GET['action'] ) { setcookie( $cookie_name , 'unset value' , time() - 1 , COOKIEPATH, COOKIE_DOMAIN , isset($_SERVER["HTTPS"]) ); wp_send_json( array( 'success' => true, 'message' => "Done unsetting cookie '{$cookie_name}'." ) ); } else if ( 'setmycookie' == $_GET['action'] ) { setcookie( $cookie_name , $cookie_value , time() + $cookie_valid , COOKIEPATH, COOKIE_DOMAIN , isset($_SERVER["HTTPS"]), true ); wp_send_json( array( 'success' => true, 'message' => "Done setting cookie '{$cookie_name}' to value '{$cookie_value}' with validity $cookie_valid seconds." ) ); } wp_die(); } It is advised to use wp_send_json with a ‘success’ parameter (either true or false) so that you check it in your javascript and add actions after sending the cookieConnector() command, for instance a reload of the page or forwarding to an url that you received from the server based on the click.
For instance, the javascript below will try to create the cookie. When successful it will display an alert on the browser and reload the page after 1.5 seconds.
<script> function cookie_trigger() { jQuery.when( cookieConnector.set( 'setmycookie', { cv: jQuery( '#selectlanguage' ).val(), valid: 5 * 60 }, 1 )) .done( function( data ) { if ( data.success ) { alert( data.message + '\r You can now safely shut down your browser, restart and visit this URL. This selectbox will reflect the last cookie value.' ); setTimeout( function() { location.reload(); }, 1500); } }); } </script> Special thanks
Thanks to 10UP for creating this github action for easy deployment. https://github.com/marketplace/actions/wordpress-plugin-deploy
version history
1.3.1
Forgot to update readme.md (this file). Small fix for isset because it’s not the value that we want (could be falsy) but return if the cookie is set.
1.3.0
Rewrite of plugin using Autoloader. Fixed isset error.
1.2.0
Removed admin_notice so that plugin can be used without Beaver Themer and Beaver Builder too (scripts), for ajax calls.
1.1.0
Updated the version because it works on WP 5.4 too
1.0.1
Updated example code
1.0.0
Initial release (January 18th, 2019)
Starting from $0 per month.
Rating
Reviewers
No reviews
Tags
Developed By
badabingbreda
Quick & Easy
Common Ninja has a large selection of powerful Wordpress plugins that are easy to use, fully customizable, mobile-friendly and rich with features — so be sure to check them out!
Testimonial plugins for Wordpress
Contact Form plugins for Wordpress
Maps plugins for Wordpress
Translation plugins for Wordpress
Chat plugins for Wordpress
Slider plugins for Wordpress
Reviews plugins for Wordpress
Contact plugins for Wordpress
Galleries plugins for Wordpress
SEO plugins for Wordpress
Forms plugins for Wordpress
Comments plugins for Wordpress
Backup plugins for Wordpress
Privacy plugins for Wordpress
Optimize plugins for Wordpress
Tabs plugins for Wordpress
Social Sharing plugins for Wordpress
Events Calendar plugins for Wordpress
Comments plugins for Wordpress
Social Feeds plugins for Wordpress
Social Sharing plugins for Wordpress
Portfolio plugins for Wordpress
Video Player plugins for Wordpress
popup plugins for Wordpress
SiteMap plugins for Wordpress
Payment plugins for Wordpress
Coming Soon plugins for Wordpress
Inventory plugins for Wordpress
Testimonials plugins for Wordpress
Portfolio plugins for Wordpress
Membership plugins for Wordpress
Forms plugins for Wordpress
Analytics plugins for Wordpress
Events Calendar plugins for Wordpress
Sliders plugins for Wordpress
Analytics plugins for Wordpress
Reviews plugins for Wordpress
Security plugins for Wordpress
Ads plugins for Wordpress
Music Player plugins for Wordpress
Countdown plugins for Wordpress
Email Marketing plugins for Wordpress
Membership plugins for Wordpress
Ecommerce plugins for Wordpress
Customer Support plugins for Wordpress
Video Player plugins for Wordpress
Tabs plugins for Wordpress
Social Feeds plugins for Wordpress
Common Ninja Apps
Browse our extensive collection of compatible plugins, and easily embed them on any website, blog, online store, e-commerce platform, or site builder.
Verify visitor age with an age verification popup that helps you meet legal requirements, protect your business, and ensure responsible access.
Add a comments system with replies, voting, and moderation to boost engagement and build an active community on your site.
Collect candidate information with a job application form that organizes submissions, streamlines hiring, and helps you manage applicants efficiently.
Create interactive March Madness brackets where users can make predictions, track tournament progress, and compete with others throughout every round.
Create interactive maps with color coding and tooltips to visualize data clearly and embed engaging maps on your site.
Track time since important events with a count up clock that displays elapsed days and hours and keeps visitors engaged.
Add collapsible content sections to your site to organize information and help users navigate content more efficiently.
Use an image magnifier to let visitors zoom in on photos, view fine details clearly, and enjoy a more accessible and informative visual experience.
Show a GDPR-ready cookie notice that clearly explains how cookies are used, helping you stay compliant while creating a more transparent experience for your visitors.
Showcase videos with a video gallery that organizes clips from multiple sources in clear visual layouts that keep visitors watching and support higher conversions.
Use a countdown to download widget that reveals files after the timer ends, builds anticipation, and guides visitors toward higher engagement.
Use a Cookie Banner to inform visitors about cookies on your site and let them manage their consent in a clear, structured way.
More plugins
The Common Ninja Search Engine platform helps website builders find the best site widgets, apps, plugins, tools, add-ons, and extensions! Compatible with all major website building platforms - big or small - and updated regularly, our Search Engine tool provides you with the business tools your site needs!
