Topic on Extension talk:WPMW

Solution to syncing WordPress-MediaWiki logouts

5
Danielxvu (talkcontribs)

Navigate to your current WordPress theme's root directory, and add the following lines of code to functions.php:

add_action('wp_logout', 'mw_logout');
function mw_logout() {
    $cookiesSet = array_keys($_COOKIE);
    for ($x=0;$x<count($cookiesSet);$x++) setcookie($cookiesSet[$x],"",time()-1);
}
92.110.165.206 (talkcontribs)

This worked fine, but it also deletes all other cookies on the same domain. I wrote something different to make sure only the wiki-related cookies are deleted.

// Synch Wordpress-MediaWiki logouts
add_action('wp_logout', 'mw_logout');
function mw_logout() {	

	// IMPORANT: Change this to your own database name
	$wikiDatabaseName = "soulslike_wiki";

	// Goes through all cookies, if it finds a cookie starting with your database name it will remove it.
	foreach($_COOKIE as $cookieKey => $cookieValue) {
		if(strpos($cookieKey,$wikiDatabaseName) === 0) {
			// remove the cookie
			setcookie($cookieKey, null, -1);
			unset($_COOKIE[$cookieKey]);
		}
	}
}
92.110.165.206 (talkcontribs)

This no longer seems to work as of MediaWiki 1.26.2

Swennet (talkcontribs)

Updated this to work with the latest version of MediaWiki (1.26.2)

function mw_logout() {
	// IMPORANT: Replace mediawiki_db with your wiki database name
	$wikiDatabaseName = "mediawiki_db";

	// Goes through all cookies, if it finds a cookie starting with your database name it will unset it.
	foreach($_COOKIE as $cookieKey => $cookieValue) {
		if(strpos($cookieKey,$wikiDatabaseName) === 0) {
			// remove the cookie
			unset($_COOKIE[$cookieKey]);
			
			// IMPORTANT: Replace .mediawiki.org with your domain name
			setcookie($cookieKey, '', time() - 3600, '/', '.mediawiki.org');
		}
	}
}
add_action('wp_logout', 'mw_logout');
92.110.165.206 (talkcontribs)

Thank you for this, works perfectly so far :)

Reply to "Solution to syncing WordPress-MediaWiki logouts"