Page MenuHomeMiraheze

How to insert a user link in the header for anonymous users?
Closed, ResolvedPublic

Description

https://sdiy.miraheze.org and https://simpleelectronics.miraheze.org

To inhibit spambots I've removed anonymous user group's createaccount user right on these two wikis. This hides Special:CreateAccount for * user group. How to add a user link in the header (top right on every page)? That is to change for anonymous users Log in to the usual Create account Log in except with Create account being now instead an external link to https://login.miraheze.org/wiki/Special:CreateAccount

Event Timeline

Rob_Kam renamed this task from How to change the user link in the header for anonymous users? to How to insert a user link in the header for anonymous users?.Sat, Apr 3, 09:40
Rob_Kam updated the task description. (Show Details)

You might be able to use common.js and the personal links bar.

That seems to be the only way it can be done, with addPortletLink.

This is what I have so far, but it has no effect:

// Add a "Create account" link to the personal toolbar, only for anonymous users.
if (mw.user.isAnon()) {
 var newElement = mw.util.addPortletLink(
 'p-tb',
 '//login.miraheze.org/wiki/Special:CreateAccount',
 'Create account',
 't-mworg',
 'Create an account to edit',
 'm',
 '#pt-login'
 );
};

This is what I have so far, but it has no effect:

// Add a "Create account" link to the personal toolbar, only for anonymous users.
if (mw.user.isAnon()) {
 var newElement = mw.util.addPortletLink(
 'p-tb',
 '//login.miraheze.org/wiki/Special:CreateAccount',
 'Create account',
 't-mworg',
 'Create an account to edit',
 'm',
 '#pt-login'
 );
};

Not certain, but it might need this instead:

mw.loader.using( [ 'mediawiki.user' ], function () {
  // Add a "Create account" link to the personal toolbar, only for anonymous users.
  if ( mw.user.isAnon() ) {
   var newElement = mw.util.addPortletLink(
       'p-tb',
       '//login.miraheze.org/wiki/Special:CreateAccount',
       'Create account',
       't-mworg',
       'Create an account to edit',
       'm',
       '#pt-login'
   );
  }
} );

Or similar, may need to make a few changes if I made syntax errors, etc...

I don't have time to look into it in detail right now though.

This comment was removed by Rob_Kam.

Thanks however that still doesn't do it.

Joritochip added a subscriber: Joritochip.

Universal_Omega almost had it right, but the desired portletId would be p-personal instead of p-tb.

mw.loader.using( [ 'mediawiki.user', 'mediawiki.util' ], function () {
  // Add a "Create account" link to the personal toolbar, only for anonymous users.
  if ( mw.user.isAnon() ) {
    var newElement = mw.util.addPortletLink(
       'p-personal',
       '//login.miraheze.org/wiki/Special:CreateAccount',
       'Create account',
       'pt-mworg',
       'Create an account to edit',
       'm',
       '#pt-login'
    );
  }
} );

This is what it is currently at MediaWiki:Common.js but it still has no effect:

mw.loader.using( [ 'mediawiki.user', 'mediawiki.util' ], function () {
  // Add a "Create account" link to the personal toolbar, only for anonymous users.
  if ( mw.user.isAnon() ) {
   var newElement = mw.util.addPortletLink(
       'p-personal',
       'https://login.miraheze.org/wiki/Special:CreateAccount',
       'Create account',
       't-create',
       'Create an account to edit',
       'null',
       'pt-login'
   );
  }
} );

The reason it did not work is because you have your entire Common.js wrapped in a mw.loader call with a dependency on mediawiki.notification, which was not loading, so the callback is never called. Removing this dependency should allow your code to work again.

Also, in the code block you posted above, the keybind parameter "null" is a string, you should remove the quotes to make it truly null. Additionally, in order to make the "Create account" link appear before the "Log in" link, the last parameter in the call to addPortletLink should be a CSS selector, so make sure to add the # before pt-login (should be #pt-login). See below for the full block of code with both of these issues corrected:

mw.loader.using( [ 'mediawiki.user', 'mediawiki.util' ], function () {
  // Add a "Create account" link to the personal toolbar, only for anonymous users.
  if ( mw.user.isAnon() ) {
    var newElement = mw.util.addPortletLink(
       'p-personal',
       'https://login.miraheze.org/wiki/Special:CreateAccount',
       'Create account',
       'pt-create',
       'Create an account to edit',
       null,
       '#pt-login'
    );
  }
} );

Thank you :-) it's now functional.