Skip to content

Instantly share code, notes, and snippets.

@simonline
Last active July 29, 2020 18:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonline/f3ded7504701ce0594992184b6729246 to your computer and use it in GitHub Desktop.
Save simonline/f3ded7504701ce0594992184b6729246 to your computer and use it in GitHub Desktop.
Some useful code snippets when integrating WECHANGE with Wordpress
// Register getURLVar for jQuery
$.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function(name){
return $.getUrlVars()[name];
}
});
// Init search form
$('.et_pb_searchform').submit(function (e) {
e.preventDefault();
location.href = 'https://mokwi.de/map/?q=' + $(this).find('[name=s]').val() + '&ne_lat=55.05320258537112&ne_lon=21.02783203125&sw_lat=49.83798245308484&sw_lon=5.789794921875&people=true&events=true&projects=true&groups=true&limit=400';
});
/*
Get WECHANGE status
1. Replaces menu item with css class "login" with avatar image
2. Updates body class (wechange-logged-in/wechange-logged-out) accordingly to show/hide elements based upon CSS definitions
like e.g.
body.wechange-logged-in .hide-logged-in,
body.wechange-logged-out .hide-logged-out {
display: none;
}
*/
$.ajax({
url: '/api/v1/user/me/',
type: 'GET',
success: function (data) {
if (data.id) {
if ((location.pathname == "/" || location.pathname == "/cms/") && $.getUrlVar("noredir") != '1') {
window.location.href = "/dashboard/";
} else {
// Update navigation links
$('nav .login a').html('<img src="' + data.avatar_url + '" alt="' + data.first_name + ' ' + data.last_name + '" />');
// Update body class
$('body').addClass('wechange-logged-in');
}
} else {
$('body').addClass('wechange-logged-out');
}
},
cache: false,
crossDomain: true,
xhrFields: {
withCredentials: true
},
});
// Init AJAX behaviour of Join Group links
$('a.wechange-join-group').click(function (e) {
e.preventDefault();
$.ajax({
url: $(this).attr("href"),
type: 'POST',
success: function (data) {
location.reload();
},
cache: false,
crossDomain: true,
xhrFields: {
withCredentials: true
},
});
});
// Map iframe sample code
// Other possible parameters are e.g. filter_group, region
<iframe src="/map/embed/?search_result_limit=1000" width="100%" height="700px" frameborder="0" style="border:0" allowfullscreen="" class="full-height"></iframe>
// Counters for groups, projects, users posts+
// place into Wordpress theme functions.php
// TODO: Translate to Javascript
function indicator_handle( $atts ) {
$string = file_get_contents("https://mokwi.de/api/v1/statistics/general/");
$obj = json_decode($string);
return $obj->{$atts["attribute"]};
}
add_shortcode( 'indicator', 'indicator_handle' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment